Welcome to FullStack. We use cookies to enable better features on our website. Cookies help us tailor content to your interests and locations and provide other benefits on the site. For more information, please see our Cookies Policy and Privacy Policy.
E2E Testing validates both your software and its functionality along with external integrations and interfaces in a production-like environment. This article will guide you through the installation, configuration, and implementation of the Nightwatch framework so you can create a simple E2E test suite.
About Nightwatch.js
This open-source automated testing framework will simplify the integration process. It’s built in Node.js, allowing you to write tests even if you don’t have experience –– and if you have written some js code, it’ll be a piece of cake!
Nightwatch uses W3C WebDriver API (Selenium Web driver) to control DOM elements and browser interfaces to make testing as smooth as possible.
Main features
Clean Syntax: Effortless test coding with JavaScript (Node.js) and the framework features, using CSS as well as XPath selectors.
Built-in Test Runner: Supports sequential or parallel testing in grouped and tagged test suites.
WebDrivers Management: Automatic control over Selenium, ChromeDriver, and other WebDrivers in a separate child process to improve performance.
Continuous Integration: Can be used with multiple continuous build processes like Jenkins, Teamcity, etc. by using JUnit XML reporting.
Cloud Features: Cloud-based cross-browser testing with NightCloud.io, SauceLabs, and BrowserStack.
Multiple DOM Selectors Support: You can select different elements using CSS and XPath selectors, allowing you to run Nightwatch commands over the DOM.
Installation
Before getting started with Nightwatch testing, we will need to set up our environment with NPM & Node.js along with the desired web driver; you can find how to set them up at Node.js.
NPM and Node: You can check if already installed by running npm -v and node -v respectively.
WebDriver: The main supported browsers are (Chrome, Firefox, IE, and Safari), You can install and configure them all in the nightwatch.con.js file; in this tutorial, we’ll use chromedriver, which is installed by running npm install chromedriver --save.
Now that you’re in the node folder project, install your dependencies: run npm install nightwatch --save-dev and npm install chromedriver --save –– once installed, change your test script to nightwatch so your package.json looks like this:
We’re almost done, but before we start writing some E2E tests, we need to set up Nightwatch and the test folders.
You can create your configuration file two different ways. One is by letting the framework do all the work by running npm run test; this will create a huge file with all possible configurations.
But we’ll go with (for me) the easiest way: create a nightwatch.conf.js file with just the essentials – your test folder, a webdriver path, the default settings, and a screenshot-on-failure feature:
If you’re used to writing tests with other frameworks, Nightwatch testing will seem a little weird because features are called by chaining them (similar to how js async calls with CSS and XPath selectors).
Additionally, remember that each file in the tests folder is a suite that must use the node module.exports; it can have as many steps or validations as you want (in the form of functions as shown below):
We need to call the browser for every test and then chain all validations, or we can separate the chained calls. Here, we checked the text of some element on the page; since the Facebook signature image didn’t have text, we assert the alt attribute (remember, you can assert any property with the domPropertyEquals).
Testing interactions
Another great E2E testing tool that Nightwatch offers is interactions; this allows us to click, press keys, set values, check if it’s displaying the correct data, main elements are loading, or if it's redirecting properly.
There’s a lot more features that I won’t be able to include in this post, but you can check the whole suite Nightwatch API offers; just plug it in your testing chain.
Nightwatch Hooks
Last but not least, hooks are one of my favorites tools to create testing suites. It makes the whole process smoother, you can use them for basic applications like sending messages, logging, and even some asynchronous tasks. You can also use them to take the interaction even deeper; for example, you can use data to log in, check data keys plus other main elements, and log out afterward –– isn’t that great?
I can’t stress enough how important it is to test your software from top to bottom. E2E testing is a great way to do it, and Nightwatch.js will take it to another level. Now that you know the basics, give it a try!
If you're looking to strengthen your testing strategy or need help setting up reliable E2E testing, FullStack is here to help. Ready to see how we can support your development and testing goals? Let's connect!
What is Nightwatch.js, and why use it for E2E testing?
Nightwatch.js is an open-source automated testing framework built on Node.js. It uses the W3C WebDriver API to control browsers, making it easy to test both UI interactions and external integrations in a production-like environment. It’s beginner-friendly but powerful enough for advanced testing scenarios.
What are the key features of Nightwatch?
Nightwatch offers:
A clean JavaScript-based syntax for writing tests
A built-in test runner for sequential or parallel suites
Automatic WebDriver management for browsers like Chrome and Firefox
Support for continuous integration tools such as Jenkins and TeamCity
Cross-browser testing with cloud platforms like SauceLabs and BrowserStack
How do you set up Nightwatch for an E2E test suite?
Start by installing Node.js, NPM, and the required dependencies. Configure Nightwatch with a simple configuration file where you define your test folder, WebDriver path, desired browser, and optional features like screenshot capture. Once set up, you can organize your tests into suites within the designated test folder.
How do Nightwatch tests work?
Nightwatch uses a chainable command structure for testing. Each test calls the browser instance and sequences actions like navigating to a URL, interacting with elements, asserting content, and verifying behavior. You can test functionality such as form inputs, button clicks, redirects, and dynamic content.
What are Nightwatch hooks, and how do they improve testing?
Hooks let you execute tasks before or after test runs, making test suites more efficient and manageable. Common uses include cleaning up test environments, logging activity, performing authentication, or automating repetitive setup and teardown steps. They help streamline workflows and reduce manual overhead.
AI is changing software development.
The Engineer's AI-Enabled Development Handbook is your guide to incorporating AI into development processes for smoother, faster, and smarter development.
Enjoyed the article? Get new content delivered to your inbox.
Subscribe below and stay updated with the latest developer guides and industry insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.