Selenium is a helpful tool that automates cross-browser testing for web-based applications. Parameterization in Selenium refers to passing different values or data to test scripts or methods at runtime. It allows us to execute the same test case multiple times with varying input values, making our tests more versatile and reusable.
There are several ways to achieve parameterization in Selenium using Python. Here are two commonly used methods:
Using data-driven testing: This approach involves storing test data in external files (e.g., CSV, Excel, JSON) and reading the data from these files during test execution. We can use libraries, such as csv
, openpyxl
, or json
in Python, to handle the file reading. Here's an example using a CSV file:
import csvfrom selenium import webdriver# Read data from CSV filewith open('testdata.csv', 'r') as file:reader = csv.reader(file)next(reader) # Skip header rowfor row in reader:username = row[0]password = row[1]# Set up WebDriverdriver = webdriver.Chrome()# Perform actions with parameterized values# Note: Replace https://example.com with an actual websitedriver.get('https://example.com')driver.find_element_by_id('username').send_keys(username)driver.find_element_by_id('password').send_keys(password)# Perform assertions, further actions, etc.# Close the browserdriver.quit()
Using test frameworks: Test frameworks, such as pytest
and unittest
have built-in features for parameterization. We can leverage these features. These frameworks provide decorators or special methods to define test cases with different input values. Here's an example using pytest
:
import pytestfrom selenium import webdriver@pytest.mark.parametrize('username, password', [('user1', 'pass1'),('user2', 'pass2'),('user3', 'pass3')])def test_login(username, password):# Set up WebDriverdriver = webdriver.Chrome()# Perform actions with parameterized values# Note: Replace https://example.com with an actual websitedriver.get('https://example.com')driver.find_element_by_id('username').send_keys(username)driver.find_element_by_id('password').send_keys(password)# Perform assertions, further actions, etc.# Close the browserdriver.quit()
Let’s look at an example of using the pytest
framework to achieve parameterization using Python and Selenium.
Note: Click the “Run” button below to execute the code. After clicking the “Run” button, wait for Google Chrome to launch within the “Output” tab. Once Google Chrome has been launched, open another terminal and execute the following command to test parameterization using the
pytest
framework:cd /usercode && bash script.sh
<!DOCTYPE html> <html> <head> <title>Web Server</title> </head> <body> <form method="POST"> <label for="num1">Enter num 1:</label> <input type="number" id="num1" name="num1" required><br><br> <label for="num2">Enter num 2:</label> <input type="number" id="num2" name="num2" required><br><br> <button type="submit" id="submit">Submit</button> </form> {% if sum_result %} <p>Sum: {{ sum_result }}</p> {% endif %} </body> </html>
In the coding playground above:
In the templates/index.html
file:
In the server.py
file:
templates/index.html
file.In the script.sh
file:
pytest
command. Both commands will be split into two terminal panes using Tmux.In the test_addition.py
file:
driver
using the module level scope. This function initializes the Chrome WebDriver and waits for the parameterization test cases to be completed. Once completed, the driver quits.test_addition
and parameterize it with different test cases. This function will run as often as the number of test cases defined.Free Resources