Integrate your Selenium Python tests with Percy
A guide to integrating your Selenium Python automated tests with BrowserStack Percy. Catch visual differences in your web application on time.
Percy SDK offers the following integrations for your Python tests:
- Percy Web
- Percy with Automate
To establish this integration, choose the appropriate variant and refer to the following section accordingly:
We recommend using our BrowserStack SDK for Percy integration.
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:
export PERCY_TOKEN="<your token here>"
$Env:PERCY_TOKEN="<your token here>"
set PERCY_TOKEN="<your token here>"
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:
npm install --save-dev @percy/cli
Install the percy-selenium
Python package by running the following command:
pip install percy-selenium
Update your test script
Import the Percy library to use the method and attributes required to take screenshots.
In the following code, the percy_snapshot
method is used:
from selenium import webdriver
from percy import percy_snapshot
browser.get('http://localhost:8000')
browser.implicitly_wait(10)
new_todo_input = browser.find_element_by_class_name('new-todo')
percy_snapshot(browser, 'Empty Todo State')
We are passing the required argument which is name
, and are using an <https://example.com>
web application. Python example
as the name of the snapshot within the percy_snapshot
method.
The snapshot method arguments are:
percy_snapshot(driver, name[, **kwargs])
-
driver
(required) - A selenium-webdriver driver instance -
name
(required) - The snapshot name; must be unique to each snapshot -
**kwargs
- An object containing various snapshot options. See per-snapshot configuration options
For example:
percy_snapshot(driver, 'Home page');
percy_snapshot(
driver=driver,
name='Homepage responsive test',
widths=[768, 992, 1200],
percy_css="iframe { display: none; }"
);
To learn more, visit Percy snapshot.
Run Percy
Run your tests using 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.
$ npx percy exec -- python tests.py
This command starts Percy, create a new Percy build, takes snapshots and uploads them to your project, and stops Percy:
[percy] Percy has started!
[percy] Created build #1: https://percy.io/[your-project]
[percy] Snapshot taken "Python example"
[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.
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!