48
www.drupaleurope.org

Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

www.drupaleurope.org

Page 2: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Salva Molina

Browser Testing

Session URL: https://www.drupaleurope.org/session/browser-testing-nightwatchjs

with Nightwatch.js

13/9/2018

Page 3: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Salva MolinaPHP Engineer

@salva_bgDrupal.org: slv_ Freelance Drupal & Symfony.Security Audits.Devops.

Page 4: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Me

Page 5: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Nightwatch.js

● Introduction & Motivation● 4 Artifacts to rule them all● Nightwatch & Drupal

Page 6: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

A somewhat familiar Friday for every developer.

The watermelon effect“ “

Page 7: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Deploying to production (no tests)

Page 8: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,
Page 9: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

IntroductionWhat’s Nightwatch.js

Page 10: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

IntroductionWhat’s Nightwatch.js

Page 11: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Test Runner with parallel execution support.

JUnit-compliant XML reports.

assert-like and expect-like validations.

Hooks: before, beforeEach, after, afterEach.

Unit Testing support.

Main FeaturesThe bread and butter

Page 12: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

"src_folders" : [ "./tests/nightwatch/tests/"],"output_folder" : "{...}/reports","custom_commands_path" : "{...}/commands","custom_assertions_path" : "{...}/assertions","page_objects_path" : [ "{...}/pages/example"],"globals_path" : "{...}/global.js",

First Stop: Nightwatch.jsonPaths

Page 13: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

"selenium" : { "start_process" : true, "server_path" : "./tests/bin/selenium-server-standalone-3.13.0.jar", "log_path" : "", "port" : 34567, "cli_args" : { "webdriver.chrome.driver" : "./tests/bin/chromedriver" }},

First Stop: Nightwatch.jsonSelenium and Webdrivers

Page 14: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

"dev" : { "launch_url" : "http://127.0.0.1:8000", "selenium_port" : 9515, "selenium_host" : "127.0.0.1", "default_path_prefix" : "", "desiredCapabilities": { "browserName": "chrome" }}

First Stop: Nightwatch.jsonEnvironments

Page 15: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

var selenium = require('selenium-server');var chromedriver = require('chromedriver');

module.exports = (function(settings) { settings.selenium.server_path = selenium.path; settings.selenium.cli_args["webdriver.chrome.driver"] = chromedriver.path;

return settings;})(require('./nightwatch.json'));

Fully-customized setupNightwatch.conf.js

Page 16: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Main headerSubtitle

4 Artifacts to rule them all

Page 17: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Commands.

Page Objects.

Asserts.

Global Data.

Artifacts

Tests.

Page 18: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Many provided by default, like:

.closeWindow.

.setValue.

.saveScreenshot.

.click.

CommandsGeneric actions to perform across the site

Page 19: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Custom Commandscommands/clickWithMessage.js

exports.command = function (selector, message) { // Click and display a message for the action. this.click(selector, function() {

if (this.globals.test_settings.disable_colors === true) { console.log(' ✔ ' + message); } else { console.log('\033[92m ✔ \033[0m' + message); } });

return this;};

Page 20: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Martin Fowler.

Page objects are a classic example of encapsulation - they hide the details of the UI structure and widgetry from other components (the tests).

Page 21: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Page Objectspages/myPage.js

Page 22: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Page Objectspages/myPage.js

Page 23: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Page Objectspages/myPage.js

Page 24: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Page Objectspages/myPage.js

Page 25: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Assertionsassertions/myAssertion.js

exports.assertion = function(selector, comparedValue, msg) {

this.message = msg || util.format('Testing if value of <%s> does not equal: "%s".', selector, comparedValue); this.expected = comparedValue;

this.command = function(callback) {};

this.value = function(result) {};

this.pass = function(value) {};};

Page 26: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

BDD Expect Assertions

checkFieldAttribute: function(fieldName, attr, Value) {this.expect.element(fieldName).to.have.attribute(attr).equals(Value); return this;

},

Language Chains: to, be, been, is, that, which, and,

has, have, with, at, does, of.

Page 27: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Global Datadata/globals.js

user: { username: 'test3',

password: 'AcceptableUserPassword_-',},unexisting_user: {

username: 'nightwatch_' + Date.now(),firstname: 'NW_First',display_name: 'NW_First' + 'Surname' + Date.now(),email: 'nightwatch_' + Date.now() + '@.example.net'

},

Page 28: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Simple Test with & w/o Page Objects.

Demo 1

Page 29: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Remote testing via JenkinsDependencies on the remote Server

Page 30: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Scenario

Page 31: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

1

Scenario

Page 32: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

1

2

Scenario

Page 33: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

1

2

3

Scenario

Page 34: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Scenario

1

2

4

3

Page 35: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

CI Configuration

"ci" : { "launch_url" : "http://{http_auth_user}:{http_auth_pass}@127.0.0.1:8000", "selenium_port" : 9515, "selenium_host" : "127.0.0.1", "default_path_prefix" : "", "selenium" : { "start_process": false }, "desiredCapabilities": { "browserName": "chrome", "chromeOptions": { "args": ["--headless"] } }}

Page 36: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

LIVE DEMO:Graphical representation

Page 37: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Remote Testing via Jenkins

Demo 2

Page 38: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Drupal commands

Nightwatch & Drupal

Page 39: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

CreateRole, CreateUser, Login, Logout,

RelativeURL, UserIsLoggedIn.

CommandsAvailable in 8.6.0

Install, LoginAsAdmin, Uninstall.

Script-based

Web-based

Page 40: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Don’t need to go through any Nightwatch documentation.

Very simple and fast setup. No custom package.json needed:

yarn install

yarn test:nightwatch

Drupal SetupPros

Page 41: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Page Objects not supported yet.

Some use cases not covered (no multiple env setup).

Need to download some other npm dependencies that you

might not want for remote testing.

Still lots to do. Can’t easily enable modules, place blocks, etc.

Drupal SetupCons

Page 42: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Module developer: YES! or… yes?

For customer sites: I would not.

Instead: Use your own setup, and simply point its config

to the commands provided by core that are somewhat

usable outside of core testing.

CommandsSo, should I use Core commands for my tests?

Page 43: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Why Nightwatch?

Page 44: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Why Nightwatch?

Page 45: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Official Docs!

Quick install: https://github.com/salvamomo/nightwatch-starter.

Phéna Proxima on Testing.

Nightwatch after 12000 tests and 3000 hours.

ResourcesSome links, tools and interesting reads

Page 46: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

DevOps + Infrastructure

13/9/2018

TRACK SUPPORTED BY

Page 47: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Questions?

Page 48: Salva Molina - Drupal Europe · 2018-09-15 · Test Runner with parallel execution support. JUnit-compliant XML reports. assert-like and expect-like validations. Hooks: before, beforeEach,

Danke fürs Kommen

@salva_bg

salva.momo[at]gmail.com

www.adevfromtheplains.com