44
END TO END TESTS WITH PROTRACTOR Hugh McCamphill

END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

END TO END TESTS WITH PROTRACTOR

Hugh McCamphill

Page 2: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Hugh McCamphill

• Test Lead with ShopKeep

• Organising Belfast Selenium Meetup since 2013

• Co-organizer of Belfast Software Testing Clinic

• @hughleo01

Page 3: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

SETUP

Open https://github.com/hughleo/protractor-workshop

and follow instructions

At the end you should have running:

• Angular application (which we will run tests against)

• And a backing API (for the Angular app to talk to)

You should also have been able to run a test with:

protractor protractor.conf.js (terminal / command line)

Page 4: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

AGENDA• Getting Setup

• Intro

• Configuration

• TypeScript

• Promises and Control Flow

• Page Objects and DSL

• Data Builder Pattern

• Wrapper methods

• Async / Await

• Close

@hughleo01

Page 5: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Intro

Page 6: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

The application you’ll be working with

Page 7: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

What have you installed

Page 8: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk
Page 9: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

PROTRACTOR OVERVIEW

Angular App

Page 10: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk
Page 11: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

DEMO

Running should navigate to create new article page in

e2e/specs/basic.e2e-spec.ts

Page 12: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Ensure you have a working email / password in params.

In terminal / command line:

>protractor protractor.conf.js

Exercise: Run first test

ç

Page 13: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Page 14: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

TYPESCRIPT

• Encapsulation through classes and modules

• Support for constructors, properties, functions

• Lambda style function support

• Provides static typing

• Intellisense and syntax checking

Page 15: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Page 16: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Exercise

Add a method to EditorPage page object• e2e/page-objects/basic/editor-page.po.ts

• Adding elements for title, description, body and

button

• Add method to enter these fields and click Publish

Page 17: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Add test for adding article• Go to e2e/specs/basic.e2e-spec.ts and finish the

test should add article

• Use the page objects already created to complete

the test

Exercise

Page 18: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

• Constructor for sync on page

• Return type for navigation

Demo : Page Objects

Page 19: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Promises (and adding expectations)

Page 20: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

WebDriverJS is an asynchronous API, where every command returns a promise. This can make even the simplest WebDriver scripts very verbose. Consider the canonical

"Google search" test:

Page 21: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Page 22: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

WebDriverJS uses a promise manager that tracks command

execution, allowing tests to be written as if they were using a synchronous API:

Page 23: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Page 24: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Demo: Promise returned from method

Demoing in e2e/specs/promise.e2e-spec.ts

with 'should navigate to create new article'

Page 25: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Exercise

Resolve promise when returning from a method• Go to e2e/specs/basic.e2e-spec.ts• Add expectation that article was added in ‘should add

article’

Page 26: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Page 27: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Page 28: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Demo: Async / Await

Page 29: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Exercise: Run async tests

• Go to e2e/specs/example.aysnc.e2e-spec.ts

• Run 'should add article' test

• Note the order in which the operations happen

Page 30: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Exercise

Update page object to do operations in order

• Go to e2e/page-objects/async/editor-page.po.ts

• Add async / await operations

• Update return type to be a promise

• Run previous test again

Page 31: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Demo: Wrapping Methods

Page 32: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Exercise: Add a clear and send keys method and use it for editor page

• Go to e2e/wrappers/element-wrapper.ts

• Add the clearAndSendKeys method

• Add calling it in updateArticleContent in e2e/page-

objects/async/editor-page.po.ts

Page 33: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Demo: domain models and builders

Page 34: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Exercise: Use domain object

Instead of passing information into article method one

variable at a time, pass it in as an object

Page 35: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

• Add a test to– Add comments to an article– Add then delete an article– Add then edit an article

Exercises

Page 36: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Recap

● Protractor

● Webdriver Manager

● TypeScript

● Promises and Control Flow

● Async / Await

● Page Objects and DSL

● Data Builder Pattern

● Wrapper methods

Page 37: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

How to’s

Page 38: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Waiting with sleeps

• If the sleep you have added is too short, then your test

may fail

• If the sleep you have added is too long, then you are

waiting unnecessarily, and this is a big deal when you

start to scale your number of tests

Page 39: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Prefer explicit waits

Page 40: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Ignoring Synchronization

If for example you had a login page that wasn’t an angular page

Page 41: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Using Generics for deciding navigation

Page 42: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Close out

Page 43: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Set directConnect to false to run tests through a grid / cloud service

Page 44: END TO END TESTS WITH PROTRACTOR - Nordic Testing Days · 2019-11-27 · • Angular application (which we will run tests against) • And a backing API (for the Angular app to talk

@hughleo01

Amending baseUrl