Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @bbc/nightwatch-vrt with all npm packages installed. Try it out:

@bbc/nightwatch-vrt lists no main file and has no index.js, so it can't be directly required. If this is a mistake, please let us know. It may however contain internal files that you can require manually:

// require("@bbc/nightwatch-vrt/[??]")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@bbc/nightwatch-vrt v1.1.2

Nightwatch Visual Regression Testing tools

Nightwatch VRT

Nightwatch Visual Regression Testing tools for nightwatch.js

Description

Nightwatch VRT extends nightwatch.js with an assertion that captures a screenshot of a DOM element identified by a selector and compares the screenshot against a baseline screenshot. If the baseline screenshot does not exist, it will be created the first time you run the test and the assertion will pass.

Configuration

Include the following sections in the nightwatch configuration file

Custom commands and assertions

Register nightwatch-vrt's assertion and commands:

    custom_commands_path: [
        'node_modules/@bbc/nightwatch-vrt/commands'
    ],
    custom_assertions_path: [
        'node_modules/@bbc/nightwatch-vrt/assertions'
    ]

Nightwatch VRT custom settings

Then, for global settings, add the visual_regression_settings entry to nightwatch's globals globals section

default: {
    "globals": {
        "visual_regression_settings": {
            "generate_screenshot_path": defaultScreenshotPathGenerator,
            "latest_screenshots_path": "vrt/latest",
            "latest_suffix": "",
            "baseline_screenshots_path": "vrt/baseline",
            "baseline_suffix": "",
            "diff_screenshots_path": "vrt/diff",
            "diff_suffix": "",
            "threshold": 0,
            "prompt": false,
            "always_save_diff_screenshot": false
        }
    }
}
PropertyDescriptionDefaults
generate_screenshot_pathPassed function that will generate a screenshot pathnone
latest_screenshots_pathPath to the most recently captured screenshots"vrt/latest"
latest_suffixA string appended to the end of the latest captured screenshot*""
baseline_screenshots_pathPath to the baseline expected screenshots"vrt/baseline"
baseline_suffixA string appended to the end of the baseline screenshot*""
diff_screenshots_pathPath to the diff image of the two screenshots"vrt/diff"
diff_suffixA string appended to the end of the diff image*""
thresholdMatching threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive.0.0
promptIf true, the user will be prompted to override baseline screenshot when the recently captured screenshot differsfalse
always_save_diff_screenshotIf true, recently captured screenshots will always override the baselinefalse

* Only necessary if screenshots are set to reside in the same directory

Nightwatch VRT screenshot path generator

The screenshot path generator option accepts a function that generates a dynamic path based on the test properties, and returns that string.

ParameterDescription
nightwatchClientThe nightwatch client test instance
basePathThe base path for the screenshot set in visual_regression_settings (e.g. *_screenshots_path)
fileNameThe file name; either the selector used or the custom name given for the test
returnsA string which contains the full path - minus the file extension

For example:

function generateScreenshotFilePath(nightwatchClient, basePath, fileName) {
    const moduleName = nightwatchClient.currentTest.module,
        testName = nightwatchClient.currentTest.name

    return path.join(process.cwd(), basePath, moduleName, testName, fileName)
}

Usage

In order to use nightwatch-vrt, you only need to invoke the screenshotIdenticalToBaseline assertion and pass a css selector for the DOM element to compare. You may also pass a custom filename, visual_regression_settings overrides, and a custom log message.

ParameterDescription
selectorIdentifies the element that will be captured in the screenshot.
fileNameOptional file name for this screenshot; defaults to the selector
settingsOptional settings to override the defaults and visual_regression_settings
messageOptional message for nightwatch to log upon completion
module.exports = {
    'Test crunch.io main content is correct': (browser) => {
        browser
            .url('https://crunch.io')
            .assert.screenshotIdenticalToBaseline('.body.entry-content',  /* Optional */ 'custom-name', {threshold: 0.5}, 'VRT custom-name complete.')
            .end()
    }
}

The first time a test is run, a baseline screenshot will be created and stored on disk. You should always register the baseline screenshot in the code repository. Further executions of this test will compare against this baseline.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free