Cross browser testing with Nightwatch,
Travis and Saucelabs
Tudor Barbu
@motanelu
github.com/motanelu
http://tudorbarbu.ninja
Frontend engineer*
*The views and opinions expressed in this presentation are my own
Everybody has a testing environment. Some people are
lucky enough to have a totally separate environment to
run production in.
Unknown author
Sample app - plug and play news widget!
Fully working demo at https://github.com/motanelu/bcn-
js-news-widget
new NewsWidget({
'selector': '#widget',
'api': 'some.url',
'userId': 1
})
// news items are
// populated correctly
[
{
"id": 1,
"title": "Everybody's rich",
"url": "http://fake.news/item/1",
"excerpt": "description",
"image": "url",
"timestamp": "1492375010933"
}
…
]
Current tab is selected
Dates are formatted
Missing images are
handled
Changing the category
works
// categories are populated
// correctly
[
{
"id": 1,
"name": "Financial"
},
{
"id": 2,
"name": "Sports"
}
]
Smoke test (Wikipedia)
When the testing
environment is just right
● Popular Javascript testing framework
● Wraps around Selenium (W3C Webdriver compatible)
● Supports BDD style expectations and assertions
● Extensible via plugins
http://nightwatchjs.org
http://expressjs.com http://restify.com
● more popular
● robust
● lots of extras (not always useful)
● aimed at RESTful APIs
● less popular
● Cross-browser testing platform
● Over 800 platform / browser combinations
● Free for open source projects
● Automated testing
● Manual testing
https://saucelabs.com
$ brew cask install sauce-connect
$ export SAUCE_USERNAME="..."
$ export SAUCE_ACCESS_KEY="..."
$ sc
sauce-connect tunnel
Browser running on Saucelabs VM
GET http://127.0.0.1:9000/
Local server listening on 9000
https://travis-ci.com
● Integration server
● Out of the box support for nodejs
● Integration with Saucelabs (sauce-connect)
● Free for open source (https://travis-ci.org)
Anatomy of a test
Load the page
Load the script
Point the API to
a mocked URL
Run assertions
/^dist/.+/ - express.static
/^static/.+/ - express.static
/^mock/.+/
/mock/{test} => fixtures/{test}
Default configuration for Saucelabs
Each such object represents a platform (browser + OS) in
Saucelabs
Nightwatch configuration (paths, ...)
$ npm run test
Run a build
Configure Nightwatch
Start Express
Run the tests
Send status to Saucelabs
package.json
runner.js
express.conf.js
nightwatch.conf.js
Stop Express
nightwatch.globals.conf.js
Example test for the
header of the widget
Demo time
...what could possibly go wrong?
$ npm run test:debug
$ gem install travis
$ travis encrypt SAUCE_USERNAME=... --add
$ travis encrypt SAUCE_ACCESS_KEY=... --add
language: node_js
node_js:
- '7.4'
addons:
apt:
packages:
- oracle-java8-set-default
sauce_connect: true
before_install:
- export TZ=Europe/Madrid
script:
- npm run test
env:
global:
- secure: ki4GZ/fXn...
- secure: toxLrMviA9...
.travis.yml
Caveats
● Latency between Travis / localhost and Saucelabs can lead
to false negatives
● Difference in timezone (ex: 5 minutes ago messages)
● Booting VMs can be slow - try to group your tests and
optimize the testing schedule
jobs.schibsted.com
@Schibsted_Eng
bytes.schibsted.com
We’re hiring!
Thank you
@motanelu
github.com/motanelu
http://tudorbarbu.ninja

Testing frontends with nightwatch & saucelabs

  • 1.
    Cross browser testingwith Nightwatch, Travis and Saucelabs Tudor Barbu @motanelu github.com/motanelu http://tudorbarbu.ninja Frontend engineer* *The views and opinions expressed in this presentation are my own
  • 2.
    Everybody has atesting environment. Some people are lucky enough to have a totally separate environment to run production in. Unknown author
  • 3.
    Sample app -plug and play news widget! Fully working demo at https://github.com/motanelu/bcn- js-news-widget new NewsWidget({ 'selector': '#widget', 'api': 'some.url', 'userId': 1 })
  • 4.
    // news itemsare // populated correctly [ { "id": 1, "title": "Everybody's rich", "url": "http://fake.news/item/1", "excerpt": "description", "image": "url", "timestamp": "1492375010933" } … ] Current tab is selected Dates are formatted Missing images are handled Changing the category works // categories are populated // correctly [ { "id": 1, "name": "Financial" }, { "id": 2, "name": "Sports" } ] Smoke test (Wikipedia)
  • 5.
  • 6.
    ● Popular Javascripttesting framework ● Wraps around Selenium (W3C Webdriver compatible) ● Supports BDD style expectations and assertions ● Extensible via plugins http://nightwatchjs.org
  • 7.
    http://expressjs.com http://restify.com ● morepopular ● robust ● lots of extras (not always useful) ● aimed at RESTful APIs ● less popular
  • 8.
    ● Cross-browser testingplatform ● Over 800 platform / browser combinations ● Free for open source projects ● Automated testing ● Manual testing https://saucelabs.com
  • 9.
    $ brew caskinstall sauce-connect $ export SAUCE_USERNAME="..." $ export SAUCE_ACCESS_KEY="..." $ sc sauce-connect tunnel Browser running on Saucelabs VM GET http://127.0.0.1:9000/ Local server listening on 9000
  • 10.
    https://travis-ci.com ● Integration server ●Out of the box support for nodejs ● Integration with Saucelabs (sauce-connect) ● Free for open source (https://travis-ci.org)
  • 11.
  • 12.
    Load the page Loadthe script Point the API to a mocked URL Run assertions
  • 13.
    /^dist/.+/ - express.static /^static/.+/- express.static /^mock/.+/ /mock/{test} => fixtures/{test}
  • 14.
    Default configuration forSaucelabs Each such object represents a platform (browser + OS) in Saucelabs Nightwatch configuration (paths, ...)
  • 15.
    $ npm runtest Run a build Configure Nightwatch Start Express Run the tests Send status to Saucelabs package.json runner.js express.conf.js nightwatch.conf.js Stop Express nightwatch.globals.conf.js
  • 16.
    Example test forthe header of the widget
  • 17.
    Demo time ...what couldpossibly go wrong?
  • 19.
    $ npm runtest:debug
  • 20.
    $ gem installtravis $ travis encrypt SAUCE_USERNAME=... --add $ travis encrypt SAUCE_ACCESS_KEY=... --add language: node_js node_js: - '7.4' addons: apt: packages: - oracle-java8-set-default sauce_connect: true before_install: - export TZ=Europe/Madrid script: - npm run test env: global: - secure: ki4GZ/fXn... - secure: toxLrMviA9... .travis.yml
  • 21.
    Caveats ● Latency betweenTravis / localhost and Saucelabs can lead to false negatives ● Difference in timezone (ex: 5 minutes ago messages) ● Booting VMs can be slow - try to group your tests and optimize the testing schedule
  • 22.