34
Михаил Боднарчук Codegyre Acceptance Testing in NodeJS 1

Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

  • Upload
    fwdays

  • View
    545

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Михаил БоднарчукCodegyre

Acceptance Testing in NodeJS

1

Page 2: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Who Am I

Michael Bodnarchuk

@davert

PHP, Ruby, JS developer from Kyiv, Ukraine

Creator of testing frameworks Codeception (PHP), CodeceptJS (Node)2

Page 3: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

What is Acceptance TestingEnd-2-End, functional, etc

3

Page 4: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

An App:

4

Page 5: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

What’s inside a box?

5

Page 6: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Interface

6

GET A CHOCOLATE!!!Secured. No Credit Card Required. Free for OpenSource. Moneyback Guarantee. Just click this f*ckn green button!pleeeeease….

Made with Love by Willy Wonka

Page 7: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

What should we test?

7

Chocolate Factory (unit/integration) - make a chocolate

Interface (acceptance/e2e) - user can get a chocolate

Page 8: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Who should write tests?

8

Unit/Integration -

Developers

Acceptance -

QA Engineers

Page 9: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

We are developers!Let’s leave testing to QAs

9

Page 10: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

THE ENDThank you

10

Page 11: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Well-tested software is a productof collaboration

11

Do it as team!

Page 12: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

NodeJS for Test Automation Engineers (QA):

12

JavaScript???

How to deal with async?

Which tools to use?

How to test Single Page applications?

Using locators in dynamic HTML

Page 13: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Tools for Acceptance Testing

Selenium Webdriver

PhantomJS

Nightmare

ZombieJS

CasperJS13

Page 14: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Automating Browser with Selenium

14

Page 15: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

How to deal with

Locating Elements

Single Page Applications

Cloud Testing

Data In Tests15

Page 16: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Locating Elements

“Hey, click me that green button”!

By CSS (JQuery style)

By XPath (Most flexible)

By Link Text

Reuse locators in PageObjects16

GET A CHOCOLATE!!!Secured. No Credit Card Required.

Page 17: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Single Page Applications

Browser automation faster than user

DOMReady vs “‘I’ve done it, master!”

Before each step:

executeAsync((done) => waitToRender(done))

17

Page 18: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Cloud Testing (SauceLabs, BrowserStack, TestingBot)

Testing app on different platforms (Windows, OSX)

Mobile testing (on real devices)

In different browsers (IE8+, FF, Opera, Safari)

Through Tunnels

Via WebDriver protocol 18

From 29$/month

Slower than local testing

Data overhead

Page 19: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Data in Tests

Isolation: Data should be cleaned between tests

Create/Delete all data inside a test

Loading fixtures into database

Loading database dump

Created/Deleted via API19

Page 20: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Testing Emails

Open Gmail in next tab, click the latest email….

Use MailDev, MailCatcher, MailHog, MailTrap

Check sent emails via REST API

20

Page 21: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Libraries for Acceptance Testing

Selenium Webdriver JS

webdriverio

Protractor

Intern

Nightmare21

NightwatchJS

CodeceptJS

WD.js

...

Page 22: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Different Bindings, Different APIs

client // webdriverio

.init()

.url('https://localhost/')

.setValue('input[name=login], 'john')

.setValue('input[name=password], '123456')

.click('input[type=submit]')

.getText('.welcome').then(function(text) {

return assert(text, 'Welcome');

});

driver.get('http://localhost/'); // Protractor

driver.findElement(protractor.By.name('login'))

.sendKeys('john');

driver.findElement(protractor.By.name('password'))

.sendKeys(‘123456’);

22

client // NightwatchJS

.url('http://localhost.com')

.setValue('input[type=login]', 'john')

.setValue('input[type=password]', '123456)

.click('button[type=submit]')

.assert.containsText('.welcome','Welcome')

.end();

Page 23: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

How to choose?Investigate, check your requirements

23

Page 24: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Choosing Library

24

Best docs, flexible API, synchronous ⇒ webdriverio

Java-like official Selenium library ⇒ Selenium WebdriverJS

For AngularJS ⇒ Protractor

Most featured ⇒ NightwatchJS

Acceptance Testing + Unit Testing ⇒ Intern

Page 25: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

CodeceptJSto rule them all

25

Page 26: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

CodeceptJS

One framework using other libraries

Synchronous (not really, uses global promise)

Scenario Driven

With PageObjects, Interactive Shell, Error output... 26

Page 27: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Synchronous looking Test

Scenario('log in as user', (I) => {

I.amOnPage('/');

I.click('Login');

I.fillField('Username', 'john');

I.fillField('Password', '123456');

I.click('Enter');

I.see('Welcome');

}

27

// assertion from webdriverio helper

see(text) {

return this.browser.getText()

.then(function (source) {

return stringIncludes()[assertType](text, source);

});

// action from webdriverio helper

click(locator, context) {

let client = this.browser;

let clickMethod = this.browser.isMobile ? 'touchClick' : 'elementIdClick';

if (context) {

client = client.element(context);

}

return findClickable(client, locator).then(function (res) {

if (!res.value || res.value.length === 0) {

if (typeof(locator) === "object") locator = JSON.stringify(locator);

throw new Error(`Clickable element ${locator.toString()} was not found

by text|CSS|XPath`);

}

let elem = res.value[0];

return this[clickMethod](elem.ELEMENT);

});

}

// adding to global promise

recorder.addStep(new Step(helper, action), args);

return recorder.promise();

Page 28: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Scenario Driven: Executed Step by Step

• I am on page ‘/’

• I fill field ‘Username’, ‘john’

• I fill field ‘Password’, ‘123456’

• I click ‘Enter’

• I see ‘Welcome’

✓OK

28

Page 29: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Interactive Shell

29

Page 30: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

PageObject, PageFragment

Scenario('log in as user', (I, loginPage) => {

loginPage.logInWith('john', '123456');

I.see('Welcome');

}

30

Automatically Injected

(angular-style)

// Login steps moved to loginPage:

logInWith(name, pass) {

I.amOnPage('/');

I.click('Login');

I.fillField('Username', name);

I.fillField('Password', pass);

I.click('Enter');

}

Page 31: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

And….

Based on Mocha testing framework.

Designed for scenario driven acceptance testing in BDD-style

Uses ES6 natively without transpiler.

Selenium WebDriver integration using webdriverio (..Protractor, Nigthmare)

Easily create tests, pageobjects, stepobjects with CLI generators.

31

Page 32: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Try CodeceptJS today!

http://codecept.io

npm install -g codeceptjs

@codeceptjs

Author: Michael Bodnarchuk @davert 32

Page 33: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Conclusions

Acceptance Testing should be done by developers and QAs

Use the same language for code and tests

NodeJS browser testing is hard (various libraries, async)

Use REST API for data, fetching emails, etc

Use CodeceptJS33

Page 34: Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"

Questions?

Michael Bodnarchuk

Follow me: @davert

https://github.com/codeception/codeceptjs

http://codecept.io

34