Nightwatch.js in Drupal 8: automated JavaScript testing

There is never too much discussion of Drupal and JavaScript frameworks.
We have taken a glimpse at Drupal 8 and Vue.js combination and know that Vue.js is a candidate for Drupal core. Today is the time to review Nightwatch.js — an automated testing framework that is already part of Drupal 8. We will see how Nightwatch.js in Drupal 8 provides for automated JavaScript testing.
A look at Nightwatch.js and its benefits
Nightwatch.js is an automated testing and continuous integration framework, meant to streamline and simplify these processes. It is an integrated end-to-end (E2E) browser testing solution. Nightwatch is also suitable for Node.js unit tests.
Nightwatch.js is written on Node.js — a cross-platform JavaScript runtime environment. To execute commands and assertions on DOM elements, Nightwatch relies on the W3C WebDriver API (formerly known as Selenium WebDriver). For identifying elements, it uses JavaScript language (Node.js) and CSS or XPath selectors.

Currently, Nightwatch.js has 148,900+ weekly NPM downloads thanks to numerous benefits:
-
ease of use due to clear syntax and detailed documentation
-
ability to perform custom commands and assertions
-
command-line test runner Nightwatch Runner included
-
its own NightCloud.io cloud testing platform
-
compatibility with other testing platforms (SauceLabs, BrowserStack etc.)
-
real-time tests possible in the browser
-
parallel test execution possible
-
screenshots and videos of tests
-
automatic management of Selenium or WebDriver services
-
support for Page Object Model that ensures better organization of elements
-
ability to integrate with CI systems (Jenkins, Hudson, Teamcity etc.) thanks to JUnit XML
Nightwatch.js in Drupal 8
Previously, JavaScript functionality on Drupal sites had often been tested via PHPUnit. That often required front-end developers to study the ins and outs of PHP programming language and PHPUnit testing framework. Generally, it was cumbersome and error-prone to test JavaScript with PHP.
Things changed when the Drupal community decided to officially use Nightwatch.js for browser testing. Inspired by the JavaScript Modernization Initiative, in 2018, Nightwatch was included into the Drupal 8.6 core.

Nightwatch.js makes it possible to test JavaScript with JavaScript. Developers can write custom tests for browser interactions directly in JS, and execute them in different browsers.
The vast community of JavaScript developers now have their own browser testing tool in the Drupal core. In addition, Nightwatch.js overcomes some PHPUnit limitations.
Nightwatch.js in Drupal 8 offers a built-in Chromedriver. It can work on a standalone basis and requires no Selenium or Java installation. However, it is possible to use other browsers via Selenium — Firefox, Internet Explorer, Safari, and more.
To work with Nightwatch.js, we will need three basic dependencies: Node.js, Yarn, and Chrome. Thanks to detailed documentation, it is easy to create and run tests for Drupal websites, modules, and themes. Helpful links include:
-
Nightwatch.js install instructions on GitHub
-
API reference on Nightwatch.js website
-
guide to writing custom assertions and commands (function-style commands and class-style commands)
-
guide to running tests with the Nightwatch Runner
-
Drupal.org guide to writing JavaScript tests in Nightwatch.js
-
Nightwatch.js tests in Drupal 8.6 core (exampleTest.js, loginTest.js, and statesTest.js)
-
Nightwatch.js commands for Drupal (DrupalInstall, DrupalUninstall, DrupalCreateUser, DrupalCreateRole, DrupalLogAndEnd, DrupalLoginAsAdmin etc.)
and more!
Get assistance with Nightwatch.js
Our development team follows the latest JavaScript trends. They implement them in Drupal projects and give speeches on them at conferences. One of our Drupal developers even took part in fixing the JS coding standards for Nightwatch.js tests in Drupal 8.6 core.
So you can trust our Drupal team if you need any assistance with Nightwatch.js in Drupal 8. We can install and configure the framework, create and run Nightwatch.js tests according to your requirements, help you integrate Nightwatch with your CI systems (Jenkins, Hudson, Teamcity), and so on.
Rely on us for any other small or big tasks that involve Drupal and JS frameworks!