Visual regression testing - SAP/fundamental-ngx GitHub Wiki
All components that are covered by regression testing have visual regression testing in place along with functional tests. Visual regression works by taking a screenshot of the desired element and comparing it to the baseline screenshot. This comparison automatically checks for a specified percent difference of any changes including:
- positioning
- colors theme
- applied states (hover, focused or selected states)
note: there is no way to skip color or other style/theme checks because the visual regression compares the whole baseline image to the whole new image and returns a percentage value for the difference between the two.
Approved screenshots, which are used as expected component/element states, are called baseline screenshot.
All baseline screenshots are stored in the e2e/wdio/baselineScreenshots
folder of the repository.
When the test executes for the first time, baseline screenshots are generated automatically and saved to the e2e/wdio/baselineScreenshots/platform or core/<component_name>/<browser_name>
folder.
For all following executions, new baseline screenshots will not be generated if the baseline screenshot with the same name already exists in this folder. Instead, a comparison of the new screenshot vs the baseline screenshot will be performed.
In case the tests fail, two screenshots will be generated, for each comparison action:
- actual screenshot
- difference screenshot
These two types of screenshots will be saved in the.tmp/actual/<browser_name>
and.tmp/diff/<browser_name>
folders respectively.
The ‘actual screenshot’ will show you the current image of the element as it is now. The ‘difference screenshot’ will show the actual vs baseline image, highlighting the differences in pink.
All baseline screenshots that are generated for new components need to be committed to the repository along with the e2e test case code.
Component/element visual regression baseline screenshots are created for each browser + OS combination. For example if one component is under visual regression and tests are executed for Chrome and FF browsers for Win and MacOs, four separate screenshots will be generated and need to be committed along with the test case code.
Visual regression test cases indicate the mismatch of the baseline screenshot and actual screenshot (in percentage). If the baseline screenshot and actual screenshot don’t match, the particular test case will fail with an error of mismatch percentage.
In case the visual regression test case is failing:
- The test case needs to be re-executed from the local environment on the Saucelabs side on the env on which test case failed.
- The difference screenshots need to be evaluated by the PR owner to determine if the difference is expected or not. In the
.tmp/diff
folder you will be able to find the screenshot with mismatch highlighted in purple color.
In case the PR introduces unexpected changes, it needs to be fixed without merging any unexpected change to the main branch.
In case the change to the component is expected, the baseline screenshot needs to be replaced with the new actual screenshot. (You can do this by moving the screenshot from the .tmp/actual
folder to the e2e/wdio/baselineScreenshots/platform or core/<component_name>/<browser_name>
folder).
The newly added (newly approved baseline) screenshots need to be committed to this PR.
All baseline screenshots need to be generated on the Saucelabs side (the same environment on which actual testing form the CI will be performed). Images generated during the local tests execution can't be used as a baseline screenshots, unless all the environment parameters completely match (OS version, Browser version, screen resolution).
When a test fails due to visual regression:
Preconditions: setup Saucelabs locally as shown here.
- Go to the Travis CI log and read which components and test cases failed
- Go to the wdio.conf.js file in your IDE
- Change the config to meet your debugging needs (specify component, browser/OS)
- i.e.
specs: [ './e2e/wdio/<platform/core>/**/<failing-spec-name>.e2e-spec.ts', ]
- i.e.
- Run the test through Saucelabs
- start app:
ng serve --disable-host-check --ssl --no-watch
- run tests:
npm run e2e:wdio:saucelabs
- start app:
- Check the log to see if there is additional info about failed tests
- After test completion, go to the
.tmp/diff/<browser>
folder (choose the folder of the browser you need) - Look for the img with the name that matches the component under test
- Open the image You will see the baseline screenshot and actual screenshot together here with differences highlighted in pink color
- Check the difference, are the changes expected?
- If the changes are not expected:
- Make appropriate changes to the PR to fix the issue
- Rerun test
- If the changes are expected:
- Continue to the next step
- If the changes are not expected:
- Go to the
.tmp/actual/<browser>
folder - Find the img with the same file name as the one you checked
- Copy the img file
- Go to the
.e2e/wdio/baselineScreenshot/<browser>
folder - Paste the img file there
- Choose to overwrite the existing file Now the original baseline screenshot is replaced with the new, actual screenshot, which will be used as the baseline for all future runs.