Benedek Gagyi
Web dev.
Kudos to @ramonvictor
"Testing is about gaining confidence that your code does what you think it should do"
Testing system (Node.js, Java etc)
Webdriver (a.k.a. Selenium)
Protractor
npm install protractor -g
webdriver-manager update
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
specs: ['example-spec.js'],
onPrepare: function() {
//pretty self explanatory
},
jasmineNodeOpts: {
//Jasmine options
}
};
describe('Demo test', function() {
it('should have a title', function() {
browser.get('http://local.liligo.com');
expect(browser.getTitle())
.toEqual('Compare flights with Liligo.com', 'The title is not correct.');
});
});
We need a server to run our code on.
webdriver-manager start
In an other terminal, we can run the test.
protractor tests/e2e/config.js
element(by.binding('appName'))
element.all(by.css('ng-click="open page"')).get(0)
Single
Collection
<span ng-bind="myModel"></span>
<!-- or -->
<span>{{myModel}}</span>
element(by.binding('myModel'))
<input ng-model="myModel" />
element(by.model('myModel'))
<button ng-click="sendMail()">Send mail!</button>
element(by.css('[ng-click="sendMail()"]'))
<input id="address" type="text" ng-model="address">
<button class="btn" ng-click="sendMail()">Send mail!</button>
$('#address')
$('.btn')
<button class="btn" ng-click="sendMail()">Send mail!</button>
$('.btn').click()
<textarea ng-model="commentText"><textarea>
element(by.model('comment')).sendKeys('Hi!', protractor.Key.ENTER)
All Protractor methods are asynchronous, and return promises.
element( by.model('zipcode') ).getText()
.then(function(val) {
var num = val.substring(0, 4);
var isNum = !isNaN(num);
expect( isNum ).toBeTruthy();
});
To keep execution organized, a queue of pending promises is maintained: the control flow.
it('should find an element by text input model', function() {
browser.get('#/home'); // (1) method browser.get
// (2) method by.binding
var login = element(by.binding('login'));
// (3) method getText
expect(login.getText()).toEqual('User');
});
onPrepare()
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
onPrepare: function() {
browser.driver.manage().window().setSize(1600, 800);
},
jasmineNodeOpts: {
showColors: true
}
};
beforeEach()
beforeEach(function(){
//iPhone sized window
browser.driver.manage().window().setSize(320, 568);
})
params
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: { 'browserName': 'chrome' },
params: {
login: {
user: 'protractor-br',
password: '#ng123#'
}
}
};
browser.driver.findElement(by.css('[data-ptor="submit-btn"]'));
//in your configuration
onPrepare: function() {
global.wbdvr = browser.driver;
}
//in your test
wbdvr.findElement(by.css('[data-ptor="submit-btn"]'));
var AngularHomepage = function() {
this.nameInput = element(by.model('yourName'));
// ...
};
module.exports = AngularHomepage;
var AngularHomepage = require('./homepage.po.js');
describe('HomePage Tests', function() {
var angularHomepage = new AngularHomepage();
angularHomepage.nameInput.sendKeys('Rafael');
//...
});
$timeout, $http
$interval
If your app is based on Angular, your first choice should be Protractor, but...
Hit me up @BenedekGagyi!
By Benedek Gagyi
Test your Angular apps with Protractor!