LambdaTest Selenium Driver

1.0.9 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `lambdatest-selenium-driver` with a remote Selenium WebDriver connected to the LambdaTest grid, including SmartUI snapshot capabilities. Ensure `LT_USERNAME`, `LT_ACCESS_KEY`, and optionally `PROJECT_TOKEN` (for SmartUI) environment variables are set. The `smartui_snapshot` function captures visual snapshots for comparison.

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

view raw JSON →