Testing is about gaining confidence that your code does what you think it should do
Protractor is built on top of WebDriverJS, which is an API, written as extensions, for controlling browsers.
Use npm to install Protractor globally with
sudo npm install protractor -g
Use webdriver-manager to download the necessary binaries with
sudo webdriver-manager update
Now start up a server with
sudo webdriver-manager start
// conf.js
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js'],
capabilities: {
browserName: 'firefox'
}
}
// conf.js
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js'],
multiCapabilities: [{
browserName: 'firefox'
}, {
browserName: 'chrome'
}]
}
// spec.js
describe('Protractor Demo App', function() {
it('should have a title', function() {
browser.get('http://juliemr.github.io/protractor-demo/');
expect(browser.getTitle()).toEqual('Super Calculator');
});
});
The describe and it syntax is from the Jasmine framework. browser is a global created by Protractor, which is used for browser-level commands such as navigation with browser.get.
// spec.js
describe('by model', function() {
it('should find an element by text input model', function() {
var username = element(by.model('username'));
var name = element(by.binding('username'));
username.clear();
expect(name.getText()).toEqual('');
username.sendKeys('Jane Doe');
expect(name.getText()).toEqual('Jane Doe');
});
});
by.model(modelName):Find an element by ng-model expression.
by.binding:Find an element by binding.
// spec.js
describe('Protractor Demo App', function() {
it('should add one and two', function() {
browser.get('http://juliemr.github.io/protractor-demo/');
element(by.model('first')).sendKeys(1);
element(by.model('second')).sendKeys(2);
element(by.id('gobutton')).click();
expect(element(by.binding('latest')).getText()).
toEqual('5'); // This is wrong!
});
});
element takes one parameter, a Locator, which describes how to find the element. The by object creates Locators.
start the webdriver server
webdriver-manager start
run Protractor in another terminal
protractor test/e2e/config.js //this is the relative path to your config.js file