himanshuseth004
Posted on February 1, 2022
Python is a programming language that needs no introduction! It is one of the most preferred languages when it comes to projects involving Machine Learning (ML), Artificial Intelligence(AI), and more. On a different battleground, the combination of Selenium Python is widely preferred as far as website automation is concerned.
As per Stack Overflow Developer Survey 2021, Python is the third-most-popular language after JavaScript & HTML/CSS. The prowess of Selenium and Python helps in automating interactions with the WebElements in the DOM (Document Object Model).
So how do you get started with Selenium and Python for Selenium automation testing? Well, this Selenium Python tutorial deep dives into the essential aspects of Selenium and Python, the learnings of which will be instrumental in your web automation journey.
In case you do not have prior expertise with Selenium or want to know more about what is Selenium, make sure to check out our detailed Selenium WebDriver tutorial to gain insights into the Selenium automation framework.
What are Selenium Python Bindings
Selenium Python bindings provide APIs using which you can write functional tests using the Selenium WebDriver. Like other Selenium language bindings, Selenium Python APIs can be leveraged to develop highly efficient tests that let you interact with the WebElements on the AUT (Application Under Test).
The bindings support local browser drivers (e.g., Chrome, Firefox, Internet Explorer, etc.) and provide a remote WebDriver that lets you connect to a remote cloud Selenium Grid.
How to configure Python and Selenium for Web Automation Testing
In this part of the Selenium Python tutorial, we cover the steps involved in setting up the development environment for Selenium automation testing with Python.
The prerequisite for Selenium Python installation is Python. So before proceeding with the further steps, make sure to install Python on your machine. As of writing this blog, the latest stable version of Python was 3.9.7.
Follow the below-mentioned steps to setup Selenium in Python:
Step 1: Install Python
Like me, if you have multiple versions of Python installed on your machine, you would need to use python3 for using Python 3.9.7. However, if you are on Mac, triggering the python command will use Python 2, whereas triggering the python3 command will use Python 3.9.7.
Step 2: Download and Install pip
To install Selenium Python, you need to make use of pip, the Package Management System for Python. Like Python 3, we would be installing pip3 so that we can use Python 3 for the development of the test scenarios.
Installing pip on Mac
- Run the below command on the terminal for downloading pip on your machine:
- Trigger the below command to install pip3 on your machine:
python3 get-pip.py
- Verify whether pip3 installation was successful by triggering the pip3 –version on the terminal.
pip3 --version
Installing pip on Windows
- For Windows, download get-pip.py on your local machine and run the below command to install pip:
python get-pip.py
- You can confirm whether pip installation is successful (or not) by running the below command on the terminal:
pip --version
Step 3: Download and install Selenium
Now that pip is installed successfully, it’s time to install Selenium in Python. First, the corresponding pip (or pip3) command is used for installing Selenium. Next, perform the below-mentioned steps depending on the operating system.
Installing Selenium on Mac
- Since we are using Python 3.9.7 for testing, we would be using the pip3 command for installing Selenium in Python. Trigger the pip3 install selenium command on the terminal:
pip3 install selenium
As seen above, Selenium 3.141 (the current stable version of Selenium) is successfully installed on the machine.
- Run the command pip freeze | grep “selenium” to verify the Selenium version installed on the machine. In my case, there are multiple Selenium versions present on the machine, but that should not cause any problem with web automation testing with Selenium Python.
pip freeze | grep "selenium"
Installing Selenium on Windows
- Run the command pip install -U selenium on the command prompt (or terminal) to install Selenium on Windows.
pip install -U selenium
- Now that Selenium for Python installation is complete, run the following command to verify the status of the installation:
python -c "import selenium; print(selenium.__version__)"
Shown below is the output of the above command, which indicates that the installation was successful.
Step 4: Install PyTest framework
PyUnit (or unittest), the default testing framework, is derived from the JUnit framework. Since it is available out-of-the-box, it can be leveraged for unit testing as well as Selenium test automation.
PyUnit, when used with Selenium, can also be used for leveraging parallel testing in Selenium. However, the biggest downside of PyUnit is the necessity of boilerplate code for the test implementation. Over & above, the framework also makes use of the camelCase naming convention that is popularly used in Java.
In case you are inquisitive to know more about the PyUnit framework for Python automation testing, make sure to check out our detailed PyUnit Selenium Python tutorial that covers all the aspects of the said framework. In this Selenium Python tutorial, we would be using the PyTest, a more widely used framework compared to PyUnit.
Installing PyTest on Mac
- Run the command pip3 install pytest for installing the latest version of pytest.
pip3 install pytest
As seen below, pytest-6.2.5 was successfully installed on the machine.
- Verify whether the corresponding version of the PyTest framework was installed successfully by triggering the following command pip freeze | grep “pytest” on the terminal.
pip freeze | grep "pytest"
Installing PyTest on Windows
- PyTest on Windows can be installed by triggering the pip install -U pytest command on the terminal.
pip install -U pytest
- Check whether PyTest installation was successful or not by running the command pytest –version on the terminal.
pytest --version
Step 5: Install Browser Drivers (Optional for Cloud Selenium Grid)
This step is only applicable for Selenium Python tests that have to be executed on the local Selenium Grid. The Selenium WebDriver architecture shows that Selenium client libraries interact with the web browser through its corresponding browser driver.
You need to download the browser driver(s) (in line with the browser and browser version) on the local machine for Selenium Python testing on a local Selenium. You should download and copy the browser driver to the location that houses the browser executable (e.g., Chrome.exe for the Chrome browser). By doing so, you would not be required to explicitly specify the browser driver path when instantiating the browser driver.
Shown below are the locations from where you can download the corresponding browser drivers:
Opera
https://github.com/operasoftware/operachromiumdriver/releases
Firefox
https://github.com/mozilla/geckodriver/releases
Chrome
http://chromedriver.chromium.org/downloads
Internet Explorer
https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
Microsoft Edge
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Since I would be performing the Selenium Python test on Chrome browser, I downloaded the Chrome Driver that matches the Chrome browser version. As shown below, I downloaded Chrome Driver (v 94.0.4606.61) from the link mentioned above.
It is recommended to place the browser driver (i.e., in our case, it is ChromeDriver) in the location where the browser executable is present.
For Mac
For Windows
With this, we are all set to run our first Selenium Python test on the local Selenium Grid. We would be using the PyTest framework for the demonstrations. Later in this Selenium Python tutorial, we will also cover how to run parallel tests on a cloud Selenium Grid (like LambdaTest) using the PyTest framework.
LambdaTest is a powerful cloud-based Selenium Grid, which makes it super simple to perform live interactive cross browser testing of your live and local websites and web apps on 2000+ online browsers running on a real operating system.
How to run Web Automation Tests using Selenium and Python
Now that we have covered the essentials related to setting up Selenium in Python, let’s get our hands dirty by testing some real test scenarios. In this Selenium Python tutorial, we would run two test scenarios on a local Selenium Grid. Here are the details:
Test Scenarios
Test Scenario – 1 (Browser – Firefox)
Navigate to the URL https://lambdatest.github.io/sample-todo-app/.
Select the first two checkboxes.
Send ‘Happy Testing at LambdaTest’ to the textbox with id = sampletodotext.
Click the Add Button and verify whether the text has been added or not.
Test Scenario – 2 (Browser – Chrome)
Navigate to the URL https://www.google.com.
Locate the search box.
Send ‘LambdaTest’ to the search box.
Click on the link that contains the LambdaTest HomePage URL.
Project Setup
Before we deep dive into the implementation, we will do a quick walk-through of the setup we are using to implement the scenarios. I have taken a leaf out of the following PyTest tutorial video to set up the base for implementing and running Selenium Python tests:
I am using Visual Studio (VS) Code IDE and Poetry Package Manager (instead of pip) since it comes with all the necessary tools to manage the project in a more deterministic manner.
However, you can use alternative Python IDEs like PyCharm, Atom, Sublime Text, etc., instead of VS Code. Since we have already done the installation of Selenium and PyTest using pip, we are good to go to perform Selenium testing in Python.
Follow the below-mentioned steps for creating a Selenium Python Project in VS Code:
Step 1
Create a folder named “python-tutorial,” which will contain the files and folders that could contain the test code.
Step 2 (Applicable only for Poetry Package Manager)
If the poetry package is not installed, please run the respective command to install the package:
- For Mac – pip3 install poetry
- For Windows – pip install poetry
Since we are using the poetry package manager, run the command poetry init on the VS terminal. This command creates the pyproject.toml configuration file. When creating the config file, choose the Python version available in your machine (i.e., Python 3.9).
We are installing the following packages using Poetry (via pyproject.toml):
- Selenium (v 3.141.59)
- PyTest (v 6.2.5)
- pytest-xdist (v 2.4.0)
- Autopep8
- Flake8
Autopep8 automatically formats Python code to conform to the PEP 8 style guide. Flake8 is a Python package that helps check the code base against coding style (PEP8), programming errors, etc. For installing the above packages, enter the relevant package name (e.g., selenium/pytest/autopep8/flake8).
Once all the packages are added to the TOML file, press Enter to complete the generation of the file.
To install the packages mentioned in the config file, run the command poetry install on the VS Code terminal. On completion, this will generate the files pyproject.toml and poetry.lock.
poetry install
In the further section of this Selenium Python tutorial, we look at the implementation of the two test scenarios.
Implementation (Test Scenario – 1)
#Implementation of Selenium WebDriver with Python using PyTest
import pytest
from selenium import webdriver
import sys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
def test_lambdatest_todo_app():
ff_driver = webdriver.Firefox()
ff_driver.get('https://lambdatest.github.io/sample-todo-app/')
ff_driver.maximize_window()
ff_driver.find_element_by_name("li1").click()
ff_driver.find_element_by_name("li2").click()
title = "Sample page - lambdatest.com"
assert title == ff_driver.title
sample_text = "Happy Testing at LambdaTest"
email_text_field = ff_driver.find_element_by_id("sampletodotext")
email_text_field.send_keys(sample_text)
sleep(5)
ff_driver.find_element_by_id("addbutton").click()
sleep(5)
output_str = ff_driver.find_element_by_name("li6").text
sys.stderr.write(output_str)
sleep(2)
ff_driver.quit()
Implementation (Test Scenario – 2)
#Implementation of Selenium WebDriver with Python using PyTest
import pytest
from selenium import webdriver
import sys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
def test_lambdatest_google():
chrome_driver = webdriver.Chrome()
chrome_driver.get('https://www.google.com')
chrome_driver.maximize_window()
if not "Google" in chrome_driver.title:
raise Exception("Could not load page")
element = chrome_driver.find_element_by_name("q")
element.send_keys("LambdaTest")
element.submit()
# Check if the LambdaTest Home Page is open
title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest"
lt_link = chrome_driver.find_element_by_xpath("//h3[.='LambdaTest: Most Powerful Cross Browser Testing Tool Online']")
lt_link.click()
sleep(5)
assert title == chrome_driver.title
sleep(2)
chrome_driver.quit()
Code Walkthrough
Since the Selenium Python APIs being used are more or less the same in both the test scenarios, we are doing a joint code walkthrough.
Step 1: Import the Selenium WebDriver, pytest, Keys, and other relevant classes from Selenium.
import pytest
from selenium import webdriver
import sys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
Keys class in Selenium Python lets you perform relevant keyboard actions on the WebElements in the DOM. PyTest in Selenium Python is primarily used for unit testing. The framework is pretty exhaustive (compared to the unittest framework) and provides a mechanism for realizing parameterized testing with Python.
Step 2: The time module in Selenium Python provides numerous ways of suspending the execution for a specified time duration. For demonstration, we are using the blocking sleep() function, which is not considered to be Selenium best practices.
from time import sleep
You can also use Selenium wait in Python to handle the loading of dynamic elements in a more efficient manner.
Step 3: Proper names (starting with test_) are assigned to the test methods in both the test files.
Test – 1
def test_lambdatest_todo_app():
Test – 2
def test_lambdatest_google():
Step 4: The next step is to create an instance of the respective browser (i.e., Firefox for Test-1 and Chrome for Test-2) to execute the tests. In both cases, we have downloaded the browser driver in the location where the browser executable is placed in both cases.
In case you are planning to run test on Safari, make sure to check out our detailed blog that deep dives into How to run Selenium tests on Safari using Safari Driver
Test – 1
ff_driver = webdriver.Firefox()
Test – 2
chrome_driver = webdriver.Chrome()
Step 5:
Test 1
The find element method in Selenium Python is used for locating the relevant WebElement using the NAME property. The URL under test is the LambdaTest ToDo Page.
The find element method in Selenium Python is used for locating the relevant WebElement using the NAME property. The URL under test is the LambdaTest ToDo Page.
ff_driver.find_element_by_name("li1").click()
ff_driver.find_element_by_name("li2").click()
This is how we located the WebElements (li1 and li2) using the Inspect tool in Chrome.
Test 2
Here the URL under test is the Google homepage. An exception is raised if the window title does not match the expected title.
if not "Google" in chrome_driver.title:
raise Exception("Could not load page")
element = chrome_driver.find_element_by_name("q")
The search box on the Google homepage is located using the find_element_by_name method. Here is how we fetch the details of the required WebElement using the Inspect tool in Chrome:
Step 6:
Test 1
title = "Sample page - lambdatest.com"
assert title == ff_driver.title
The current window title is compared with the expected title (i.e., Sample page – lambdatest.com). Assert is raised if the window title does not match the expected one. Other than this Selenium Python tutorial, you can check out our detailed blog on asserts in Selenium Python to get a deeper understanding of how to use assertions while performing Python automation testing.
Test 2
Send Keys in Selenium is used to input the search term (i.e., LambdaTest) in the search box that was located using the find_element_by_name() method.
element.send_keys("LambdaTest")
Submit method in Selenium is used for submitting the form in Selenium. There are other ways to achieve the same task but submit() is the most efficient of them all!
element.submit()
Step 7:
Test 1
A new item (i.e., Happy Testing at LambdaTest) is added to the ToDo list. Then, the required WebElement is located using the find_element method and the ID property.
sample_text = "Happy Testing at LambdaTest"
email_text_field = ff_driver.find_element_by_id("sampletodotext")
A new item is added to the ToDo list using the send_keys() method in Selenium.
email_text_field.send_keys(sample_text)
Test 2
The find element by XPath in Selenium is used for locating the relevant search result from Google. We are using the POM Builder, a Chrome AddOn that lets you extract the XPath of the desired WebElement with ease. You can also use SelectorsHub for Chrome to locate XPath, CSS Selector, and other web locators with a few clicks.
title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest"
lt_link = chrome_driver.find_element_by_xpath("//h3[.='LambdaTest: Most Powerful Cross Browser Testing Tool Online']")
Now that the relevant search link is located, the click command in Selenium is used to click and open the requisite page.
lt_link.click()
Step 8:
Test 1
We perform a check if the newly added item is successfully added to the ToDo list or not. The element li6 is located using the NAME property.
The text of the corresponding WebElement is read using the text method. getText method in Selenium is popularly used when you intend to read text attributes of the WebElement. In this case, the method will return the text attribute of the li6 element (i.e., Happy Testing at LambdaTest).
output_str = ff_driver.find_element_by_name("li6").text
The quit() method in Selenium is used to destroy the instance of the Firefox browser.
ff_driver.quit()
Test 2
Assert is raised if the current window title (i.e., LambdaTest homepage) does not match the expected title.
title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest"
assert title == chrome_driver.title
On completion of the test execution, the instance of the Chrome browser is destroyed using the quit() method.
chrome_driver.quit()
Test Execution
Since we have two test files (i.e., test_lambdatest_todo.py and test_lambdatest_google.py), we run the tests in serial by triggering the following command on the terminal
pytest -s tests/test_lambdatest_todo.py tests/test_lambdatest_google.py
This will serially execute the tests (i.e., non-parallel execution). This is not a recommended execution practice since it will elongate the execution time. It is definitely not recommended for large-scale projects!
Here is the screenshot of the test execution:
Test – 1 Execution
Test – 2 Execution
As seen below, the tests were executed successfully:
This certification is for professionals looking to develop advanced, hands-on expertise in Selenium automation testing with Python and take their career to the next level.
Here’s a short glimpse of the Selenium Python 101 certification from LambdaTest:
How to run Parallel Selenium Tests with Python
In this section of the Selenium Python Tutorial, we will see how to perform parallel testing using Python with Selenium.
By default, PyUnit and PyTest frameworks support only serial test execution (i.e., there is no out-of-the-box support for parallel test execution). Parallel execution in Selenium provides a number of benefits, the major ones are accelerated test execution, improved product quality, and faster time to market.
However, it is relatively easy to run Selenium Python tests in parallel using the PyTest framework. All you need to do is install the pytest-xdist plugin.
Here are the various execution modes supported by the pytest-xdist plugin:
- Multi-process load balancing – Multiple CPUs or hosts can be used for performing a parallel test run.
- LooponFail – Tests are repeatedly executed in a sub-process.
- Multi-platform coverage – Provides the flexibility to use multiple Python interpreters (e.g., PyTest, PyUnit, etc.) in the same test run.
Here are the steps to install pytest-xdist plugin to run Selenium Python tests in parallel:
- Mac: pip3 install pytest-xdist
- Windows: pip install pytest-xdist
As shown below, the pytest-xdist plugin provides a number of command-line options to leverage the available CPUs for running Selenium Python tests in parallel.
In our case, we would be utilizing 2 CPU cores (through -n option) to run the two test scenarios in parallel. Here the parallel execution is done at the file level (i.e., test methods in two files are running in parallel).
pytest -s <file_names> --verbose -n=<number_of_cpus>
where <"number_of_cpus"> = 2
Since we had already installed the pytest-xdist plugin through poetry, we did not run the pip command to install the plugin.
To run the two tests in parallel, run the following command on the terminal:
pytest -s tests/test_lambdatest*.py --verbose -n=2
As seen in the execution snapshot, both the tests are executing in parallel on the local Selenium Grid.
Both the tests were successfully executed in close to 19 seconds.
Other than this Selenium Python tutorial, if you need more information about parallel testing in Selenium Python, make sure to check out our detailed PyTest tutorial that deep-dives into the integral aspects of parallel execution with PyTest in Selenium.
How to run Selenium Python tests on Cloud Selenium Grid
In this Selenium Python tutorial, we have seen that running Selenium tests in Python on a local Selenium Grid is a good solution, only if the number of test scenarios and test combinations are less. For example, consider a scenario where you have a Mac machine, and you need to run cross browser tests on (Internet Explorer + Windows 10) combination.
Well, testing on Internet Explorer is still not a thing of the past! But, over & above, maintaining an in-house Grid infrastructure that houses machines with different browsers, browser versions, and platforms installed is a costly proposition.
This is where cloud Selenium testing can play a major role in running Selenium Python tests on a range of virtual browsers, browser versions, and operating systems. LambdaTest is one such platform that provides a scalable, secure, and reliable cloud-based Selenium Grid infrastructure that lets you run Selenium Python tests at scale.
From a developer’s perspective, you just need to migrate from a local Selenium WebDriver to Remote Selenium WebDriver. This essentially means only a few lines of code change, that too from an infrastructural point of view!
Create an account to get started with cloud Selenium Grid on LambdaTest. Once you have created a profile, note the username and access key from the LambdaTest profile section. Next, you can generate the desired browser capabilities by choosing the browser and OS options from the Selenium Capabilities Generator.
For demonstrating the use of cloud Selenium Grid for running Selenium Python tests in parallel, we will port the earlier tests so that they run on the following combinations:
Test Case – 1 (Chrome – 92, Windows 10)
- Navigate to the URL https://lambdatest.github.io/sample-todo-app/
- Select the first two checkboxes
- Send ‘Happy Testing at LambdaTest’ to the textbox with id = sampletodotext
- Click the Add Button and verify whether the text has been added or not
Test Scenario – 2 (Safari – 14, macOS Big Sur)
- Navigate to the URL https://www.google.com
- Locate the search box
- Send ‘LambdaTest’ to the search box
- Click on the link that contains the LambdaTest HomePage URL
Shown below is the implementation where the tests would run on the LambdaTest Selenium Grid instead of the local Selenium Grid:
Implementation (Test Scenario – 1)
#Implementation of Selenium WebDriver with Python using PyTest
import pytest
from selenium import webdriver
import sys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
saf_capabilities = {
"build" : "Porting test to LambdaTest Selenium Grid (Safari)",
"name" : "Porting test to LambdaTest Selenium Grid (Safari)",
"platform" : "MacOS Big sur",
"browserName" : "Safari",
"version" : "14.0"
}
def test_lambdatest_todo_app():
# ff_driver = webdriver.Firefox()
# Profile Link - https://accounts.lambdatest.com/detail/profile
user_name = "user_name"
app_key = "access_key"
# chrome_driver = webdriver.Chrome()
remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
ff_driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = saf_capabilities)
ff_driver.get('https://lambdatest.github.io/sample-todo-app/')
ff_driver.maximize_window()
ff_driver.find_element_by_name("li1").click()
ff_driver.find_element_by_name("li2").click()
title = "Sample page - lambdatest.com"
assert title == ff_driver.title
sample_text = "Happy Testing at LambdaTest"
email_text_field = ff_driver.find_element_by_id("sampletodotext")
email_text_field.send_keys(sample_text)
sleep(5)
ff_driver.find_element_by_id("addbutton").click()
sleep(5)
output_str = ff_driver.find_element_by_name("li6").text
sys.stderr.write(output_str)
sleep(2)
ff_driver.quit()
Implementation (Test Scenario – 2)
#Implementation of Selenium WebDriver with Python using PyTest
import pytest
from selenium import webdriver
import sys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
ch_capabilities = {
"build" : "Porting test to LambdaTest Selenium Grid (Firefox)",
"name" : "Porting test to LambdaTest Selenium Grid (Firefox)",
"platform" : "Windows 10",
"browserName" : "Chrome",
"version" : "92.0"
}
def test_lambdatest_google():
# Profile Link - https://accounts.lambdatest.com/detail/profile
user_name = "user_name"
app_key = "access_key"
# chrome_driver = webdriver.Chrome()
remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
chrome_driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = ch_capabilities)
chrome_driver.get('https://www.google.com')
chrome_driver.maximize_window()
if not "Google" in chrome_driver.title:
raise Exception("Could not load page")
element = chrome_driver.find_element_by_name("q")
element.send_keys("LambdaTest")
element.submit()
# Check if the LambdaTest Home Page is open
title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest"
lt_link = chrome_driver.find_element_by_xpath("//h3[.='LambdaTest: Most Powerful Cross Browser Testing Tool Online']")
lt_link.click()
sleep(5)
assert title == chrome_driver.title
sleep(2)
chrome_driver.quit()
Code Walkthrough
As the core functional logic is unchanged, we would just focus on the changes we did to run the Selenium Python tests on LambdaTest Grid.
Here are the browser capabilities (generated using the LambdaTest Capabilities Generator) for the two test scenarios.
Test – 1
saf_capabilities = {
"build" : "Porting test to LambdaTest Selenium Grid (Safari)",
"name" : "Porting test to LambdaTest Selenium Grid (Safari)",
"platform" : "MacOS Big sur",
"browserName" : "Safari",
"version" : "14.0"
}
Test – 2
ch_capabilities = {
"build" : "Porting test to LambdaTest Selenium Grid (Firefox)",
"name" : "Porting test to LambdaTest Selenium Grid (Firefox)",
"platform" : "Windows 10",
"browserName" : "Chrome",
"version" : "92.0"
}
Instead of the local Selenium Grid, the tests are executed on the cloud-based Selenium Grid on LambdaTest. Hence, the combination of user-name & access-key (available in the profile section) are used for accessing the Remote Selenium Grid (i.e., @hub.lambdatest.com/wd/hub).
The RemoteWebDriver API uses the remote URL and browser capabilities generated using the capabilities generator.
Test – 1
ff_driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = saf_capabilities)
Test – 2
chrome_driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = ch_capabilities)
The remaining part of the implementation remains unchanged. That’s all in this Selenium Python tutorial, we are all set to perform the parallel execution on the cloud Selenium Grid.
Execution
Run the following command on the terminal to run Selenium Python tests in parallel on the LambdaTest Grid.
pytest -s tests/test_lambdatest*.py --verbose -n=2
To check the execution status, visit the Automation Dashboard on LambdaTest. As seen below, the two tests are running in parallel on the cloud Selenium Grid. Since my current plan supports five parallel sessions, I could have run five parallel tests (if required).
As seen below, both the Selenium tests in Python were executed successfully on the LambdaTest Grid.
It’s a Wrap
Python is one of the widely used languages, and it is growing up the ranks with each passing year. Though PyUnit is the default test automation framework, a majority crowd prefer the PyTest framework for performing Python automation testing.
Also, it is relatively easy to run Selenium Python tests in parallel using the pytest-xdist plugin. In this exhaustive Selenium Python tutorial, we deep-dived into the integral aspects of running cross browser tests with Selenium in Python.
The potential of Python and Selenium can be truly exploited by performing Python automation testing on a cloud Selenium Grid (like LambdaTest). Furthermore, this approach helps in improving the product quality by increasing the overall browser coverage.
I hope you find this detailed Selenium Python tutorial useful! Also, do drop in your comments about the common challenges that you normally encounter when it comes to Selenium automation testing!
Happy Testing 🙂
Posted on February 1, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.