@porsche-design-system/visual-regression-tester v4.5.0
Porsche Design System - Visual Regression Tester
Export functions to create visual regression tests.
Using the Visual Regression Tester
Installation
Run the following command using npm:
npm install @porsche-design-system/visual-regression-tester --save-devIf you prefer Yarn, use the following command instead:
yarn add @porsche-design-system/visual-regression-tester --devUsage
It's highly recommended executing the visual regression tester within a Docker container to get reliable tests results across any operating system and machine.
Check out the Basic integration example for an example how to get the Visual Regression Tester up and running.
API
Constructor
The constructor expects two parameters:
browser: Browseroptions: VisualRegressionTestOptions(optional)
Browser should be a Puppeteer Browser instance. Check the basic integration example for how to create a Puppeteer browser.
test() Method
In the actual visual regression test you have to call
the test(snapshotId: string, scenario: Function, options: TestOptions = {elementSelector: '', maskSelectors: [], regressionSuffix: ''})
-method in your expect block, taken a unique name of the reference shot as first parameter (snapshotId: string).
As second parameter (scenario: Function) within the scenario callback function you call the goTo() method with
the extended URL (will be concatinated with the baseURL), as well as click(), hover(), focus() and type() if
necessary and prepare the state to compare.
goTo(), click(), hover(), focus() and type() method accept following optional
parameters networkIdleTimeout: number and maxInflightRequests: number which means, — consider loading has finished
when there are no more than maxInflightRequests network connections for at least networkIdleTimeout ms.
As a third and optional parameter (options: TestOptions) you can pass following options:
elementSelector: string = ''- pass a css selector for the element (selector is allowed to match exactly one element only) that should be included in your visual regression test.maskSelectors: string[] = []- pass a string array which includes css selectors for the elements that should be ignored in your visual regression test. IfmaskSelectorsis used in combination withelementSelectorthen those two selectors are concatenated automatically to match elements nested inelementSelector.regressionSuffix: string = ''- pass a string to add a suffix in regression filenames
To make use of Puppeteers Page instance within the scenario: Function you call the getPage() method and apply any
supported Puppeteer method like click(), hover() or type().
VisualRegressionTestOptions
viewportsselects the viewports of your browserdeviceScaleFactorspecify device scale factor (can be thought of as dpr)fixturesDirdirectory where the reference-shots will be savedresultsDirdirectory where the diffing- and the regression-shots will be savedtolerancegives the tolerance range for your visual regression diffsbaseUrlthe base URL of the page you would like to testtimeoutimpacts the timeout limit of page loadmodedefines the method with which the height for snapshot is determined
Note: All the VisualRegressionTestOptions are optional, those are the default options:
viewports: [320, 480, 760, 1000, 1300, 1760]
deviceScaleFactor: 1
fixturesDir: 'vrt/fixtures'
resultsDir: 'vrt/results'
tolerance: 0
baseUrl: 'http://localhost'
timeout: 30000
mode: 'auto'Notes:
- Add your
resultsDirfolder to.gitignorein order to not spam git history.
What to do when tests are failing
- Switch to your
resultsDirdirectory. Here you can find the belongingdiffandregressionimages. - Check if you would like to accept the changes
- If yes: Replace the reference shot in the
fixturesDirfolder with the belonging one in theresultsDirfolder and delete the images in theresultsDirdirectory afterwards manually. - If no: Recheck your code and run the tests again, when you think you fixed it.
- If yes: Replace the reference shot in the
License
- MIT