protractor tutorial quality in agile 2015

53
UI Testing with Protractor Andrew Eisenberg Tasktop Technologies

Upload: andrew-eisenberg

Post on 17-Jul-2015

220 views

Category:

Internet


2 download

TRANSCRIPT

Page 1: Protractor Tutorial Quality in Agile 2015

UI Testing with Protractor

Andrew Eisenberg

Tasktop Technologies

Page 2: Protractor Tutorial Quality in Agile 2015

• Sr. Software Engineer @ Tasktop Technologies

• PhD from UBC in programming languages & IDEs

Page 3: Protractor Tutorial Quality in Agile 2015

A Cautionary Tale

100% Code Coverage!!!

…but I was playing the wrong game.

Page 4: Protractor Tutorial Quality in Agile 2015

Testing an app in 7+ easy steps

1. Installing and running Protractor

2. First tests

3. Creating test scenarios

4. Real tests

5. Synchronicity and Asynchronicity

6. Page object pattern

7. Debugging, screen capture, and other tips

8. Discussion items and gotchas

Page 5: Protractor Tutorial Quality in Agile 2015

BACKGROUND

Step 0

Page 6: Protractor Tutorial Quality in Agile 2015

Kinds of testing

• Unit testing

• Integration testing

• System testing (or Functional testing)

– UI Testing Using Protractor

A simplification!

Page 7: Protractor Tutorial Quality in Agile 2015

What is Protractor?

• “End-to-End testing”

– Well, not really. Just UI testing

• Built to work with AngularJS

• Built on top of Selenium

• Is a node app

Page 8: Protractor Tutorial Quality in Agile 2015

http://www.thoughtworks.com/insights/blog/testing-angularjs-apps-protractor

Page 9: Protractor Tutorial Quality in Agile 2015

When should I use Protractor?

• UI stable

• Use cases well established

• Already unit-tested

• Uses AngularJS

• UI unstable

• Use cases in flux

• Not unit-tested

• Doesn’t use AngularJS

Do it Avoid

Page 10: Protractor Tutorial Quality in Agile 2015

INSTALLING AND RUNNING PROTRACTOR

Step 1

Page 11: Protractor Tutorial Quality in Agile 2015

Installing and running Protractor

• Install node/npm from https://nodejs.org/

• Install protractor, grunt

– npm install -g protractor

– npm install -g grunt-cli

• Update webdriver

– webdriver-manager update

Page 12: Protractor Tutorial Quality in Agile 2015

Getting tutorial code

• git clone https://github.com/aeisenberg/angular-app.git

• cd angular-app

• git checkout qia-step-1

• Open file: Quality-in-Agile-Protractor-Tutorial.md

• Follow instructions in file

Page 13: Protractor Tutorial Quality in Agile 2015

Run the app and play

user: [email protected]: nuthing

Page 14: Protractor Tutorial Quality in Agile 2015

FIRST TESTS

Step 2

Page 15: Protractor Tutorial Quality in Agile 2015

Protractor & Jasmine

describe('Home Page', function () {

it('should be the default page',

function () {

browser.get(browser.baseUrl);

expect(browser.getCurrentUrl())

.toEqual(browser.baseUrl +

'projectsinfo');

});

});

Page 16: Protractor Tutorial Quality in Agile 2015

Let’s Code!

• Getting started

– git checkout qia-step-2-before

• If you get stuck:

– git checkout qia-step-2-after

Page 17: Protractor Tutorial Quality in Agile 2015

SKETCHING TEST SCENARIOS

Step 3

Page 18: Protractor Tutorial Quality in Agile 2015

AuthenticationUser

management

Project management

Backlogmanagement

Sprintmanagement

Page 19: Protractor Tutorial Quality in Agile 2015

USE CASE-BASED TEST SCENARIOS

Step 4

Page 20: Protractor Tutorial Quality in Agile 2015

ElementFinderelement(by.css(‘#run-command’));

element(by.css(‘#run-command’)).isPresent();

element(by.css(‘button.command’)).click();

element.all(by.css(‘button.command’)).click();

element(by.css(‘#my-input’)).getText();

element(by.cssContainingText(‘button.command’, ‘Run’)).click();

browser.waitForAngular();

Page 21: Protractor Tutorial Quality in Agile 2015

Let’s Code!

• Getting started

– git checkout qia-step-4-before

• If you get stuck:

– git checkout qia-step-4-after

Page 22: Protractor Tutorial Quality in Agile 2015

MORE COMPLEX TEST

Step 5

Page 23: Protractor Tutorial Quality in Agile 2015

Secretly, everything is a promise

Page 24: Protractor Tutorial Quality in Agile 2015

This throws an exception:

This does not:

var text = element(

by.css(‘#my-input’)).getText();

expect(text.split(‘,’)[0]).toBe(‘sumthin’);

var text = element(

by.css(‘#my-input’)).getText();

expect(text).toContain(‘sumthin’);

Page 25: Protractor Tutorial Quality in Agile 2015

JavaScript Promises

• (Eventually) contains result of computation

• Asynchronous programming

• Composable, chainable, and…FUN!

• A standard

promise.then(function(result) {

verify(result);

}).then(function(result) {

display(result);

});

Page 26: Protractor Tutorial Quality in Agile 2015

ElementFinder returns promise

element(by.css(‘#my-input’))

.sendKeys(‘Hello’)

.getText()

.then(function(text) {

console.log(text);

});

All ElementFinder methods return promises

Page 27: Protractor Tutorial Quality in Agile 2015

element(by.css(‘#my-button’)).click();

element(by.css(‘#my-button’))

.isDisplayed();

element(by.css(‘#my-button’))

.click()

.isDisplayed();

element(by.css(‘#my-button’))

.click().then(function() {

element(by.css(‘#my-button’))

.isDisplayed();

});

Page 28: Protractor Tutorial Quality in Agile 2015

Protractor and `expect`

• expect accepts promises

– delays execution until promise is resolved

var text = element(

by.css(‘#my-input’)).getText();

expect(text.split(‘,’)[0]).toBe(‘sumthin’);

var text = element(

by.css(‘#my-input’)).getText();

expect(text).toContain(‘sumthin’);

Page 29: Protractor Tutorial Quality in Agile 2015

Let’s Code!

• Getting started

– git checkout qia-step-5-before

• If you get stuck:

– git checkout qia-step-5-after

Page 30: Protractor Tutorial Quality in Agile 2015

PAGEOBJECT PATTERN

Step 6

Page 31: Protractor Tutorial Quality in Agile 2015

The PageObject pattern

• Readable DSL for tests

• Promotes reuse

• Centralizes UI coupling

Page 32: Protractor Tutorial Quality in Agile 2015

PageObject Big Idea

• Extract CSS ugliness

– element(by.css('#login')).click();

• Into semantically meaningful actions

– loginPage.openDialog();

Page 33: Protractor Tutorial Quality in Agile 2015

Tests do not touch DOM directly

var loginPage = new require(‘./LoginPage)();

loginPage.login(‘[email protected]’, ‘secret’);

expect(loginPage.isLoggedIn().toBeTruthy();

loginPage.logout();

Page 34: Protractor Tutorial Quality in Agile 2015

Let’s Code!

• Page Object Skeleton

– git checkout qia-step-6-before

• Getting started

– git checkout qia-step-6-middle

• If you get stuck:

– git checkout qia-step-6-after

Page 35: Protractor Tutorial Quality in Agile 2015

DEBUGGING AND OTHER FUN THINGS

Step 7

Page 36: Protractor Tutorial Quality in Agile 2015

Let’s Code!

• Getting started

– git checkout qia-step-7

Page 37: Protractor Tutorial Quality in Agile 2015

Focused tests

• fit & fdescribe

• Focus protractor

– on a single test or set of tests

Page 38: Protractor Tutorial Quality in Agile 2015

Debug mode

• protractor debug protractor.conf.js

• Enter node’s debugger

– debugger;

• Enter debugger (webdriver aware)

– browser.debugger();

• Enter debugger (new feature doesn’t work too well)

– browser.pause();

Page 39: Protractor Tutorial Quality in Agile 2015

Element Explorer

• protractor --elementExplorer

• Interactive invoking of webdriver commands

Page 40: Protractor Tutorial Quality in Agile 2015

Interactive login

> element(by.css('#login')).click()

>

> element(by.css('#login-email').sendKeys(’[email protected]')

>

> element(by.css('#login-password').sendKeys(’nuthin')

>

> element(by.css('#do-login')).click()

Page 41: Protractor Tutorial Quality in Agile 2015

Screenshots

browser.takeScreenshot().then(function(png) {

var stream = fs.createWriteStream(filename);

stream.write(new Buffer(png, 'base64'));

stream.end();

});

Page 42: Protractor Tutorial Quality in Agile 2015

Jasmine reporters

• Hook into jasmine spec lifecycle

– before/after test/suite/jasmine

• Things to do

– Format test results

– Grab screenshot on failure

– Cleanup before/after tests

– Start/stop resources

– …

Page 43: Protractor Tutorial Quality in Agile 2015

Do something on failure

jasmine.getEnv().addReporter({

specDone: function(result) {

if (result.failedExpectations

.length > 0) {

console.log(‘I am a failure…’);

}

}

});

Page 44: Protractor Tutorial Quality in Agile 2015

SOME PHILOSOPHICAL QUESTIONS

Page 45: Protractor Tutorial Quality in Agile 2015

What should be unit tested?

What should be UI tested?

Page 46: Protractor Tutorial Quality in Agile 2015

How much refactoring in production code is

appropriate?

Page 47: Protractor Tutorial Quality in Agile 2015

Should test use id attributes or complex

CSS expressions?

Page 48: Protractor Tutorial Quality in Agile 2015

THE TRUTH BEHIND PROTRACTOR

Page 49: Protractor Tutorial Quality in Agile 2015

It ain’t perfect

• Timing problems

• Difficult to debug

• Inconsistent behavior

• Failures hard to reproduce

• Expensive to maintain

• Tests can be slow

Page 50: Protractor Tutorial Quality in Agile 2015

Protractor is appropriate

• When…

– Using AngularJS

– UI is mature

– Front end is well unit-tested

– Use cases well described

• Until then…

Page 51: Protractor Tutorial Quality in Agile 2015

…use manual automation

Page 52: Protractor Tutorial Quality in Agile 2015

Protractor is…

• For UI testing

• AngularJS-aware

• Secretly powered by Selenium

• Asynchronous and event-based

• A powerful and clean way to test UI

But…

• Expensive to maintain

Page 53: Protractor Tutorial Quality in Agile 2015

Questions?

[email protected]

• twitter: @werdnagreb

• https://github.com/aeisenberg/angular-app