Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active November 29, 2017 14:22
Show Gist options
  • Save danilobatistaqueiroz/37e714526e305712afb2c63902e9d41a to your computer and use it in GitHub Desktop.
Save danilobatistaqueiroz/37e714526e305712afb2c63902e9d41a to your computer and use it in GitHub Desktop.
working with protractor

Working With Protractor

installing protractor globally

npm install -g protractor

protractor --version

npm install webdriver-manager -g

webdriver-manager update


installing protractor as development dependency

npm install protractor --save-dev

npm install webdriver-manager --save-dev

webdriver-manager update


basic configuration

create a directory named: tests and inside:

create a directory named: e2e

create a file protractor.conf.js:

//protractor.conf.js
module.exports.config = {
	seleniumAddress: 'http://localhost:4444/wd/hub',
	capabilities: {
		'browserName': 'chrome'
	},
	specs: ['specs/*.spec.js'],
	baseUrl: 'http://www.protractortest.org/'
};

create a file: homepage.spec.js

//homepage.spec.js
describe('Homepage', function() {
	it('perform a search into the api page', function() {
		browser.get('#/api');
		element(by.model('searchTerm')).sendKeys('restart');
		element(by.css('.depth-1')).click();
		expect(element(by.css('.api-title')).getText()).toContain('browser.restart');
	});
});

Executing the scripts:

start webdriver server:
webdriver-manager start

from the root folder execute the protractor scripts:
clear ; protractor tests/e2e/protractor.conf.js


Another tutorial from the protractor's site:

At the root folder

create a file todo-spec.js:

describe('angularjs homepage todo list', function() {
  it('should add a todo', function() {
    browser.get('https://angularjs.org');

    element(by.model('todoList.todoText')).sendKeys('write first protractor test');
    element(by.css('[value="add"]')).click();

    var todoList = element.all(by.repeater('todo in todoList.todos'));
    expect(todoList.count()).toEqual(3);
    expect(todoList.get(2).getText()).toEqual('write first protractor test');

    // You wrote your first test, cross it off the list
    todoList.get(2).element(by.css('input')).click();
    var completedAmount = element.all(by.css('.done-true'));
    expect(completedAmount.count()).toEqual(2);
  });
});

create a conf.js file:

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['todo-spec.js']
};

execute the scripts:
protractor conf.js


Some rules:

Don't create e2e tests for features already covered by unit and integration tests

Use only one configuration file

Use automation tasks toolkit such as Gulp and Grunt.

Avoid xpath for locators

Declare one Page Object per file

Don't use mock unless it is absolutely necessary


credits for:
Walmyr Filho
https://www.casadocodigo.com.br/products/livro-protractor
I strongly recommend this excellent book, cheap for the value inside.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment