How to perform Visual Regression Testing in NightwatchJS
On This Page Ocular Regression Testing in NightwatchJSMay 13, 2026 · 8 min read · Testing Guide
is crucial to catch unexpected alteration in a website ’ s appearing after updates. Minor UI issues like layout shifts or color changes can harm user experience and brand trust. NightwatchJS makes it mere to automate these checks by capturing and comparing screenshots in your test workflow. NightwatchJS is a powerful end-to-end testing framework for web applications. It simplify writing automated tests and integrates well with visual comparison creature, making it easier to catch UI issues through visual regression screen within your existing test rooms. Best Practices for Optic Regression Testing with NightwatchJS This clause cover two methods to perform visual regression quiz in NightwatchJS: using the built-in Nightwatch tools and mix with Percy. Visual regression prove focuses on identifying unintended changes in a web application ’ s user interface by comparing screenshots taken at different degree of development. Unlike, which verifies if features work as expected, visual regression testing ensures that the looking and look of the coating stay logical across code updates. , a wide used framework, support visual regression testing by integrate with tool that capture and compare screenshots. This enables teams to efficiently automate optic differences catching as portion of their testing workflow. By using NightwatchJS, developers and testers can write maintainable that verify functionality and reminder UI constancy, reducing the hazard of ocular bugs slipping into production. Below are some key advantages and disadvantages to consider when performing visual regression testing habituate NightwatchJS. Advantages Disadvantages For teams look to simplify and raise optical regression testing in NightwatchJS, volunteer a seamless integration that automates screenshot comparison with powerful optic review workflows. Percy facilitate trim false positives, effortlessly manages baseline updates, and provides detailed visual diffs across multiple browsers and devices. tests can accelerate while better accuracy and collaboration across your team. Pre-Requisite:Set up Nightwatch JS Step 1: Install the nightwatch-visual-testing bundle The NightwatchJS doesn ’ t support Visual Testing out of the box, so you demand to instal the third-party package Use the below bidding to install the package. Step 2: Configure NightwatchJS for Visual Testing Open the nightwatch.conf.jsfile in your project, and add the configuration below. Look for the variable custom_commands_path in nightwatch.conf.js add an additional value to that. Look for the varying custom_assertions_path in nightwatch.conf.js add an additional value to that. Search for the nonremittal: {inside the test_settings: {cube and add the screenshot folder locations The doorway is the tolerance of pixels when comparing the two screenshots; the higher the value lower the sensitivity. Step 3: Create a Nightwatch Visual Test Create a fresh javascript file inside your craved folder (ex: tests/demoVRT.js) and write the uncomplicated codification to test. Here is an example of essay the BrowserStack home page at Example Code Snippet: Step 4: Execute the Test Execute Nightwatch Visual Validation trial expend the below command\ Syntax: For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users. Example: Note:The baseline screenshot is captured, when you execute the test the first clip, so to get an literal comparison examination needs to be executed at least 2 times. Step 5: View the event Once you execute the script the output will be shown in the command line terminal. If there is no difference in the screenshots, so the test will be marked as Pass. If there is any difference in the image, then the test fails The foldertests_output/visual-regressioncontains multiple images, the actual persona, the substructure image, and the difference between the literal and base image. The plugin also supports Optical Testing for a single HTML element with the commandcompareElementScreenshot (). Syntax: Example code Snippet: is a popular visual testing puppet that supports the visual testing of multiple browsers. It provides a dedicated build dashboard for each build, which aid the users view the test consequence online, and also provides functions such as Review, Accept and Reject the frame. Percy supports both Manual and Automated visual testing. Advantages of Percy Read More: A step-by-step guide to Performing Visual Regression testing Nightwatch and Percy Pre-Requisite:Set up Nightwatch JS Step 1: Instal Required Percy packages The Percy packages need to be installed to use the Percy commands in your projection Command to establish Percy parcel. The above command establish the two bundle@ percy/cli and @ percy/nightwatch. The @ percy/nightwatchprovides the impost commands for optic comparison and@ percy/cliprovides functionality to accomplish the test and upload to Percy infrastructure. Step 2: Configure Percy for Nightwatch Open the nightwatch.conf.js file and add the required statement Note:The above require statement should be before module.exports In your nightwatch.conf.js file search for custom_commands_path: and add the Percy command like below. Step 3: Create a simple Percy Nightwatch script In the above codification, we are voyage to the and taking the screenshot for visual comparison. The percySnapshot (& # 8216; Demo snapshot & # 8217;) command saves the screenshot with the name & # 8216; Demo snapshot & # 8217; Step 4: Copy the PERCY_TOKEN Step 5: Set the PERCY_TOKEN environment variable Based on the command-line tool you are using, you can set the environment variable as below. Step 5: Execute Percy Test Use the below command to action your Percy NightwatchJS script. Example: Step 6: View The Result Once the execution is completed, the command line tool provides the output with Percy make URL. You can easily navigate to the build using the build URL to see the optical validation result. If there are no changes, then Percy shows the message & # 8220; No Changes & # 8221; If there is a difference between the baseline screenshot and the actual screenshot when Percy shows the side-by-side difference Percy can also help you to capture and equate individual HTML elements. Instead of capturing the integral page you can capture the snapshot of the required element and perform optic testing. The scope option inpercySnapshot ()dictation can assist to capture the HTML element Example Code: In the above codification, the picker is specified in the reach option alternatively of capturing the integral page. .percySnapshot (& # 8216; name & # 8217;, {scope: & # 8216; # product-menu-toggle & # 8217;}) :The HTML factor which match the selector with id product-menu-toggle will be get and utilize for Visual Validation. Using Percy with NightwatchJS enhances visual fixation testing by automating screenshot comparisons and streamlining visual revaluation. To maximize the strength of this integration, consider the undermentioned best practices: Visual regression examine with NightwatchJS insure that web applications maintain a reproducible and true user interface through automated screenshot comparisons. When integrated with Percy, this summons becomes still more powerful, enabling reactive testing, multi-browser coverage, and streamlined visual reviews. By following best practice like targeted shot, sensitivity tuning, and frequent build analysis, squad can get UI bugs early, improve collaboration, and render a polished user experience with every release. # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.How to perform Visual Regression Testing in NightwatchJS
Overview
Visual Regression Testing in NightwatchJS
Advantages and Disadvantages of Visual Regression Testing in NightwatchJS
Performing Visual Regression Testing using nightwatch-visual-testing software
npm install nightwatch-visual-testing
custom_commands_path: ['./node_modules/nightwatch-visual-testing/commands '],
custom_assertions_path: ['./node_modules/nightwatch-visual-testing/assertions '],
globals: {visual_regression_settings: {outputDir: './tests_output ', threshold: 0.5},},//demoVRT.js describe ('Nightwatch Visual Test Demo ', function () {it ('Nightwatch Visual Test ', async function (browser) {expect browser .navigateTo ('https: //www.browserstack.com/ ') .assert.compareScreenshot ('First tryout ', 'Screenshot captured! ') .end ();});});npx nightwatch & lt; path_to_test_file & gt;
npx nightwatch tests/demoVRT.js
Visual Testing for Single HTML Element
compareElementScreenshot (' & lt; selector & gt; ', ' & lt; name ', 'message ')//demoVRT.js describe ('Nightwatch Visual Test Demo ', function () {it ('Nightwatch Visual Test ', async mapping (browser) {wait browser .navigateTo ('https: //www.browserstack.com/docs ') .assert.compareElementScreenshot (' # product-menu-toggle ', 'First test ', 'Screenshot trance! ') .end ();});});Performing Nightwatch Visual Regression Testing with Percy
npm install -- save-dev @ percy/cli @ percy/nightwatch
const Hotspur = require (' @ percy/nightwatch ');custom_commands_path: [percy.path],
//demoVRT.js describe ('Nightwatch Visual Test Demo ', function () {it ('Nightwatch Visual Test ', async function (browser) {await browser .navigateTo ('https: //www.browserstack.com/docs ') .percySnapshot ('Demo shot ') .end ();});});# Unix $ export PERCY_TOKEN= '' & lt; your-project-token & gt; '' # Windows $ set PERCY_TOKEN= '' & lt; your-project-token & gt; '' # Powershell $ $ Env: PERCY_TOKEN= '' & lt; your-project-token & gt; ''
npx percy exec -- nightwatch & lt; testscript_path & gt;
npx percy exec -- nightwatch tests/demoVRT.js
HTML Element Visual Testing apply Percy
//demoVRT.js describe ('Nightwatch Visual Test Demo ', function () {it ('Nightwatch Visual Test ', async function (browser) {await browser .navigateTo ('https: //www.browserstack.com ') .percySnapshot ('Single Element Snapshot ', {scope: ' # product-menu-toggle '}) .end ();});});Percy NightwatchJS Best Practices
Conclusion
Related Guides
Automate This With SUSA
Test Your App Autonomously