Integrate your Cypress tests with Percy
A guide to integrating your Cypress automated tests with BrowserStack Percy. Catch visual differences in your web application on time.
Percy integrates with your tests through both Percy and Automate TurboScale using BrowserStack Cypress CLI. To set up the integration, select the appropriate section and follow the instructions provided.
This documentation applies to Percy Cypress SDK version 3.0.0 and above.
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:
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:
Update your test script
Import the Percy library to use the method and attributes required to take screenshots.
In order to add Percy snapshots to your Cypress tests, you have to first import the @percy/cypress
package into your cypress/support/e2e.js
file:
If you’re using TypeScript
, include "types": ["cypress", "@percy/cypress"]
in your tsconfig.json
file.
This gives you access to the Percy snapshot command in any of your Cypress tests, via cy.percySnapshot()
. You can now incorporate visual tests in your existing suite:
The snapshot method arguments are:
-
name
- The snapshot name; must be unique to each snapshot; defaults to the test title -
options
- See per-snapshot configuration options
For example:
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.
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.
Related topics
In this tutorial, we will use Cypress’ Kitchen Sink sample app to run our tests. It is an example app provided by Cypress to learn how Cypress works.
Prerequisites
- Ensure that you understand Cypress fundamentals and how Cypress runs tests
- Cypress version 13.10
- BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan
- NodeJS version 20.9
Run your first test
Perform the following steps to run Cypress tests with Automate Turboscale using the sample Kitchen Sink project.
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 to 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 and BrowserStack Cypress CLI dependencies
Install the components required to establish the integration environment for your test suite.
To install the dependencies, run the following command:
# Install the BrowserStack Cypress CLI
npm install -g browserstack-cypress-cli
# Install the Percy and Cypress CLI
npm install --save-dev @percy/cypress
Create the configuration file
Create and configure the browserstack.json
file which contains configurations, such as BrowserStack credentials, capabilities, etc., that are required for running the tests. Use the following init
command to initialize the app project folder and create a boilerplate browserstack.json
file:
browserstack-cypress init
After the browserstack.json
file is created, modify or add the mandatory configurations that are required to run the Cypress test as shown in the following sample code. The mandatory configurations are BrowserStack credentials, Turboscale parameters, Cypress configuration file, browser-device combinations, and the number of parallels:
// browserstack.json
{
"auth": {
"username": "YOUR_USERNAME",
"access_key": "YOUR_ACCESS_KEY"
},
"browsers": [{
"browser": "chrome",
"os": "linux",
"versions": "latest"
}
],
"run_settings": {
"cypress_config_file": "./cypress.config.js",
"project_name": "new-project",
"build_name": "new-build",
"exclude": [],
"parallels": "2",
"npm_dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-modal": "^3.16.1",
"react-router-dom": "^6.15.0",
"@percy/cypress": "^3.1.6",
"system_env_vars": ["PERCY_TOKEN"]
},
"record": true,
"record-key": "87b774c4-a373-43fa-986e-0833a55dc6d7",
"projectId": "2m6i88",
"package_config_options": {},
"headless": true,
"turboScale": true,
"turboScaleOptions": {
"gridName": "turboscale-trial-grid"
}
},
"disable_usage_reporting": true
}
In the above sample code snippet, the following parameters are mandatory to run the test:
-
username
,access_key
-
browser
,os
-
projectId
,turboScale
PERCY_TOKEN
For more information, see Run your first Cypress test with Automate Turboscale.
Update your test script
Import the Percy library to use the method and attributes required to take screenshots.
In order to add Percy snapshots to your Cypress tests, you have to first import the @percy/cypress
package into your cypress/support/e2e.js
file:
If you’re using TypeScript
, include "types": ["cypress", "@percy/cypress"]
in your tsconfig.json
file.
This gives you access to the Percy snapshot command in any of your Cypress tests, via cy.percySnapshot()
. You can now incorporate visual tests in your existing suite:
The snapshot method arguments are:
-
name
- The snapshot name; must be unique to each snapshot; defaults to the test title -
options
- See per-snapshot configuration options
For example:
Run tests
Run the test using the following command:
npx browserstack-cypress run
View your test results
After you run your test, review visual changes in your Percy project builds.
Advanced scenario
If you run tests across multiple browsers or use parallels, set the PERCY_PARALLEL_TOTAL
and PERCY_PARALLEL_NONCE
environment variable so Percy knows how many parallel workers to expect, and which parallels to combine into a single build. For more information, see the Parallel test suites.
You must both export and set PERCY_PARALLEL_NONCE
and PERCY_TOKEN
in the config file.
Example
If you run tests with 2 parallels across 3 browsers, set:PERCY_PARALLEL_TOTAL=6
and PERCY_PARALLEL_NONCE=12345
.
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!