This presentation focuses on implementing Page Objects Model using Protractor for AngularJS apps for more maintainable, reusable and flexible end-to-end testing for your project. The presentations was done at 99X Technology as a Tech Talk session done by Team Finale.
How to submit a standout Adobe Champion Application
Ā
Better End-to-End Testing with Page Objects Model using Protractor
1. Better End-to-End Testing With
Page Object Model
Using Protractor
Kasun Kodagoda | Team Finale
Trainee Software Engineer | 99X Technology
2. Overview
ā¢ Protractor
ā¢ What is it?
ā¢ Getting Up and Running
ā¢ Basic Requirements
ā¢ Page Objects
ā¢ What is it?
ā¢ Why Use it?
ā¢ A Deeper Dive..
ā¢ Demo
4. What Is It?
āProtractor is an end-to-end test framework for AngularJS
applications. Protractor runs tests against your application running in
a real browser, interacting with it as a user wouldā
Source: Official Protractor Website
5. What Is It?
ā¢ Combination of powerful technologies and tools for testing
Node.js, Selenium, Jasmine, WebDriver, Cucumber, Mocha
ā¢ Wrapper for Selenium, made for Angular.js
ā¢ Uses angular specific constructs for capturing elements
Model, bindings, repeaters etc.
ā¢ Runs on real browsers as well as headless browsers
6. Get Up And Running..
ā¢ Install Node.js
ā¢ Install Protractor
npm install āg protractor
ā¢ Install WebDriver
webdriver-manager update
ā¢ Start WebDriver
webdriver-manager start // do this before you run the tests
7. Basic Requirements
ā¢ Youāll need 2 basic files to start,
ā¢ A Test/Spec file
Contains the element references, assertions
ā¢ The Configuration File
Contains the configuration options for running protractor
ā¢ Use your preferred test framework to write tests
ā¢ Jasmine ā Fully Supported
ā¢ Mocha ā Limited Support
ā¢ Cucumber ā Limited Support
9. Sample Test
describe('Test Home Page', function () {
it("Should display the text typed in to the input field.", function () {
browser.get('http://localhost:8080/#/');
// Type in a value to input field
var textInput = element(by.model('sampleInput'));
textInput.sendKeys('The KVK Blog');
// Check if the same value is displayed
var displayField = element(by.binding('sampleInput'));
expect(displayField.getText()).toEqual('The KVK Blog');
});
});
10. Conventional UI Tests
ā¢ Element references are inside tests
ā¢ Test are cluttered
ā¢ Less readable
ā¢ Very fragile
12. What Is A Page Object?
āA page object is an object-oriented class that serves as an interface to
a page of your AUTā
ā¢ Wraps a HTML page/fragment
ā¢ Provide Application Specific API
ā¢ Hides the underlying complexity
ā¢ Used to manipulate elements on page
13. Why Use Page Objects?
ā¢ Reduce Code Duplication
ā¢ Reusable Page Objects
ā¢ More Readable Tests
ā¢ Less Vulnerable to Break
ā¢ More Maintainable Tests
Especially in Agile Based Projects
14. A Deeper Diveā¦
Return Types
ā¢ Methods in Page Objects should return fundamental types
Strings, Dates, Booleans
ā¢ It can return other complex types as well
Promises, other Page Objects
15. A Deeper Diveā¦
ā¢ Method names can be much closer to what the user is actually doing.
var inputField = element(by.model(āfirst_nameā);
inputField.sendKeys(āKasunā);
Or
element(by.model(āfirst_nameā)).sendKeys(āKasunā);
Instead We can use
somePage.addFirstName(āKasunā);
16. A Deeper Diveā¦
The Dilemma
ā¢ Use of assertions
ā¢ Include Assertions inside Page Objects
ā¢ Not Include Assertions inside Page Objects
ā¢ Itās Subjective and the Choice is yoursā¦
17. A Deeper Diveā¦
No Assertions in Page Objects for Us. Why??
ā¢ Separation of Responsibility
ā¢ Page Objects should only provide access to HTML pages
ā¢ Page Objects become longer
18. A Deeper Diveā¦
What We Believe
āIf what we test changes, The assertions and the tests should changeā
āIf the HTML page changes, The Page Object should changeā
This,
ā¢ Allows us to easily modify the tests/specs when functionality or
underlying HTML pages change overtime