LambdaTest Selenium Driver
The lambdatest-selenium-driver is a Python SDK for integrating SmartUI visual regression testing with Selenium tests on the LambdaTest cloud grid. It facilitates running automated cross-browser compatibility and visual tests across a wide range of browser and operating system environments. Currently at version 1.0.9, the library is actively maintained, with associated repositories showing regular updates and support for the latest Selenium versions.
Warnings
- gotcha For SmartUI visual testing features, you must install the `@lambdatest/smartui-cli` Node.js package globally and set the `PROJECT_TOKEN` environment variable. This is an external dependency not managed by pip.
- breaking Selenium 4 introduced the W3C WebDriver standard, which changed how capabilities are structured. Ensure your capabilities are W3C compliant or properly vendor-prefixed (e.g., LambdaTest specific options should be nested under `LT:Options`) to avoid session startup failures.
- gotcha The `LT_USERNAME` and `LT_ACCESS_KEY` environment variables are crucial for authenticating with the LambdaTest cloud grid. If these are not correctly set, your `webdriver.Remote` connection will fail.
- gotcha Relying on `time.sleep()` or excessively long implicit waits can lead to flaky tests, especially when dealing with dynamic web elements or network latency on a remote grid. Elements might not be ready when the script tries to interact with them.
Install
-
pip install lambdatest-selenium-driver -
npm install -g @lambdatest/smartui-cli
Imports
- smartui_snapshot
from lambdatest_selenium_driver import smartui_snapshot
- webdriver.Remote
from selenium import webdriver
- Options
from selenium.webdriver.chrome.options import Options as ChromeOptions
Quickstart
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
from lambdatest_selenium_driver import smartui_snapshot
LT_USERNAME = os.environ.get('LT_USERNAME', '')
LT_ACCESS_KEY = os.environ.get('LT_ACCESS_KEY', '')
if not LT_USERNAME or not LT_ACCESS_KEY:
print("Please set LT_USERNAME and LT_ACCESS_KEY environment variables.")
exit(1)
def run_lambdatest_smartui_test():
options = ChromeOptions()
options.browser_version = "latest"
options.platform_name = "Windows 10"
lt_options = {
"username": LT_USERNAME,
"accessKey": LT_ACCESS_KEY,
"build": "Python SmartUI Build",
"project": "My SmartUI Project",
"name": "SmartUI Sample Test",
"w3c": True,
"plugin": "python-python",
"smartUI.project": os.environ.get('PROJECT_TOKEN', 'Your_SmartUI_Project_Token_Here') # For SmartUI
}
options.set_capability("LT:Options", lt_options)
driver = None
try:
driver = webdriver.Remote(
command_executor=f"http://{LT_USERNAME}:{LT_ACCESS_KEY}@hub.lambdatest.com/wd/hub",
options=options,
)
driver.implicitly_wait(10)
driver.get("https://lambdatest.github.io/sample-todo-app/")
print("Successfully loaded URL: https://lambdatest.github.io/sample-todo-app/")
# Take a SmartUI snapshot
smartui_snapshot(driver, "Todo App Homepage")
driver.find_element(By.NAME, "li1").click()
smartui_snapshot(driver, "First Item Clicked")
driver.find_element(By.NAME, "li2").click()
smartui_snapshot(driver, "Second Item Clicked")
driver.execute_script("lambda-status=passed")
print("Test Finished Successfully")
except Exception as e:
if driver:
driver.execute_script("lambda-status=failed")
print(f"Test Failed: {e}")
finally:
if driver:
driver.quit()
if __name__ == '__main__':
# Make sure to set PROJECT_TOKEN environment variable if running SmartUI tests
# For Linux/macOS: export PROJECT_TOKEN="<YOUR_SMARTUI_PROJECT_TOKEN>"
# For Windows: set PROJECT_TOKEN="<YOUR_SMARTUI_PROJECT_TOKEN>"
run_lambdatest_smartui_test()