How to Run Selenium Tests With Python on TestMu AI
Run your Python Selenium tests on the TestMu AI cloud grid across 10,000+ browser/device combinations. The setup is the same for every framework: you connect to the grid and pass your capabilities. This guide covers that shared flow once, then gives you a per-framework quickstart in the tabs below.
Prerequisites
Have these in place before you run any of the frameworks below.
- Create a TestMu AI account if you don't have one.
- Get your Username and Access Key from the TestMu AI Dashboard.
- Install Python and pip.
- Install the Selenium client and WebDriver bindings, and (recommended)
virtualenvto isolate dependencies.
Set Your Credentials
Every framework authenticates the same way: your Username and Access Key are read from environment variables. Set them once. Pick your operating system:
Verified- macOS / Linux
- Windows (CMD)
export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"
set LT_USERNAME=undefined
set LT_ACCESS_KEY=undefined
How the Sample Test Works
Every framework below connects to the grid at hub.lambdatest.com/wd/hub and passes your browser and OS choices through a capabilities dictionary:
capabilities = {
"build": "your build name",
"name": "your test name",
"platformName": "Windows 10",
"browserName": "Chrome",
"browserVersion": "latest",
}
What changes between frameworks is only how those capabilities are supplied: inline, a conftest.py, a .robot variables block, or a JSON config. That is what each tab covers.
Use the Capabilities Generator to build a capabilities block for any browser, version, and OS combination.
Run a Test in Your Framework
Each tab lists the framework-specific pieces. Clone the matching repo (it contains the full, ready-to-run project), then run.
- unittest
- pytest
- Robot
- Behave
- Lettuce
The standard-library unittest framework connects a remote WebDriver to the grid, with capabilities inline in the test.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/Python-UnitTest-Selenium
cd Python-UnitTest-Selenium
- Set your browser and OS in the capabilities dictionary:
capabilities = {
"build": "UnitTest-Selenium-Sample",
"name": "UnitTest-Selenium-Test",
"platformName": "Windows 11",
"browserName": "Chrome",
"browserVersion": "latest",
}
- Install the dependencies:
pip install -r requirements.txt
- Run the test:
python lambdatest_test.py
The test then appears on the Automation Dashboard. A green status confirms it passed.
pytest keeps capabilities in conftest.py and runs in parallel via pytest-xdist.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/pytest-selenium-sample
cd pytest-selenium-sample
- Set your browser and OS in
conftest.py:
capabilities = {
"build": "Sample PY Build",
"platformName": "Windows 11",
"browserName": "Chrome",
"browserVersion": "latest",
}
- Install the dependencies:
pip install -r requirements.txt
- Run the test. The actual pytest run is the
pytestcommand (which also enables parallel execution); the plainpythoninvocation runs the script directly:
python tests/lt_sample_todo.py
pytest -s -n=2 tests/lt_sample_todo.py
The test then appears on the Automation Dashboard. A green status confirms it passed.
Robot Framework uses Selenium2Library, with capabilities and the grid URL declared as variables in a .robot file, run through a Makefile.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/Robot-Selenium-Sample
cd Robot-Selenium-Sample
- Set your browser and OS in the variables block of
common.robot:
*** Settings ***
Library Selenium2Library
*** Variables ***
@{_tmp}
... browserName: %{browserName},
... platformName: %{platform},
... browserVersion: %{version},
... name: RobotFramework Lambda Test
${BROWSER} %{ROBOT_BROWSER}
${CAPABILITIES} ${EMPTY.join(${_tmp})}
${REMOTE_URL} https://${KEY}@hub.lambdatest.com/wd/hub
- Install the dependencies:
pip install -r requirements.txt
- Run a single test, or all in parallel:
make test_Windows_10_chrome_latest
make run_all_in_parallel
The test then appears on the Automation Dashboard. A green status confirms it passed.
Behave is BDD for Python: feature files plus step definitions, with capabilities in a JSON config.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/Python-Behave-Selenium
cd Python-Behave-Selenium
- Set your browser and OS in
config/config.json:
[
{
"platformName": "Windows 10",
"browserName": "chrome",
"browserVersion": "latest",
"build": "Behave Selenium Sample",
"name": "Behave Sample Test"
}
]
- Install the dependencies:
pip install -r requirements.txt
- Run the test:
behave features/test.feature
The test then appears on the Automation Dashboard. A green status confirms it passed.
Lettuce is unmaintained (since 2016, Python 2 only). For new projects, use the actively maintained Behave or pytest frameworks instead (see the Behave and pytest tabs).
Lettuce is a BDD framework for Python: feature files plus step definitions, with capabilities in a JSON config.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/sample-lettuce
cd sample-lettuce
-
Set your browser and OS in
config.json. -
Install the dependencies:
pip install -r requirements.txt
- Run the test:
python tests/lt_sample_todo.py
The test then appears on the Automation Dashboard. A green status confirms it passed.
View Your Results
Your test results, including video, network logs, and command-by-command execution, appear on the TestMu AI Automation Dashboard.
Next steps: If this is your first run, walk through running your first Selenium test end to end. From there, explore the full set of Selenium automation capabilities you can pass to the grid, learn how to debug your Selenium tests, and organize and filter your Selenium tests as your suite grows.
Next Steps
Continue with these related guides:
