Nightwatch.js Makes Software Test Scripts Fun and Easy

Sauce AI for Test Authoring: Move from spirit to execution in minutes.|xBack to ResourcesBlogPosted

May 02, 2026 · 7 min read · Testing Guide

Sauce AI for Test Authoring: Move from spirit to execution in minutes.

|

x

Back to Resources

Blog

Posted February 15, 2018

Nightwatch.js Makes Software Test Scripts Fun and Easy

quote

Nightwatch.js is a Seleniumwrapper that provides several out-of-the-box commands and assertions for performing various operation on the page. The commands and affirmation are light and straightforward, enabling you to develop tests very quickly using Javascript (specifically,Node.js). It allow you to create custom helpers to increase your testing capabilities.

In this article, we ’ ll walk through developing a set of test scripts for front-end applications quickly and effortlessly using Nightwatch.

Getting Started with Nightwatch.js

This blog helps you to set up Nightwatch.js by understanding the required file and folder structure, the ins and outs of configuration files, and how to pen and run tests both locally and in a cloud-based environment such asSauce Labs.

It starts by clone the nightwatch-skeleton GitHub repo and completing the requirement below:

Step

Action

1

Unfastened Terminal

2

Make Directory

$ mkdir ~/work/github

3

Clone Nightwatch-Skeleton

$ git clone git @ github.com: gsypolt/nighwatch-skeleton.git

4

Download Node.js (version 6.5 or after)

5

Change Directory

$ cd ~/work/github/nightwatch-skeleton

6

Install Nightwatch-Skeleton Node Package Dependencies

$ npm install

How Is Nightwatch Skeleton Going to Help?

Before getting into the code and learning how to develop tests using Nightwatch.js, it & # x27; s worth understanding the nightwatch-skeleton brochure structure and contour. In this post, we ’ re going to plunge into some very specific configurations and custom commands, but most significantly, this situation will manifest how the nightwatch-skeleton allows testers to come up to rush faster with less worry about the configuration and the standard burden.

sypolt feb image9
  • The custom-commandsfolder contains a list of custom helpers to increase your testing potentiality and reusability.

  • nightwatch.jsonis a configuration file which tells the Nightwatch tool where to happen the stand-alone Selenium binary, browser drivers, location of the examination, environment profile, and more.

  • The package.jsoncomprise the metadata and node package colony for the project

    For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

  • The ci/functional-test.Jenkinsfilefile is a templet to facilitate quickly add your functional tests to the Jenkins pipeline.

To reduce copying and glue the cube of code at the end of each test script, the tradition helper eliminates the need to duplicate code within all of the test scripts. Code duplicate is a frightening coding practice because it ’ s a maintenance incubus (and that ’ s just the start). Reference the custom_command directory inside the Nightwatch frame to locate more custom helpers to use. (We use them every day.)

sypolt feb image3

The configuration is the heart and somebody of a Nightwatch test undertaking. Setting up any test automation project can be tricky and perplexing, with weak documentation. Let & # x27; s take a closer look at the configuration file and highlight some important lines of codification, looking atnightwatch.json below.

sypolt feb image8
  • LINE 1Where the tests are located

  • LINE 3The location of the custom commands that will be loaded

  • LINES 10-12The test_workers set to true allows for the run of individual tests in latitude, and for setting the number of proletarian to auto or a number.WARNING: If the routine of workers is set high than the number of CPUs where the tests are running in parallel, you will experience random test failure.

sypolt feb image5
  • LINE 2set to true, manage the Selenium operation for local execution

  • LINE 3is the location of the stand-alone Selenium binary. This requires that the start_process = true.

  • LINES 7-10- A list of CLI argument passed to the Selenium process (where to set browser drivers for local execution).

sypolt feb image2
  • The defaulttest_settingswill be passed to the Nightwatch case, but environment profile scope could overwrite specific defaulttest_settingsduring runtime.

  • LINES 16-20The desiredCapabilities object will be legislate to the Selenium WebDriver when a new session is created. I opt to continue the desired capabilities angle. Our touchstone is to set the browser gens as part of the environment profile. (I will go into more detail in the succeeding codification subdivision.) The name tag javascriptEnabled and assertSslCerts are the defaults, and everything else is set inside the environment profile.

  • LINE 29Here, we set stand-alone Selenium as mistaken and control this property from the environs profile. We default to executing all tests on Sauce Labs over running them locally.

sypolt feb image10
  • The mobilesauce-androidprofile incorporate the scope that either add or override the settings in the nonremittal profile. In this example, the desired capabilities and global variables for the mobile web environment are added. (Please note that Appium is habituate in a remote environment to found the virtual machine on Sauce Labs for nomadic web quiz.)

sypolt feb image11
  • The local-chromeprofile is necessary while developing trial. The developer can leverage the decreased system latency to quicken testing. The settings allow the Selenium waiter to run topically and use local resources (browser, platforms and associated driver) to execute the test.

To build your own test profiles for Sauce Labs, go to Sauce Labs ’ to find the capabilities needed for Sauce Labs. You can chance all the above codification snippets at:gist.github.com/gsypolt/nightwatch-code-snippets or https: //github.com/gsypolt/nighwatch-skeleton.

Learn How to Write Test Scripts

The nightwatch-skeleton comes with sample tests developed with our standard guidelines. The good place to get started is getting familiar with theNightwatch API Reference Document. Here are some of our best practice for indite test handwriting (not all of them):

Before and After test hooks - LINE 5 and LINE 14.

sypolt feb image4


Tags - Test can experience multiple tag to radical the exam (keep them meaningful) - LINE 6.

sypolt feb image1


In general, most of the tests do a user activeness (which is commonly a .click action). At least one .assert statement should be included - LINE 14.

sypolt feb image12


Include GIVEN WHEN THEN comment statements to explain what the test does and its readability - LINES 13, 15, and 17.

sypolt feb image6


The perform blocks are used to synchronize the test steps, as Nightwatch.js is asynchronous in nature. (We use the perform blocks over explicit waits) - LINES 15 and 19.

sypolt feb image7

Running Tests

Now you & # x27; re ready to run Nightwatch.js tests. The cloned GitHub repo already has a few sampling tests ready to execute. The like test files can be executed locally and remotely through Sauce Labs as long as you feature credentials. The current configuration automatically start the Selenium host and launches the desired browser based on the choose environment. Respective environment profiles are available by nonpayment in the nightwatch.json file. Here are the commands to run the tryout for a specific surround defined in the nightwatch.json file:

This dictation runs a specific test in the test directory:
$ cd ~/work/github/nightwatch-skeleton
$ nightwatch /test_path/test_dir/ * .js

This bid runs the test in a specific environment profile available:

$ nightwatch -- e & lt; env-name & gt; /test_path/test_dir/ * .js

This command go all the tests under the specific tag-name:

$ nightwatch -- e & lt; env-name & gt; -- label & lt; tag-name & gt;

There are a few different options for how to use the test moon-curser, depending on your installation character or strategy. (Reference the Nightwatch run tests documentation.)

Conclusion

So far, you have learned how to get begin by cloning my nightwatch-skeleton and downloading and installing project dependencies, and you ’ ve learned additional information about how to configure and tune Nightwatch.js. I alone briefly covered how to write tests, but with these steps, you get all the necessary tool to make examination and run them topically or remotely.

For that, you have to consider some aspects, but please note that there are no silver smoke here. Depending on your business need, you may answer the following questions differently:

  • Where should I run tests? On staging? On product?

  • When do I build my exam pipeline?

  • What are the tryout scenarios I desire to try?

  • When and who should write end-to-end tests?

In closing, a lot of the credit for the nightwatch-skeleton implementation came from my amazing Quality Engineering team at Gannett | USA Today Network, with me spearheading the design.

Greg Sypolt (@ gregsypolt) is Director of Quality Engineering at Gannett | USA Today Network, a Fixate IO Contributor, and co-founder of Quality Element. He is responsible for test mechanization solutions, test reportage (from unit to end-to-end), and uninterrupted integrating across all Gannett | USA Today Network products, and has help change the testing coming from manual to automated examine across various products at Gannett | USA Today Network. To determine improvements and testing opening, he conducted a face-to-face interview sketch summons to understand all product growth and deployment processes, testing strategies, tooling, and interactive in-house training programs.

Published:
Feb 15, 2018
Share this post
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rights earmark. SAUCE and SAUCE LABS are register trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdiction.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free