Skip to main content
No Result Found

Integrate your Playwright tests with Percy

A guide to integrating your Playwright automated tests with BrowserStack Percy. Catch visual differences in your web application on time.

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
export PERCY_TOKEN="<your token here>"
Copy icon Copy
$Env:PERCY_TOKEN="<your token here>"
Copy icon Copy
set PERCY_TOKEN="<your token here>"

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.

To install the dependencies, run the following command:

Copy icon Copy
npm install @percy/playwright
Copy icon Copy
<groupId>io.percy</groupId>
<artifactId>percy-playwright-java</artifactId>
<version>1.0.0</version>
Copy icon Copy
pip install percy-playwright
Copy icon Copy
<PackageReference Include="PercyIO.Playwright" Version="1.0.0" />

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
const { chromium } = require('playwright');
const percySnapshot = require('@percy/playwright');
(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('http://example.com/', { waitUntil: 'networkidle' });
  await percySnapshot(page, 'Example Site');
  await browser.close();
})();
Copy icon Copy
percy.snapshot("snapshot_1");
Copy icon Copy
percy_snapshot(page, name="snapshot_1")
Copy icon Copy
Percy.Snapshot(page, "snaspshot_1");

For other Playwright examples, see the Playwright docs.

The snapshot method arguments are:

Copy icon Copy
percySnapshot(page, name[, options])
Copy icon Copy
Map<String, Object> options = new HashMap<>();
options.put("testCase", "Should add product to cart");
percy.snapshot("snapshot_2", options);
Copy icon Copy
percy_screenshot(page, name = 'snapshot_1', options = {})
Copy icon Copy
Percy.Snapshot(page, "snapshot_2", options);


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
npx percy exec -- <command to run the test script file>
Copy icon Copy
[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.

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