What is parameterization in Selenium?

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 csv
from selenium import webdriver
# Read data from CSV file
with open('testdata.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header row
for row in reader:
username = row[0]
password = row[1]
# Set up WebDriver
driver = webdriver.Chrome()
# Perform actions with parameterized values
# Note: Replace https://example.com with an actual website
driver.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 browser
driver.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 pytest
from selenium import webdriver
@pytest.mark.parametrize('username, password', [
('user1', 'pass1'),
('user2', 'pass2'),
('user3', 'pass3')
])
def test_login(username, password):
# Set up WebDriver
driver = webdriver.Chrome()
# Perform actions with parameterized values
# Note: Replace https://example.com with an actual website
driver.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 browser
driver.quit()

Example

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>
Implementation of parameterization using the pytest framework

Explanation

In the coding playground above:

  • In the templates/index.html file:

    • Lines 1–18: We define the HTML template for our web page.
  • In the server.py file:

    • Lines 1–18: We use the Flask framework to create a web application that renders the HTML template defined inside the templates/index.html file.
  • In the script.sh file:

    • Lines 1–29: We define the script to launch the server and execute the pytest command. Both commands will be split into two terminal panes using Tmux.
  • In the test_addition.py file:

    • Lines 1–10: We import the required libraries.
    • Lines 16–24: We define a fixture function 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.
    • Lines 28–57: We define a test function test_addition and parameterize it with different test cases. This function will run as often as the number of test cases defined.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved