|
1 | 1 | # Protractor
|
2 |
| -> Best practices using Protractor |
| 2 | +> Some aspects for Protractor usage in projects |
3 | 3 |
|
4 |
| -## In construction |
| 4 | + |
| 5 | +## Table of contents |
| 6 | +- [Why End to End (E2E) tests](#why-end-to-end-e2e-tests) |
| 7 | +- [Why Protractor](#why-protractor) |
| 8 | +- [Boilerplate](#boilerplate) |
| 9 | +- [Best Practices](#best-practices) |
| 10 | +- [Page Objects](#page-objects) |
| 11 | +- [Why Page Objects](#why-page-objects) |
| 12 | +- [Page Object - Boilerplate](#page-object---boilerplate) |
| 13 | +- [Using Page Objects](#using-page-objects) |
| 14 | +- [Working with promises](#working-with-promises) |
| 15 | +- [Performance in your End to End Tests](#performance-in-your-end-to-end-tests) |
| 16 | +- [Using NodeJS in Protractor tests](#using-nodejs-in-protractor-tests) |
| 17 | + |
| 18 | + |
| 19 | +## Why End to End (E2E) tests |
| 20 | + |
| 21 | +End to End tests are pretty good tests for validate some aspects of your application , such as functionalities and interactions. For example, you will use End to End test in this cases: |
| 22 | + |
| 23 | +- Validate if your component works on in your application, generating a "happy way"; |
| 24 | +- Validate interaction in your browser between backend and frontend, based in a complete operation, like a login, search and other validations; |
| 25 | + |
| 26 | +> Unit Tests and End to End tests ever works together. |
| 27 | +
|
| 28 | + |
| 29 | +## Why Protractor |
| 30 | + |
| 31 | +Protractor is a tool created for attend developers of an easier way, with a reasonable setup, low context-switching and sensible syntax using all power of WebDrivers can do for us. |
| 32 | + |
| 33 | +Your configuration is very easy, that help in your use in applications. |
| 34 | + |
| 35 | + |
| 36 | +## Boilerplate |
| 37 | + |
| 38 | +> Basic step for use end-to-end tests |
| 39 | +
|
| 40 | +```javascript |
| 41 | +describe('angularjs homepage todo list', function() { |
| 42 | + |
| 43 | + beforeEach(function() { |
| 44 | + // before function |
| 45 | + ... |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(function() { |
| 49 | + //after function |
| 50 | + ... |
| 51 | + }); |
| 52 | + |
| 53 | + it('should add a todo', function() { |
| 54 | + browser.get('http://www.angularjs.org'); |
| 55 | + |
| 56 | + element(by.model('todoText')).sendKeys('write a protractor test'); |
| 57 | + element(by.css('[value="add"]')).click(); |
| 58 | + |
| 59 | + var todoList = element.all(by.repeater('todo in todos')); |
| 60 | + expect(todoList.count()).toEqual(3); |
| 61 | + expect(todoList.get(2).getText()).toEqual('write a protractor test'); |
| 62 | + }); |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | + |
| 67 | +## Best Practices |
| 68 | + |
| 69 | + |
| 70 | +### Page Objects |
| 71 | + |
| 72 | + |
| 73 | +#### Why Page Objects |
| 74 | + |
| 75 | +Page Object is a object model with properties and methods, wrapping page elements and interactions event (click, submit, etc). Your objective is simplify the test scripts and upgrade your code reuse in end-to-end tests, reducing amount of duplicate code and centralyzing UI modifications and fixes in only one file. |
| 76 | + |
| 77 | + |
| 78 | +#### Page Object - Boilerplate |
| 79 | + |
| 80 | +> A example of Page object construction |
| 81 | +
|
| 82 | +```javascript |
| 83 | +var PageObject = function() { |
| 84 | + // properties |
| 85 | + this.property = element(by.id('property')) |
| 86 | + ... |
| 87 | +} |
| 88 | + |
| 89 | +module.exports = new PageObject(); |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +#### Using Page Objects |
| 94 | + |
| 95 | +You should use Page Objects in your application tests. It's a best practice for manutenibility and sanity for your tests. A simple example using Page Objects in your protractor tests. |
| 96 | + |
| 97 | +```javascript |
| 98 | + |
| 99 | +describe('angularjs homepage todo list', function() { |
| 100 | + var pageObject = require('page.js'); |
| 101 | + |
| 102 | + beforeEach(function() { |
| 103 | + // before function |
| 104 | + browser.get('<your-url>'); |
| 105 | + ... |
| 106 | + }); |
| 107 | + |
| 108 | + afterEach(function() { |
| 109 | + //after function |
| 110 | + ... |
| 111 | + }); |
| 112 | + |
| 113 | + it('Page Object should be "Angular Testing Recipes" in your text content by default', function() { |
| 114 | + expect(pageObject.property.getText()).toEqual('Angular Testing Recipes'); |
| 115 | + }); |
| 116 | +}); |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +### Working with promises |
| 121 | + |
| 122 | +All page events returns for you test like as promise. In this case, your have to resolve the promises get your result and finish your test correctly. For example |
| 123 | + |
| 124 | +```javascript |
| 125 | +PageObject.property.click(); |
| 126 | +// your assertions are here |
| 127 | +``` |
| 128 | + |
| 129 | +can be used this way too |
| 130 | + |
| 131 | +```javascript |
| 132 | +PageObject.property.click().then(function(){ |
| 133 | + // your assertions are here |
| 134 | + ... |
| 135 | +}); |
| 136 | +``` |
| 137 | + |
| 138 | +You can use `waitForAngular()` method for that Protractor waits for AngularJS event finalization. |
| 139 | + |
| 140 | +```javascript |
| 141 | +browser.waitForAngular(); |
| 142 | +``` |
| 143 | + |
| 144 | +`expect()` method resolve the promises in your tests. For example: |
| 145 | + |
| 146 | +```javascript |
| 147 | +expect(PageObject.property.getText()).toEqual('your test'); |
| 148 | +``` |
| 149 | + |
| 150 | +### Performance in your End to End Tests |
| 151 | + |
| 152 | +Test your application with animation disabled. In many times has no sense to test your app with animations enabled and with this resource disabled the tests run more fast. |
| 153 | + |
| 154 | +```javascript |
| 155 | +allowAnimations(false); |
| 156 | +``` |
| 157 | + |
| 158 | +Disable angular debug informations |
| 159 | + |
| 160 | +```javascript |
| 161 | +$compileProvider.debugInfoEnabled(false); |
| 162 | +``` |
| 163 | + |
| 164 | + |
| 165 | +### Using NodeJS in Protractor tests |
| 166 | + |
| 167 | +In some cases you can the option of use NodeJS with protractor methods for more elaborated tests. In this example we are testing a ordenation click event in a tablesorter component, but using NodeJS for set table headers in our tests and caching table headers elements (get all elements that `by.css()` method return, resolving your promise and managing tests based in `items` values). |
| 168 | + |
| 169 | +```javascript |
| 170 | +// spec.js |
| 171 | +describe('TableSorter: testing component', function () { |
| 172 | + |
| 173 | + var tableSorter; |
| 174 | + |
| 175 | + it('changes active table order based in user\'s choice', function () { |
| 176 | + |
| 177 | + browser.get('/'); |
| 178 | + |
| 179 | + element.all(by.css('.table-sorter-order')).then(function(items) { |
| 180 | + |
| 181 | + expect(items.length).toBe(1); |
| 182 | + |
| 183 | + // Testing all elements with ordenation method |
| 184 | + ['Name', 'Email'].forEach(function(text, key){ |
| 185 | + |
| 186 | + describe('Testing Item "'+text+'"', function(){ |
| 187 | + // Order Asc |
| 188 | + it('ASC ordenation', function () { |
| 189 | + expect(items[key].getText()).toBe(text); |
| 190 | + items[key].click(); |
| 191 | + tableSorter = element(by.css('.table-sorter-order.asc')); |
| 192 | + expect(tableSorter.getText()).toEqual(text); |
| 193 | + }); |
| 194 | + // Order Desc |
| 195 | + it('DESC ordenation', function () { |
| 196 | + items[key].click(); |
| 197 | + tableSorter = element(by.css('.table-sorter-order.desc')); |
| 198 | + expect(tableSorter.getText()).toEqual(text); |
| 199 | + }); |
| 200 | + }); |
| 201 | + }); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | +}); |
| 206 | +``` |
0 commit comments