Integrate Percy with Playwright and Cucumber
Learn how to integrate Playwright with Java-Cucumber automated tests and Percy to catch visual differences in your website.
The Percy Playwright SDK ships ready-made Cucumber step definitions in the io.percy.playwright.cucumber.PercySteps class. Add this package to your Cucumber glue path to write visual tests in plain Gherkin, without authoring custom Java step code.
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:
To learn about environment variables in Percy, see Percy environment variables.
Install Percy dependencies
Install the following components to establish the integration environment for your test suite.
Install Percy CLI by running the following command:
If you’re using Maven, add percy-playwright-java and cucumber-java to your project dependencies. The Percy SDK declares Cucumber as a provided dependency, so you supply your own Cucumber version (7.x is recommended):
Set up Cucumber hooks
Pass your Playwright Page instance to PercySteps from a Cucumber @Before hook, and reset it in an @After hook. The setPage method also registers the Cucumber wrapper in your Percy build information.
Add the io.percy.playwright.cucumber package to your Cucumber glue path so that the step definitions are discovered:
Write a feature file
Use the Percy steps in your Gherkin scenarios. The following feature file takes a basic snapshot, a responsive snapshot, and a snapshot that ignores a region:
To pass several options at once, use the with options: step with a Cucumber data table:
For the full list of available steps, see Available step definitions.
Run Percy
Run your tests using the percy exec command as shown below:
This command starts Percy, creates a new Percy build, takes snapshots and uploads them to your project, and stops Percy:
Summary
You have successfully integrated Percy with your Playwright Cucumber test suite and created your first build. 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.
Available step definitions
The PercySteps class provides Gherkin steps for snapshots and ignore or consider regions.
Snapshot steps
Capture DOM snapshots for visual comparison:
| Gherkin step | Description |
|---|---|
When I take a Percy snapshot named "name" |
Basic snapshot |
When I take a Percy snapshot named "name" with widths "375,768,1280" |
Snapshot at specific browser widths |
When I take a Percy snapshot named "name" with min height 1024 |
Set minimum screenshot height |
When I take a Percy snapshot named "name" with Percy CSS "h1 { color: red; }" |
Inject custom CSS during the snapshot |
When I take a Percy snapshot named "name" with scope ".main-content" |
Scope the snapshot to a CSS selector |
When I take a Percy snapshot named "name" with layout mode |
Layout-only comparison that ignores text changes |
When I take a Percy snapshot named "name" with JavaScript enabled |
Enable JavaScript during rendering |
When I take a Percy snapshot named "name" with labels "smoke,regression" |
Add labels to organize snapshots |
When I take a Percy snapshot named "name" with test case "TC-001" |
Associate the snapshot with a test case ID |
When I take a Percy snapshot named "name" with Shadow DOM disabled |
Disable Shadow DOM capture |
When I take a Percy snapshot named "name" with responsive capture |
Resize the browser for each width |
When I take a Percy snapshot named "name" with sync |
Wait for Percy to finish processing |
When I take a Percy snapshot named "name" with regions |
Snapshot with previously defined regions |
When I take a Percy snapshot named "name" with widths "375,1280" and regions |
Snapshot with widths and regions |
When I take a Percy snapshot named "name" with options: |
Snapshot with a Cucumber data table of options |
Region steps
Define ignore, consider, or IntelliIgnore regions before you take a snapshot:
| Gherkin step | Description |
|---|---|
Given I create a Percy ignore region with CSS selector ".ad-banner" |
Ignore a region by CSS selector |
Given I create a Percy ignore region with XPath "//header" |
Ignore a region by XPath |
Given I create a Percy ignore region with bounding box 0, 0, 600, 80 |
Ignore a region by coordinates |
Given I create a Percy ignore region with CSS selector ".ad" and padding 10 |
Ignore a region by CSS selector with padding |
Given I create a Percy ignore region with XPath "//ad" and padding 10 |
Ignore a region by XPath with padding |
Given I create a Percy consider region with CSS selector ".content" |
Consider only this region, by CSS selector |
Given I create a Percy consider region with CSS selector ".content" and diff sensitivity 3 |
Consider a region with custom diff sensitivity |
Given I create a Percy consider region with XPath "//main" |
Consider a region by XPath |
Given I create a Percy consider region with XPath "//main" and diff sensitivity 3 |
Consider a region by XPath with custom diff sensitivity |
Given I create a Percy intelliignore region with CSS selector ".dynamic" |
IntelliIgnore a region by CSS selector |
Given I create a Percy intelliignore region with XPath "//carousel" |
IntelliIgnore a region by XPath |
Given I clear Percy regions |
Clear all previously defined regions |
Related topics
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
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!