Applitools Eyes SDK for Selenium Python
Applitools Eyes SDK for Selenium Python is a proxy package that facilitates automated visual testing for web applications by integrating with Selenium WebDriver. It allows developers and QA engineers to automatically validate the correctness of an application's UI layout, content, and appearance using AI-driven image comparisons. The current version is 6.4.32, and the library is actively maintained with frequent patch releases.
Warnings
- breaking The GitHub repository `applitools/eyes.selenium.python` has been deprecated. While the `eyes-selenium` PyPI package remains the correct installation, users should refer to `applitools/eyes.sdk.python` for the latest source and potentially more up-to-date examples if direct repository interaction is needed.
- gotcha The version of `ChromeDriver` (or any other browser driver) used with Selenium WebDriver must precisely match the installed version of the Chrome browser to avoid `WebDriverException` errors during initialization.
- gotcha Applitools tests require the `APPLITOOLS_API_KEY` environment variable to be set for authentication with the Applitools Eyes server.
- gotcha Every Applitools Eyes test session must begin with `eyes.open()` and conclude with `eyes.close()` to ensure proper test execution and result reporting. Failing to call `close()` can lead to abandoned tests.
- deprecated The synchronous `eyes.close()` method returns a single test result. For parallel execution or when using runners (like `VisualGridRunner`), it's recommended to use asynchronous closing mechanisms (`eyes.close_async()`) followed by `runner.get_all_test_results()` to retrieve all results efficiently.
Install
-
pip install eyes-selenium
Imports
- Eyes
from applitools.selenium import Eyes
- Target
from applitools.selenium import Target
- VisualGridRunner
from applitools.selenium import VisualGridRunner
- ClassicRunner
from applitools.selenium import ClassicRunner
- BatchInfo
from applitools.common import BatchInfo
Quickstart
import os
import pytest
from selenium import webdriver
from applitools.selenium import Eyes, Target, VisualGridRunner, BatchInfo
# Set your Applitools API key as an environment variable APPLITOOLS_API_KEY
# For example: export APPLITOOLS_API_KEY='YOUR_API_KEY'
APPLITOOLS_API_KEY = os.environ.get('APPLITOOLS_API_KEY', '')
if not APPLITOOLS_API_KEY:
raise ValueError("APPLITOOLS_API_KEY environment variable not set.")
@pytest.fixture(scope='session')
def applitools_runner():
runner = VisualGridRunner()
yield runner
# Wait for all visual tests to complete and get results
print("\nVisual test results:\n", runner.get_all_test_results(raise_exception=True))
@pytest.fixture(scope='function')
def driver():
# Ensure your ChromeDriver version matches your Chrome browser version
options = webdriver.ChromeOptions()
# options.add_argument('--headless') # Uncomment for headless execution
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(10)
yield driver
driver.quit()
def test_applitools_example(driver, applitools_runner):
eyes = Eyes(runner=applitools_runner)
eyes.api_key = APPLITOOLS_API_KEY
eyes.batch = BatchInfo('My Python App') # Group tests in Test Manager
try:
# Open Eyes with the WebDriver, application name, test name, and viewport size
eyes.open(driver=driver, app_name='My App', test_name='Login Page Test', viewport_size={'width': 800, 'height': 600})
driver.get('https://demo.applitools.com/')
eyes.check('Login Window', Target.window().fully())
driver.find_element('id', 'username').send_keys('user')
driver.find_element('id', 'password').send_keys('password')
driver.find_element('id', 'log-in').click()
eyes.check('App Dashboard', Target.window().fully())
finally:
# Close Eyes to finish the test session
eyes.close()