npm install -g protractor
protractor --version
npm install webdriver-manager -g
webdriver-manager update
npm install protractor --save-dev
npm install webdriver-manager --save-dev
webdriver-manager update
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');
});
});
start webdriver server:
webdriver-manager start
from the root folder execute the protractor scripts:
clear ; protractor tests/e2e/protractor.conf.js
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
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.