Skip to main content
Experience faster, smarter testing with BrowserStack AI Agents. See what your workflow’s been missing. Explore now!
No Result Found
Connect & Get help from fellow developers on our Discord community. Ask the CommunityAsk the Community

Integrate Percy with Playwright and Javascript

Learn how to integrate Playwright with Javascript automated tests and Percy to catch visual differences in your website.

Percy SDK offers the following integrations for your Playwright tests:

  • Percy Web
  • Percy with Automate

To establish this integration, choose the appropriate variant and refer to the following section accordingly:

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.

For details on creating a project, see Create a Percy project.

Set the project token as an environment variable
Run the given command to set PERCY_TOKEN as an environment variable:

Copy icon Copy
Copy icon Copy
Copy icon Copy

To learn about environment variables in Percy, see Percy environment variables.

Install Percy dependencies
Install the components required to establish the integration environment for your test suite.

Copy icon Copy

Update your test script

Import the Percy library to use the method and attributes required to take screenshots.

The following example uses the percySnapshot function:

Copy icon Copy

For other Playwright examples, see the Playwright docs.

The snapshot method arguments are:

Copy icon Copy


To learn more, see Percy snapshot.

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.

Copy icon Copy
Copy icon Copy

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.

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.

  • 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.

Step 1 - Install the latest Percy CLI

Copy icon Copy

Step 2 - Install the SDK

Copy icon Copy

Step 3 - In the Percy dashboard make selections as below:

  • Select Web as the platform.
  • Select Automate to handle browser selection.
  • Click Create Project.

Select the type of project

Step 4 - Export the Percy token.

A Percy token will be generated when you create a project. Export this token environment variables.

Copy icon Copy
Copy icon Copy
Copy icon Copy

Step 5 - Update your Automate test script.

(1) Import percy_screenshot from Percy library using the below code:

Copy icon Copy

(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”.

Copy icon Copy
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

Copy icon Copy

Results

You can view the visual comparison results on the generated Percy builds. For more information, check Percy on Automate features.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle