Top Banner
End-to-end tests with Protractor Walmyr Filho
73

Workshop - E2e tests with protractor

Jan 14, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Workshop - E2e tests with protractor

End-to-end tests with Protractor

Walmyr Filho

Page 2: Workshop - E2e tests with protractor

Agenda

● How it works● Basic configurations● Creating tests● The WebDriver control flow● Style guide● E2e tests for non-AngularJS apps● Questions + examples● References

Page 3: Workshop - E2e tests with protractor

End to end testing framework for AngularJS applications

Page 4: Workshop - E2e tests with protractor

Test pyramid - Mike Cohn

Page 5: Workshop - E2e tests with protractor

Differently of unit tests, the main intention of UI

tests (e2e tests) is to save time when running regression tests, that are very slow and boring to execute manually

Page 6: Workshop - E2e tests with protractor

How Protractor works?

Page 7: Workshop - E2e tests with protractor
Page 8: Workshop - E2e tests with protractor
Page 9: Workshop - E2e tests with protractor
Page 10: Workshop - E2e tests with protractor

protractor.conf.js

Page 11: Workshop - E2e tests with protractor
Page 12: Workshop - E2e tests with protractor

*.spec.js

Page 13: Workshop - E2e tests with protractor
Page 14: Workshop - E2e tests with protractor
Page 15: Workshop - E2e tests with protractor
Page 16: Workshop - E2e tests with protractor
Page 17: Workshop - E2e tests with protractor
Page 18: Workshop - E2e tests with protractor

WebDriver Control Flow

Page 19: Workshop - E2e tests with protractor

Promises ...

Page 20: Workshop - E2e tests with protractor

… and the Control Flow

Page 21: Workshop - E2e tests with protractor

So, you can write something like this:

Page 22: Workshop - E2e tests with protractor

Instead of this:

Page 23: Workshop - E2e tests with protractor
Page 24: Workshop - E2e tests with protractor

Style Guide

Page 25: Workshop - E2e tests with protractor

● General rules

● Project structure

● Locators strategy

● Page Objects

● Test suites

Style guide

Page 26: Workshop - E2e tests with protractor

● General rules

● Project structure

● Locators strategy

● Page Objects

● Test suites

Style guide

Page 27: Workshop - E2e tests with protractor

● Why?

○ Unit tests are much faster and e2e tests

○ Avoiding duplicity

Do not create e2e tests for functionalities already covered by unit tests

Page 28: Workshop - E2e tests with protractor

● Why?

○ You may use tools such as grunt or gulp

to override configurations

○ Avoiding configurations duplicity

Use only one configuration file

Page 29: Workshop - E2e tests with protractor

/* Avoid */

protractor.conf.dev.js

protractor.conf.stg.js

protractor.conf.local.js

/* Recommended */

protractor.conf.js -> gulp test-local; gulp test-dev; ...

Page 30: Workshop - E2e tests with protractor

● General rules

● Project structure

● Locators strategy

● Page Objects

● Test suites

Style guide

Page 31: Workshop - E2e tests with protractor

● Why?

○ Easy to locate tests

○ Separate e2e tests from unit tests

○ Cleaner test structure

Group e2e tests in a structure that make sense for the whole project (ex.: my-project/tests/e2e/)

Page 32: Workshop - E2e tests with protractor
Page 33: Workshop - E2e tests with protractor

● General rules

● Project structure

● Locators strategy

● Page Objects

● Test suites

Style guide

Page 34: Workshop - E2e tests with protractor

● Why?

○ Markup is easily subject to change

○ xpath has performance issues

○ xpath is not easily readable

NEVER use xpath

Page 35: Workshop - E2e tests with protractor
Page 36: Workshop - E2e tests with protractor

● Why?

○ Access elements easily

○ The code is harder to change than the

markup

○ More readable locators (e.g.: by.model,

by.binding, by.repeater)

Prefer Protractor specific locators when possible

Page 37: Workshop - E2e tests with protractor
Page 38: Workshop - E2e tests with protractor

● Why?

○ Access elements easily

○ This way you use markup that is less

subject to change

○ Readability

Prefer by.id and by.css when there is not Protractor specific locator available

Page 39: Workshop - E2e tests with protractor
Page 40: Workshop - E2e tests with protractor

Avoid text locators that change frequently● Why?

○ Texts of buttons, links and labels tend

to change during the time

○ Tests will break after simple changes in

the texts

○ Localization

Page 41: Workshop - E2e tests with protractor

● General rules

● Project structure

● Locators strategy

● Page Objects

● Test suites

Style guide

Page 42: Workshop - E2e tests with protractor

● Why?

○ Encapsulation of elements from the page

under test

○ Code reuse

○ Decoupling test logic from

implementation details

Use Page Objects to interact with the page under test

Page 43: Workshop - E2e tests with protractor
Page 44: Workshop - E2e tests with protractor

● Why?

○ Maintain the code clean and ease finding

what you are looking for

Declare one Page Object per file

Page 45: Workshop - E2e tests with protractor
Page 46: Workshop - E2e tests with protractor

Use just one module.exports at the end of the Page Object● Why?

○ One PageObject per file means that there

is only one class to export

Page 47: Workshop - E2e tests with protractor
Page 48: Workshop - E2e tests with protractor

Require and instantiate all node modules in the top of● Why?

○ The dependencies and node modules must be

clear and easy to find

○ Separations of dependencies and test code

○ Dependencies are then available for all the

test suite

Page 49: Workshop - E2e tests with protractor
Page 50: Workshop - E2e tests with protractor

● Why?

○ The user of the Page Object must have

quick access the the elements available

in the page

Declare all elements from the constructor as public

Page 51: Workshop - E2e tests with protractor
Page 52: Workshop - E2e tests with protractor

Declare functions for operations that require more than one step● Why?

○ Clean code / Readability

○ Code reuse

○ Maintainability

Page 53: Workshop - E2e tests with protractor
Page 54: Workshop - E2e tests with protractor

● Why?

○ Tests are responsible for the assertions

○ People that will read the tests must be able

to understand the application expected

behaviour by just reading the tests

Don't do assertions in the Page Objects

Page 55: Workshop - E2e tests with protractor

● Why?

○ You may reuse them in many tests

○ Avoid code duplicity

○ When a directive change, you just the to

change the wrapper, and only once

Add wrappers for directives, dialogs and common elements

Page 56: Workshop - E2e tests with protractor
Page 57: Workshop - E2e tests with protractor

● General rules

● Project structure

● Locators strategy

● Page Objects

● Test suites

Style guide

Page 58: Workshop - E2e tests with protractor

● Why?

○ E2e tests exist to simulate real users

using the application with all its

integrations

○ Using the real application with its own

dependencies provides high confidence

Do not use mock unless this is totally necessary

Page 59: Workshop - E2e tests with protractor

● Why?

○ It is well documented

○ It is also supported by the Protractor team

○ beforeAll e afterAll

Use Jasmine2

Page 60: Workshop - E2e tests with protractor
Page 61: Workshop - E2e tests with protractor

● Why?

○ Run tests in parallel with sharding

○ The execution order is not guaranteed

○ You can run a test suite isolated

Make tests independent at least at the file level

Page 62: Workshop - E2e tests with protractor
Page 63: Workshop - E2e tests with protractor

● Why?

○ You may execute test cases isolated

○ Easy to debug (e.g.: fit, xit)

Make tests totally independent of each other

Page 64: Workshop - E2e tests with protractor

Except when the operations to start the test are

too much expensive

● Why?

○ The tests would be running for ever

Make tests totally independent of each other

Page 65: Workshop - E2e tests with protractor

● Why?

○ Ensure that the main application parts

are well connected to each other

○ Real users don't navigate by URLs

Have a test suite to navigate through the main application routes

Page 66: Workshop - E2e tests with protractor

● Why?

○ Ensure that the tests is in a clean state

○ Avoid code duplicity

Navigate till the page under test before each test

Page 67: Workshop - E2e tests with protractor
Page 68: Workshop - E2e tests with protractor
Page 69: Workshop - E2e tests with protractor

Testing non-AngularJS apps

Page 70: Workshop - E2e tests with protractor
Page 71: Workshop - E2e tests with protractor

Questions?+

Examples (if you want to see some)

Page 73: Workshop - E2e tests with protractor

Takk!

Walmyr Filho