Skip to main content
AI agents are now live in Website Scanner! Enter your URL and fix website issues 3x faster with AI. Explore now!
No Result Found
Connect and get help from 7,000+ developers on our Discord community. Ask the CommunityAsk the Community

Integrate Percy with Cucumber-TestNG

Learn how to integrate Cucumber-TestNG automated tests and Percy to catch visual differences in your website.

Percy integrates with your tests using both Percy and BrowserStack SDK. To establish this integration, choose the appropriate SDK and refer to the following section accordingly:

To integrate your Cucumber-TestNG based test suite with Percy using the BrowserStack SDK, follow these steps:

Prerequisites

Before you start, ensure that you have a BrowserStack Automate account using the BrowserStack SDK to integrate your test script.

Set BrowserStack credentials

Save your BrowserStack credentials as environment variables. It simplifies running your test suite from your local or CI environment. You can export the environment variables for the Username and Access Key of your BrowserStack account or you can set it in the config file.

Copy icon Copy
Copy icon Copy
Copy icon Copy

Install or Upgrade BrowserStack SDK

Copy icon Copy
Copy icon Copy

Update your BrowserStack config file

In the browserstack.yml file:

  1. Set percy: true.

  2. Set a projectName.
    After you run a test, an Automate project and a Percy project are created with the project name you set here.
    If a Percy project by the name you set in the browserstack.yml file already exists, your screenshots are added to the same project. However, if the name matches an App Percy project, your visual tests will continue to run, but an error message will appear in your logs. Your functional tests still run as expected.

  3. Set percyCaptureMode: auto.
    There are other supported automated capture modes in the percyCaptureMode parameter. The table below lists and describes all the acceptable modes.

Percy screenshot capture mode Description
auto Automatically capture screenshots on common events such as screenshot, click, and sendKeys.
testcase Automatically capture screenshots at the end of each test case.
click Automatically capture screenshots on every click.
screenshot Automatically capture screenshots on every driver.screenshot call.
manual This gives you more control over the screenshots you want captured. When used, you have to add the Percy.screenshot(driver, name) method at required points in your test script.

Below sample browserstack.yml file shows how to set the percy, the projectName, and the percyCaptureMode parameters.

Copy icon Copy

Additional configuration settings (Optional)

To manually capture screenshots alongside the auto mode, implement the following steps in your test script:

  1. Import the BrowserStack Percy SDK in your test script.
  2. Add the PercySDK.screenshot(driver, name) method at required points in your test script to get the screenshots you want.

Here’s an example:

Java
Copy icon Copy

Run your test script

You can continue running your tests as you have been previously.

Summary

You have successfully integrated Percy with BrowserStack SDK 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 screenshots. You can then see the comparisons between the two runs on the new build. You can access the Percy dashboard directly from the Automate dashboard.

The Percy Selenium SDK ships ready-made Cucumber step definitions in the io.percy.selenium.cucumber.PercySteps class. Add this package to the 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:

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 following components to establish the integration environment for your test suite.

Install Percy CLI by running the following command:

Copy icon Copy

If you’re using Maven, add percy-java-selenium 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):

Copy icon Copy

Set up Cucumber hooks
Pass your WebDriver instance to PercySteps from a Cucumber @Before hook, and reset it in an @After hook. The setDriver method also registers the Cucumber wrapper in your Percy build information.

Copy icon Copy

Add the io.percy.selenium.cucumber package to your Cucumber glue path so that the step definitions are discovered:

Copy icon Copy

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:

Copy icon Copy

To pass several options at once, use the with options: step with a Cucumber data table:

Copy icon Copy

For the full list of available steps, see Available step definitions.

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

This command starts Percy, creates a new Percy build, takes snapshots and uploads them to your project, and stops Percy:

Copy icon Copy

Summary

You have successfully integrated Percy with your Selenium 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, Automate screenshots, 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

Screenshot steps (Percy on Automate)

Capture screenshots when you run visual tests on BrowserStack Automate:

Gherkin step Description
When I take a Percy screenshot named "name" Basic Automate screenshot
When I take a Percy screenshot named "name" with regions Screenshot with previously defined regions
When I take a Percy screenshot named "name" with options: Screenshot with a Cucumber data table of options

Region steps

Define ignore, consider, or IntelliIgnore regions before you take a snapshot or screenshot:

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

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