Rykus0 / nightwatch-visual-regression

Custom nightwatch assertion and commands for capturing, cropping, and comparing screenshots
MIT License
16 stars 1 forks source link

Visual Regression Testing for Nightwatch

This is a NightwatchJS custom assertion and commands for capturing and comparing screenshots during testing.

Node dependencies are listed in package.json.

This project uses the gm module, and so requires that graphicsmagick or imagemagick are installed and configured on your system.

Description

The first time the assertion is run, a baseline image is saved.

Subsequent runs will compare to these baseline images. If the mismatch percentage is more than the given threshold, the assertion fails.

When the assertion fails, the visual diff is copied to a separate folder.

Generated folders and images are organized in this way: testPath/testName/browser_version_os/widthxheight/testLabel__selector--label.png

Installation

Configuration

Copy the contenst of globals.js into the file specified by globals_path in nightwatch.json

See globals.js for configuration details.

Usage

module.exports = {
    'My Test':  function(client){
        var page = client.page['myPage']();

        page.navigate();

        // Screenshot cropped to default selector
        client.assert.visualRegression();

        // Screenshot cropped to contents of given selector
        client.assert.visualRegression('.my-component');

        // Use page selector
        page.assert.visualRegression('@myForm');

        page.click('@button');

        // Add a custom label to discern identical screenshots
        page.assert.visualRegression('@myForm', 'afterButtonPress')

        // Hide Sensitive/Dynamic content from screenshot
        page.hide('@username', function(){
            client.assert.visualRegression();
            form.show('@username');
        });
    }
};