npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

protractor-screenshot-extension

v1.0.8

Published

An extension for Protractor that allows testing visual screenshots

Downloads

202

Readme

Protractor Screenshot Extension

A simple, light-weight extension for Protractor (Angular's integration testing tool) that allows you to test visual screenshots of your application or individual elements. This extension uses the pixelmatch npm module under the hood for image comparison. Furthermore, it is very easy to indicate sections of the screenshot to ignore during the image comparison that may be susceptible to change. You can do this by specifying an array of rectangular areas to ignore or an array of elements using Protractor's ElementFinder api.

Installation

npm install protractor-screenshot-extension --save-dev

Usage and Interfaces

In order to add this extension to your Protractor browser instance, you can add the following few lines to your protractor.conf.js file.

const { ProtractorScreenshotExtension } = require('protractor-screenshot-extension');

exports.config = {
   // your config here ...

    onPrepare: function() {
        browser.screenshotExtension = new ProtractorScreenshotExtension('path/to/save/screenshots');
    },
}

ProtractorScreenshotExtension should be initialized with a path to where you want your screenshots to be saved. Inside that directory, 3 subdirectories will be created - /baseline, /actual and /diff. Commit the /baseline images to your repo. /actual and /diff are only created on a failure - I recommend adding these paths to your .gitignore file.

There are only 2 functions exposed for use during your e2e tests:

  • checkElementScreenshot(element: ElementFinder, tag: string, options: IScreenshotOptions)
    • element - the element to take a screenshot of
    • tag - the name of the baseline image to check
    • options - see options below
  • checkPageScreenshot(tag: string, options: IScreenshotOptions)
    • tag - the name of the baseline image to check
    • options - see options below
interface IRectangle {
    x: number;
    y: number;
    w: number;
    h: number;
}

interface IScreenshotOptions {
    ignoreRectangles?: IRectangle[];
    ignoreElements?: ElementFinder[];
    threshold?: number;
    includeAA?: boolean;
}

Simple Example

Below is a simple example of testing a basic element screenshot:

    it('should compare screenshots of the test-angular-component', async () => {
        const diffPixels = await browser.screenshotExtension.checkElementScreenshot(
            element(by.css('test-angular-component')),
            'test-angular-component'
        );
        expect(diffPixels).toEqual(0);
    });

The first time you run your test suite, all screenshots will be saved in the /baseline folder. All subsequent executions will compare the live screenshot to this baseline.

Options (IScreenshotOptions)

There are several options available for tuning your test to make it more robust to changes

  • ignoreRectangles?: IgnoreRectangle[] - an array of objects that describe a rectangle to be ignored during the image comparison. These rectangles will appear blacked out in the baseline image that gets saved, so it is easily verifiable to the human eye.
  • ignoreElements?: ElementFinder[] - an array of elements that should be ignored during the image comparison. These elements will appear blacked out in the baseline image that gets saved, so it is easily verifiable to the human eye.
  • threshold?: number - Matching threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive. 0.1 by default. For more information see the pixelmatch docs.
  • includeAA?: boolean - If true, disables detecting and ignoring anti-aliased pixels. false by default. For more information see the pixelmatch docs.

Example using the ignore options

    it('should compare screenshots of the test-angular-component', async () => {
        const diffPixels = await browser.screenshotExtension.checkElementScreenshot(
            element(by.css('test-angular-component')),
            'test-angular-component',
            {
                ignoreElements: [ element(by.css('test-angular-component .subsection-to-ignore')) ]
            },
            {
                ignoreRectangles: [ { x: 100, y: 100, w: 200, h: 15 } ]
            }
        );
        expect(diffPixels).toEqual(0);
    });

Example output

This is an example of what image files get created. In this example only one ignoreElement was passed.

| expected | actual | diff | | --- | --- | --- | | | | |

Contribution

Be sure to first build the module and then execute npm run e2e at the toplevel of the repository if making any local modifications to run the test suite.

See CONTRIBUTING.md.