Code Monkey home page Code Monkey logo

nightwatch-vrt's Introduction

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
        }
    }
}
Property Description Defaults
generate_screenshot_path Passed function that will generate a screenshot path none
latest_screenshots_path Path to the most recently captured screenshots "vrt/latest"
latest_suffix A string appended to the end of the latest captured screenshot* ""
baseline_screenshots_path Path to the baseline expected screenshots "vrt/baseline"
baseline_suffix A string appended to the end of the baseline screenshot* ""
diff_screenshots_path Path to the diff image of the two screenshots "vrt/diff"
diff_suffix A string appended to the end of the diff image* ""
threshold Matching threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive. 0.0
prompt If true, the user will be prompted to override baseline screenshot when the recently captured screenshot differs false
always_save_diff_screenshot If true, recently captured screenshots will always override the baseline false
* 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.

Parameter Description
nightwatchClient The nightwatch client test instance
basePath The base path for the screenshot set in visual_regression_settings (e.g. *_screenshots_path)
fileName The file name; either the selector used or the custom name given for the test
returns A 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.

Parameter Description
selector Identifies the element that will be captured in the screenshot.
fileName Optional file name for this screenshot; defaults to the selector
settings Optional settings to override the defaults and visual_regression_settings
message Optional message for nightwatch to log upon completion
module.exports = {
    'Test Google UI loads correctly': (browser) => {
        browser
            .url('https://www.google.co.uk')
            .assert.screenshotIdenticalToBaseline('body',  /* 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.

nightwatch-vrt's People

Contributors

avengerpenguin avatar bbc-search-and-navigation avatar carwynnelson avatar davebbc avatar gregoryduckworth avatar renovate-bot avatar sframpton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nightwatch-vrt's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>bbc/cd-renovate)

xml2js is vulnerable to prototype pollution

Just echoing the Dependabot warning that we had on our repo:

Dependabot cannot update xml2js to a non-vulnerable version
The latest possible version that can be installed is 0.4.23 because of the following conflicting dependencies:

@bbc/[email protected] requires xml2js@^0.4.5 via a transitive dependency on [email protected]
No patched version available for xml2js
The earliest fixed version is 0.5.0.

xml2js versions before 0.5.0 allows an external attacker to edit or add new properties to an object. This is possible because the application does not properly validate incoming JSON keys, thus allowing the proto property to be edited.

Temporary solution

Add the following to your package.json

"overrides": {
  "xml2js": "0.5.0"
}

Moving nightwatch-vrt to the Nightwatch Community org

Hello,

We have recently created a Nightwatch Community Github organisation where we will host various Nightwatch related independent projects with the purpose of bringing the community together.

We are seeing significant interest from users in having the nightwatch-vrt plugin upgraded to Nightwatch 2, which also brings a new plugin api.

We would be happy to see it under the Nightwatch Community organisation if the maintainers would consider that.

Thanks!

Directories "vrt/latest" and "vrt/diff" weren't created

Started the test:
buildPlanPage.verify.screenshotIdenticalToBaseline( "@canvas", "CanvasBuildPlan", { threshold: 0.01 }, "VRT Build Plan Canvas verified" );
And directory with biseline was created
Diff and latest - not
The same behaviour in case failed or success test(screenshot is the same or not)

I have following packages
{ "name": "tests", "version": "2.0.0", "description": "Automation tests for AMP Manager", "main": "nightwatch.conf.js", "devDependencies": { "@nightwatch/selenium-server": "^4.1.2", "allure-commandline": "^2.17.2", "axios": "^0.24.0", "child_process": "^1.0.2", "chromedriver": "^107.0.2", "find-root": "^1.1.0", "form-data": "^4.0.0", "lodash": "^4.17.21", "nightwatch": "^2.3.4", "nightwatch-allure": "^1.2.0", "node-fetch": "^3.2.3", "node-fetch-with-proxy": "^0.1.6", "prettier": "^2.4.1", "robotjs": "^0.6.0", "selenium-appium": "^1.0.2", "selenium-webdriver": "^4.1.2", "@bbc/nightwatch-vrt": "^2.0.0" }, "scripts": { "test": "set NODE_OPTIONS=--insecure-http-parser=true&&node node_modules/nightwatch/bin/runner.js --config ./nightwatch.conf.js", }, "author": "", "license": "ISC" }
nightwatch.conf.js
globals: { asyncHookTimeout: 600000, abortOnAssertionFailure: false, "visual_regression_settings": { "generate_screenshot_path": generateScreenshotFilePath, "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 },

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.