Get your setup working faster. Join our Discord for optimisation tips from elite testers.Join our Discord
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:
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.
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.
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.
```yaml
userName: "YOUR_USERNAME"
accessKey: "YOUR_ACCESS_KEY"
platforms:
- os: Windows
osVersion: 11
browserName: Chrome
browserVersion: 103.0
- os: Windows
osVersion: 10
browserName: Firefox
browserVersion: 102.0
- os: OS X
osVersion: Big Sur
browserName: Safari
browserVersion: 14.1
parallelsPerPlatform: 1
browserstackLocal: true
buildName: browserstack-build-1
projectName: BrowserStack Sample
debug: false
percy: true
percyCaptureMode: auto
```
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.
By default, Percy Web supports running Selenium tests with Automate Turboscale.
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.
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):
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.
```java
import io.percy.selenium.cucumber.PercySteps;
import io.cucumber.java.Before;
import io.cucumber.java.After;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Hooks {
private static WebDriver driver;
@Before
public void setUp() {
driver = new ChromeDriver();
PercySteps.setDriver(driver);
}
@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
PercySteps.reset();
}
}
```
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:
```gherkin
Feature: Visual testing with Percy
Scenario: Homepage visual test
When I take a Percy snapshot named "Homepage"
Scenario: Responsive snapshot
When I take a Percy snapshot named "Responsive" with widths "375,768,1280"
Scenario: Homepage without ads
Given I create a Percy ignore region with CSS selector ".ad-banner"
When I take a Percy snapshot named "Homepage Clean" with regions
```
To pass several options at once, use the with options: step with a Cucumber data table:
```gherkin
Scenario: Snapshot with multiple options
When I take a Percy snapshot named "Checkout" with options:
| key | value |
| widths | 375,1280 |
| sync | true |
| labels | smoke,checkout |
```
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.
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"