UI testing in JS/TS, Part 2: Nightwatch.js

Nightwatch is a UI automation test tool that supports Chrome, Edge, Firefox and Safari browsers. It was released in January 2014, has 11.3k stars on GitHub, and is fully open sourced and maintained by BrowserStack.

Nightwatch uses the WebDriver protocol to perform the browser automation tasks, and can be used with self hosted Selenium grid instances, and can run tests in parallel.

In addition to desktop E2E tests, Nightwatch also supports component testing for Vite, React and Vue with additional frameworks planned, integration testing with REST API’s via an additional plugin, and mobile device testing through Appium:
https://nightwatchjs.org/guide/writing-tests/api-testing.html
https://nightwatchjs.org/guide/mobile-web-testing/with-appium.html

Like all good test frameworks, Nightwatch can capture screenshots (on demand and on failure), but is not able to record video directly.

Getting Started

Nightwatch is installable using Node package manager such as npm or Yarn and interactively sets up a test project.

The sample test project can then be opened in the IDE of your choice and you can start to create page objects and build out your tests.

See the following guide for step by step instructions:
https://nightwatchjs.org/guide/quickstarts/create-and-run-a-nightwatch-test.html

Developing a framework

Nightwatch includes a built in assertion libraryhttps://nightwatchjs.org/guide/writing-tests/adding-assertions.html) and supports page objects for maintainability and reusability (https://nightwatchjs.org/guide/concepts/page-object-model.html).

The tests can then be executed locally on the supported browsers (Chrome, Edge, Firefox, Safari) and additionally on cloud providers:

https://docs.saucelabs.com/visual-testing/integrations/nightwatch/

https://www.browserstack.com/docs/automate/selenium/getting-started/nodejs/nightwatch

https://www.lambdatest.com/selenium-automation-testing-with-nightwatchjs-framework

Nightwatch.js Typescript sample

Clone the repository from https://github.com/ObjectiveTester/AllThingsTesting.com-examples.git and start a terminal in the ‘NightwatchTS’ directory and first run:

npm install

and then either:

npm run test to execute the tests in headed mode

npm run junit to execute the tests and produce junit output for a CI platform

Summary

Nightwatch is a very capable framework supporting component, integration, E2E testing and mobile devices through Appium, however, it doesn’t have a UI component or the ability to record videos to aid in debugging.
The tests can be easily integrated into a CI platform and it has support for a range of reporting frameworks.
Although not as popular and well supported as some other frameworks, Nightwatch would be an excellent choice for development and test teams building and modern web and mobile applications.
A notable alternative JS/TS frameworks using WebDriver is WebdriverIO.

Leave a Reply

Your email address will not be published. Required fields are marked *