Selenium for Python

Integrating Percy with your Selenium Python tests

👍

Covered in this doc

  • Integrating Percy with your Selenium Python tests
  • Installing and importing the percy-selenium module
  • Calling and configuring percy_snapshot

Test

Setup


If you're not ready to integrate Percy, check out our 2-minute Selenium for Python tutorial and example app with Percy already added.

🚧

Uprgrading to v1.x

If you're currently using v0.x.x of the Percy Python SDK, there are a few migration steps to follow

Install @percy/cli (currently only available from NPM). This package does the heavy lifting & communication to our API:

npm install --save-dev @percy/cli
yarn add -D @percy/agent

Now install the percy-selenium Python package:

pip install percy-selenium

Step 1: In order to start creating snapshots from your Selenium tests, you'll need to import the percy_snapshot function from the percy-selenium package into your test.

# At the top of your test file(s)
from percy import percy_snapshot

Step 2: You can now incorporate visual tests in your existing suite:

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')

Step 3: Finally, wrap your test runner command in the percy exec command. This will start a local Percy server to receive snapshots from your Python Selenium tests. From there the snapshots are processed and uploaded to your Percy dashboard. An example might be: percy exec -- python tests.py.

Before you can successfully run Percy, the PERCY_TOKEN environment variable must be set:

export PERCY_TOKEN=[your-projects-token]
set PERCY_TOKEN=[your-projects-token]

# Powershell
$Env:PERCY_TOKEN="[your-projects-token]"

Now you can run percy exec. For example:

percy exec -- python tests.py

Note the double dash, --, between percy exec and your test run command.

That's it! Now, whenever you run your tests with Percy, a snapshot of the app in that state will be uploaded to Percy for visual regression testing!

Configuration


percy_snapshot(driver, name[, **kwargs])

For example:

percy_snapshot(driver, 'Home page');
percy_snapshot(
  driver=driver, 
  name='Homepage responsive test',
  widths=[768, 992, 1200],
  percy_css="iframe { display: none; }"
);

Global configuration

You can also configure Percy to use the same options over all snapshots. To see supported configuration read our configuration docs