The internet is everywhere. Web and mobile properties have a hefty influence on consumer purchase decisions. Therefore, more and more businesses are moving towards providing a quality digital first experience for their end users. In simple terms, people are building many web and mobile apps. According to the Statista Report, there are more than 1.88 billion websites on the Internet, and the number is growing.
With quality being such an essential aspect of the success of a business, organizations are investing more and more into building up QA practices internally, and Automation Testing is at the forefront of this transformation.
So, as a developer or tester, how does that concern you? In today's job market, having automation abilities on your CV is quite appealing. It is also quite quick to get started with. The best framework to get started with automation testing is Selenium. It supports all programming languages and has the largest market share among automation testing frameworks.
In this article, we will look at what Selenium is, how to set up Selenium, and how to write an automated test script with Python language.
Next, we will see how to use a testing framework like pytest and perform parallel test execution on the cloud using LambdaTest.
Table of Contents
What is Selenium?
Selenium is a popular automated testing framework for web application browser automation. In a recent survey on test automation, 81% of the participants said they utilized Selenium to automate testing.
It allows website or web application testing across numerous operating systems and browsers. It also supports many programming languages, such as Python, C#Java, JavaScript(Node.js), Ruby, and more, allowing testers to automate their website testing in the most familiar language.
The Selenium framework has three components, as shown in the image below.
Selenium IDE
Selenium IDE (Integrated Development Environment) provides a record and playback tool that will generate scripts based on actions performed on a browser. It provides a user-friendly interface that allows testers and developers to easily export automated test scripts in programming languages like Java, Python, Ruby, C#, and many more. It is an ideal tool for beginners, requiring almost no prior programming knowledge. However, it is unable to create more complex test cases. For that, Selenium WebDriver and Selenium Grid can be used.
Selenium WebDriver
Selenium WebDriver offers a reliable and object-oriented method of automating browsers. Writing automation scripts in programming languages like Java, C#, Python, Ruby, and JavaScript is possible with Selenium WebDriver. By interacting with the browser directly, it may programmatically control it. Numerous browsers, including Chrome, Firefox, Safari, Microsoft Edge, and others, are supported by WebDriver.
Selenium Grid
Selenium Grid is also classified as a Local Grid that enables parallel test execution by allowing you to split test execution across several physical or virtual machines. You can simultaneously run tests on several browsers, operating systems, and devices, thanks to its integration with Selenium WebDriver. Several nodes and a hub make up the Selenium Grid. Tests are distributed to the available nodes for execution on the Hub, which serves as a central location for test registration.
How to Setup Selenium with Python?
Setting up Selenium with Python is relatively easy. You can set it up without any hassle by following the steps provided in this section.
Prerequisites
- The latest version of Python.
- Web browser on which you wish to run your test.
Using the Python package manager pip
, you may install Selenium by entering the following command at a command line or terminal:
pip install selenium
Console Output
To verify the installation of Selenium, you can run the following code:
import selenium
print(selenium.__version__)
Console Output
Writing First Automation Test Script with Selenium
Most of us created a sample to-do app as our first project, so why not create a test for it? In this section, we will create two simple yet practical difficulties, one of which is to get the website's title, and the other is to add an item to a to-do list.
Getting Website Title
In this test, we will print the title, which we will retrieve from the website. For this test, we will use the Chrome browser to execute our test.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://lambdatest.github.io/sample-todo-app/")
website_title = driver.title
print("Title:", website_title)
driver.quit()
Console Output
Code Walkthrough
The import statement is used to get WebDriver from the Selenium package.
It is used to initialize a new instance of the Chrome WebDriver.
get()
: This is a method provided by the WebDriver object, which is used to navigate to a specific URL. It instructs the web browser to open the specified URL.
title
: The title method is used to retrieve the title of the webpage the user is currently working on.
quit()
: This method quits the entire browser session with all its tabs and windows.
In automation, interacting with input fields and buttons is a regular task. Right-click anywhere on the webpage and choose inspect element to view the element’s details. Hover over the element you want to access.
Using the find_element()
method to identify the input element, you may imitate keyboard input using the send_keys()
method. You can simulate user input by filling the input field with the desired data by giving the selected text or value as an argument. To use the button, you need to use the find_element()
method to find the button and click()
method to press it. With the help of the below test, we will see how to use these methods.
Adding an item to a to-do list
In this test, we will add Drinking Water to the to-do list. For this test, we are going to use the Firefox browser to execute our test.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("https://lambdatest.github.io/sample-todo-app/")
search_for_key = driver.find_element(By.ID, "sampletodotext")
search_for_key.send_keys("Drinking Water")
search_btn = driver.find_element(By.ID, "addbutton")
search_btn.click()
driver.quit()
Output
Code Walkthrough
To use By
, the following import statement needs to be imported:
Initialize the Firefox browser.
Get the element using find_element()
method.
send_keys()
: It is used to send text to any field, such as the input field of a form.
click()
: This method will click the button.
How to Setup Selenium with Python Testing Framework?
The testing framework offers an organized and effective method of testing that makes it essential for software development. It provides a methodical technique to arrange, automate, and manage tests, making finding and addressing coding errors simpler.
Testing frameworks allow for test automation, make it easier to organize test cases, produce insightful reports, and guarantee the consistency and dependability of tests. They are crucial resources for preserving code quality, confirming functionality, and providing users with trustworthy software.
Python provides testing frameworks like Unittest, pytest, Behave, and Robot, which are readily used. To start with, we are going to rewrite our previous test in pytest.
First, we need to install pytest using the following command.
pip install pytest
Console Output
We need to convert our test into a function like the one below.
from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest
def test_get_website_title():
driver = webdriver.Chrome()
driver.get("https://lambdatest.github.io/sample-todo-app/")
website_title = driver.title
driver.quit()
assert "Sample page - lambdatest.com" in website_title
def test_add_todo_item():
driver = webdriver.Firefox()
driver.get("https://lambdatest.github.io/sample-todo-app/")
search_for_key = driver.find_element(By.ID, "sampletodotext")
search_for_key.send_keys("Drinking Water")
search_btn = driver.find_element(By.ID, "addbutton")
search_btn.click()
driver.quit()
if __name__ == "__main__":
pytest.main()
This script has two test functions, test_get_website_title()
and test_add_todo_item()
, each representing a test case. pytest will automatically discover and execute these tests.
Output
Pytest will find the tests and run them, giving you thorough output that includes a pass/fail status. You can use pytest's testing features while automating Selenium's web testing chores by combining Selenium with pytest.
Using the pytest Framework for Parallel Test Execution
When you have a big suite of tests, parallel testing can drastically shorten the time it takes to run each test. You can utilize pytest's built-in parallel test execution functionality to run the specified code in parallel.
Consider an example where we need to execute 2 test cases, which will take 7-8 seconds. In stark contrast, if we do it using Parallel Test Execution, the time will be 3-4 seconds. Here is how to do it:
Install the pytest-xdist Plugin
To allow parallel test running, you must install the pytest-xdist
plugin. With pip
, you can:
pip install pytest-xdist
Console Output
Now, run the script using the following command.
pytest main.py -n 2
The pytest-xdist
plugin offers the option -n 2 for parallel test running. It states you wish to use two parallel processes to conduct the tests. This indicates that pytest will divide the tests and run them concurrently on two CPU cores, which can speed up the execution of your test suite.
Without Parallel Test Execution
With Parallel Test Execution
As you can see, the whole test script executed in 3.73s, almost half compared to the above executed test, which took 7.88s.
Challenges of Testing on the Local Grid
Many challenges can arise while testing on a local grid. More readily available physical devices and browsers are one of the significant problems. A local grid makes it challenging to test across various device and browser configurations, which could negatively affect test coverage as a whole.
It also needs a lot of resources, including infrastructure setup, software, and hardware, to maintain and manage. It can take time and money to scale the grid to handle rising test demand.
Cloud-based testing grids address the problems with local grids. They provide various devices and browsers, enabling scalable and flexible testing. Testers can use different configurations, run tests concurrently for quicker execution, and increase coverage. Grids that are based in the cloud do not require managing local infrastructure.
AI-powered test orchestration and execution platforms like LambdaTest provide a cloud grid where you can run tests with automated testing tools like Selenium with Python. They make it simple to write and execute automation scripts with well-known test frameworks like pytest and Unittest because they support numerous Chrome browser versions and operating systems.
Running Test Case on a Cloud-Based Testing Grid
In this section, we will run a test on LambdaTest using pytest as the framework. We will test on the Windows 10 operating system with Chrome version 114.0.
Before running a Python test on LambdaTest, follow the simple steps.
- Create a LambdaTest account and complete all the required processes.
- Go to the LambdaTest Dashboard. To get your credentials, navigate to your Profile avatar in the top right corner.
- Then go to Account Settings > Password & Security. You can find your Username and Access Key and save it for future use.
Understanding the Code Step by Step
Shown below is the test script of a sample to-do-application.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.by import By
import pytest
username = "YOUR USERNAME"
access_key = "YOUR ACCESS KEY"
def test_get_website_title_on_lambdatest():
lt_options = {
"browserName": "Chrome",
"platformName": "macOS Ventura",
"project": "First Test",
"build": "Testing to-do list",
"name": "Get Website Title Test",
}
remote_url = "http://{}:{}@hub.lambdatest.com/wd/hub".format(username, access_key)
browser_options = ChromeOptions()
browser_options.set_capability("LT:Options", lt_options)
driver = webdriver.Remote(command_executor=remote_url, options=browser_options)
driver.get("https://lambdatest.github.io/sample-todo-app/")
website_title = driver.title
driver.quit()
assert "Sample page - lambdatest.com" in website_title
def test_add_todo_item_on_lambdatest():
lt_options = {
"browserName": "Firefox",
"platformName": "macOS Ventura",
"project": "First Test",
"build": "Testing to-do list",
"name": "Add Todo Item Test",
}
remote_url = "http://{}:{}@hub.lambdatest.com/wd/hub".format(username, access_key)
browser_options = FirefoxOptions()
browser_options.set_capability("LT:Options", lt_options)
driver = webdriver.Remote(command_executor=remote_url, options=browser_options)
driver.get("https://lambdatest.github.io/sample-todo-app/")
search_for_key = driver.find_element(By.ID, "sampletodotext")
search_for_key.send_keys("Drinking Water")
search_btn = driver.find_element(By.ID, "addbutton")
search_btn.click()
driver.quit()
if __name__ == "__main__":
pytest.main()
In the above script, add your LambdaTest credentials (Username and Access Key) in the above test script or set them in your Environment Variables, as it will help the LambdaTest run tests on your account.
Get your desired capabilities generated from the LambdaTest capabilities generator.
Run your first Python test
Find details of your test case under Automation > Web Automation.
You can also explore other available options to get a better idea of the LambdaTest platform.
Running Test Case on a Cloud Grid with Parallel Test Execution
To execute the test in parallel, you must run the same command we did for the local grid.
pytest main.py -n 2
As you can see, both the tests run simultaneously on LambdaTest.
Conclusion
Selenium is a robust set of technologies for automating online browser interactions. We discovered how to combine Python and Selenium and put our first test into practice. We also discussed the benefits of using testing frameworks like pytest and running tests concurrently to increase efficiency.
We also highlighted the difficulties of using a local grid for testing and the advantages of using a cloud-based automation testing platform, like LambdaTest, which offers a scalable and resource-effective option. Testers may accelerate test execution and increase test coverage with Selenium, cloud-based grids, and parallel execution, thereby raising the caliber of their web applications.
In conclusion, Selenium offers a thorough and effective online automation and testing method combined with Python, contemporary testing frameworks, cloud-based grids, and parallel execution. It gives testers the tools to handle problems and produce high-quality software.