Selenium is a browser automation framework, designed to automate browsers. While it is used for running functional cross browser tests, it lacks the ability to compare the UI of webpages across various browsers.
TestingBot offers a visual regression testing feature which integrates in your existing or new Selenium WebDriver scripts. By adding one or more visual checks to your Selenium test, you can easily do UI comparison checks and detect visual regressions.
When you run this command for the first time, a screenshot will be taken from the page and will be marked as the baseline.
Any future calls with the same identifier will trigger a pixel comparison on TestingBot's end. If TestingBot detects the number of different pixels to be higher than the supplied threshold, the call will return a response indicating the UI has changed.
To add the command to your Selenium tests, please see the example below, where we save a screenshot of the current page as uniqueIdentifier.
The identifier passed to tb:visual.snapshot is unique per browser vendor, not per browser version. TestingBot considers browser versions to generate identical UI's, whereas different browser vendors might generate slightly different UI's (for example Safari vs Chrome).
Visual Comparison Response
Whenever you run a visual comparison during a Selenium test, you will receive back a response in less than 2 seconds. This response indicates whether TestingBot considers the current screenshot and the baseline screenshot to be identical, along with the count of detected differing pixels.
Please see the example below on how to check whether a visual change was detected:
json_string=driver.execute_script("tb:visual.snapshot=uniqueIdentifier")# Decode the JSON response using the json module
json_object=json.loads(json_string)# Check if the 'matched' key is true
The first time you'll run the compare command, TestingBot will automatically mark the screenshot as the baseline.
If you want to change the baseline after the first run, you can use the tb:visual.baseline command to take a new screenshot and set it as the baseline (golden image).
You might want to use it to update the baseline when your webpage has changed its content or layout.
Please see the example below on how to set the baseline to a new screenshot: