Integrate Percy with your test suite to run visual tests. To do that, follow these steps:
Create a Percy project Sign in to Percy. In Percy, create a project of the type, Web, and then name the project. After the project is created, Percy generates a token. Make a note of it. You have to use it set your environment variable in the next step.
Run Percy
Run your tests using the percy exec command as shown below:
If you are unable to use the percy:exec command or prefer to run your tests using IDE run options, you can use the percy exec:start and percy exec:stop commands. To learn more, visit Run Percy.
```bash
npx percy exec -- <command to run the test script file>
```
[percy] Percy has started![percy] Created build #1: https://percy.io/[your-project][percy] Running "node script.js"[percy] Snapshot taken "Example Site"[percy] Stopping percy...
[percy] Finalized build #1: https://percy.io/[your-project][percy] Done!
Congratulations!
You have successfully created your first build on Percy. To see the build with snapshots of your application, visit your project in Percy.
When you run another build with visual changes to your application, Percy takes new snapshots. You can then see the comparisons between the two runs on the new build.
Advanced topics
Percy Snapshot command
In the preceding steps, we used the Percy Snapshot command for capturing snapshots. Percy provides various configurations to use with Percy snapshot command. To learn more visit, Percy snapshot command.
Base build selection
By default, Percy uses the previous build for comparison however, you always have the option to configure the base build for comparison as needed. To learn more, visit base build selection logic.
Integrating functional and visual testing can greatly enhance your ability to catch UI regressions and ensure the visual consistency of your application. By integrating Percy with your functional testing pipeline, you can seamlessly capture screenshots during your functional test runs and compare them to baseline images.
Percy with Automate
If you choose the “Automate” option when creating a Percy project, the “browsers selection” option will not appear in the project settings. Instead, the selection of browsers will be based on the capabilities specified in the Automate session.
You can use the Percy Screenshot command to capture a screenshot when a page is rendered during an Automate session. This screenshot is directly sent to the Percy build for comparison.
Check out the video below to get an overview of Percy on Automate:
Set the browser height and width to get consistent screenshots.
For Apple devices, close the tag for smart app banners before taking screenshots.
Playwright for Android is currently supported only for NodeJS.
Integration steps
Integrate Percy on Automate with your test suite to run visual tests. To do that, follow these steps:
Capture a full-page screenshot using the full-page parameter. By default, Percy On Automate captures a single tile, for the full page, refer to the instructions below.
(2) Use the Percy screenshot command to take required screenshots in your Automate session.
For example, in the following Python code, the percy_screenshot("name", "options") method is called twice. In the first instance, we are passing only the required argument which is “name”. In the second instance, we are passing both the required and optional “options”.
# In your desired location add the command
percy_screenshot(page, name ='Screenshot 1')# You can pass `options`
percy_screenshot(page, name ='Screenshot 1', options ={'freezeAnimation': False, 'percyCSS':'h1{color:green;} a{color: green;font-size: 1rem;}', 'ignore_region_xpaths':['/html/body/div/p[2]/a']})
```javascript
// In your desired location add the command
await percyScreenshot(page, "Screenshot 1");
// You can pass `options`
await percyScreenshot(page, 'Screenshot 1', { 'freezeAnimation': False, 'percyCSS': 'h1{color:green;} a{color: green;font-size: 1rem;}', 'ignoreRegionXpaths':['/html/body/div/p[2]/a']})
```
// In your desired location add the command
await percyScreenshot(page, "Screenshot 1");
// You can pass `options`
await percyScreenshot(page, 'Screenshot 1', {'freezeAnimation': False, 'percyCSS':'h1{color:green;} a{color: green;font-size: 1rem;}', 'ignoreRegionXpaths':['/html/body/div/p[2]/a']})
```java
// Initialize the percy object using page instance
Percy percy = new Percy(page);
// In your desired location add the command
percy.screenshot("screenshot_1");
// You can pass `options`
HashMap<String, Object> options = new HashMap<String, Object>();
options.put("percyCSS", "h1{color:green;}");
percy.screenshot("screenshot_1", options);
```
// Initialize the percy object using page instance
Percy percy = new Percy(page);
// In your desired location add the command
percy.screenshot("screenshot_1");
// You can pass `options`
HashMap<String, Object> options = new HashMap<String, Object>();
options.put("percyCSS", "h1{color:green;}");
percy.screenshot("screenshot_1", options);
```.NET
Percy.Screenshot(page, "example_screenshot_1");
// You can pass `options`
Dictionary<string, object> options = new Dictionary<string, object>();
options.Add("percyCSS", "h1{color:green}");
Percy.Screenshot(driver, "example_screenshot_1", options);
```
Percy.Screenshot(page, "example_screenshot_1");
// You can pass `options`
Dictionary<string, object> options = new Dictionary<string, object>();
options.Add("percyCSS", "h1{color:green}");
Percy.Screenshot(driver, "example_screenshot_1", options);
Arguments
Description
name(String)
(Required) The screenshot name must be unique for each screenshot captured. For example: name = Screenshot 1.
options()
1. (Optional) freezeAnimatedImage(Boolean) - To determine whether Percy will perform stabilization on the DOM. By default set to false, set to true if stabilization needs to be performed by Percy. 2. (Optional) freezeAnimatedImageOptions(Boolean) - In the case of specific animated images that do not appear to freeze, we can select and submit them for freezing.There are two ways to select it. i. freezeImageBySelectors. ii. freezeImageByXpaths. 3. (Optional) percyCSS - Apply Percy-specific CSS to stabilize screenshots. 4. (Optional) ignoreRegions - To ignore a particular set of elements or sections in DOM. There are four ways to pass it. i. ignoreRegionXpaths. ii. ignoreRegionSelectors. iii. customIgnoreRegions. 5. (Optional) considerRegions - To counteract the effects of IntelliIgnore. There are two ways to consider it. i. considerRegionXpaths. ii. considerRegionSelectors. iii. customConsiderRegions.
full-page(Boolean)
Set to true if we want to take fullpage screenshots, default is false.
Step 6 - Run the build
```bash
npx percy exec -- <command to run the automate script file>
```
npx percy exec -- <command to run the automate script file>
Results
You can view the visual comparison results on the generated Percy builds.
Percy on Automate Features
If there are more than five screen widths, a dropdown menu will show up with different width options. The dropdown will display a maximum of ten entries.
The build generation panel now includes a new device selection filter on the left pane.
You can easily switch between the Percy and the Automate session.
You can use the Intelli Ignore feature to check the contextual differences. For more information, see Intelli Ignore.
Debug in Automate Session
The generated Percy builds have a redirect link to your builds on the Automate session.
The generated builds on the Automate session have a redirect link to your Percy builds.
Once the Automate builds are generated, you can use various logging options such as Text Logs, Network Logs, Other Logs, Issues Detected, and many more. For more information, see Debugging options to review your builds.
To determine the timestamp of a screenshot taken, you can search for the Percy Screenshot command within your Automate session.
The Percy screenshot command displays the screenshot name besides it, as shown:
Handle dynamic elements
If you have dynamic JavaScript elements or carousels in your test, stabilize the DOM first and then invoke percyScreenshot. This allows for capturing consistent and accurate screenshots.
For example, if you are using bootstrap carousel, handle it as shown below:
```python
# before taking screenshot we force the carousel to be at 1st tile
driver.execute_script(`var carousel = $('.carousel');
carousel.carousel(0);
carousel.carousel('pause');`)
percy_screenshot(driver, name = 'Screenshot 1')
```
# before taking screenshot we force the carousel to be at 1st tile
driver.execute_script(`var carousel =$('.carousel');
carousel.carousel(0);
carousel.carousel('pause');`)
percy_screenshot(driver, name ='Screenshot 1')
Another example is how to handle sticky elements. Suppose the website you’re testing has the cookie banner. You can either accept or reject it. In the following snippet, we’re accepting the cookie:
```python
# before taking screenshot please close the sticky elements
cookie_banner = wait.until(EC.presence_of_element_located((By.ID, 'cookie-banner')))
# Find the accept button and click it
accept_button = cookie_banner.find_element(By.XPATH, '//button[text()="Accept"]')
accept_button.click()
percy_screenshot(driver, name = 'Screenshot 1')
```
# before taking screenshot please close the sticky elements
cookie_banner = wait.until(EC.presence_of_element_located((By.ID, 'cookie-banner')))# Find the accept button and click it
accept_button = cookie_banner.find_element(By.XPATH, '//button[text()="Accept"]')
accept_button.click()
percy_screenshot(driver, name ='Screenshot 1')
Full page screenshots
Your webpage may require multiple scrolls to get to the end of a page. You can test these long pages using Percy’s full-page screenshot feature. It offers a comprehensive snapshot of a web page, capturing its entire length and content in a single image.
Things to keep in mind
Call percyScreenshot after ensuring the page has loaded.
Although we attempt to handle lazy loading, if any issues arise, ensure you scroll to the bottom before calling percyScreenshot.
Dynamic elements such as JS based animation, video, carousel need to be paused before full page screenshot.
The maximum limit for the length of a full-page screenshot on desktop is 10000px or 10 tiles, whichever is minimum, and for mobile, it is 10 tiles.
A website with different layouts based on geolocation can be managed by the user using IP Geolocation, as explained in the BrowserStack Documentation.
The user needs to handle any popups that might appear while taking a screenshot.
Remove meta tags that may hinder the webpage by adding native elements.
The customIgnoreRegions from the ignoreRegions option and customConsiderRegions from the considerRegions option do not function with full-page screenshots.
Benefits of Percy on Automate
Using Percy on Automate is cost-effective and performant.
It supports a single pipeline for functional and visual testing.
It saves lot of network traffic as we are directly uploading screenshots from BrowserStack terminal to Percy cloud.
Tell us the capture movement and Percy will take screenshot of that movement as it supports css pseudo classes.
It supports third party automated testing platforms including in-house setups.
Exposure to highest browser and devices coverage while using BrowserStack Automate.
Limitations
Internet Explorer is not supported.
For Mozilla Firefox, Google Chrome, and Microsoft Edge, browser versions less than 91 are not supported.
Mojave and older versions of macOS are not supported.
The FreezeAnimatedImage option does not have support for macOS Catalina and earlier versions.
Chromium on iPhone is not supported.
Compatibility Matrix
Refer the below matrix to check what frameworks are supported by Percy on Automate.