Data-Driven Testing with Selenium and Python

 Data-driven testing (DDT) is a powerful technique in test automation that allows testers to run the same test with multiple sets of input data. This approach helps improve test coverage and efficiency, especially in scenarios where the same functionality must be validated with different inputs. In Selenium with Python, data-driven testing can be achieved using data sources like Excel files, CSV files, or even databases.

Why Use Data-Driven Testing?

Manual input of test data for each test case is time-consuming and error-prone. With DDT, you can:

  • Reduce redundancy by writing one test script for all data sets
  • Increase test coverage by testing more scenarios
  • Maintain test scripts easily by separating logic and data

How to Implement Data-Driven Testing in Selenium with Python

Let’s walk through a simple example using Selenium, Python, and data from a CSV file.

 Prerequisites

Install the required packages:

pip install selenium

Prepare a sample CSV file (test_data.csv) with the following content:

username,password

user1,pass1

user2,pass2

Sample Code

import csv

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.chrome.options import Options

# Read data from CSV

def read_test_data(file_path):

    with open(file_path, newline='') as csvfile:

        data = list(csv.DictReader(csvfile))

    return data

# Setup Selenium

options = Options()

options.add_argument('--headless')  # Optional headless mode

driver = webdriver.Chrome(options=options)

# Load data

test_data = read_test_data('test_data.csv')

# Loop through test data

for data in test_data:

    driver.get("https://example.com/login")

    driver.find_element(By.NAME, "username").send_keys(data['username'])

    driver.find_element(By.NAME, "password").send_keys(data['password'])

    driver.find_element(By.ID, "loginBtn").click()

    # Example verification (can be adjusted)

    print(f"Tested login with: {data['username']}/{data['password']}")

driver.quit()

Best Practices

  • Use external libraries like openpyxl for Excel files
  • Keep data and logic separate for easier maintenance
  • Add assertions to validate outcomes

Conclusion

Data-driven testing with Selenium and Python provides a flexible and scalable way to automate tests across a wide range of inputs. By reading data from external sources and looping through test cases, you can save time, reduce errors, and ensure your application is thoroughly tested under different conditions.

Learn Selenium Python Training in Hyderabad

Read More:

Handling Alerts and Pop-ups Using Selenium Python

Automating Login Pages with Selenium Python

Best Practices in Selenium Automation with Python

How to Automate File Uploads and Downloads

Visit our IHub Talent Training Institute

Get Direction

Comments

Popular posts from this blog

SoapUI for API Testing: A Beginner’s Guide

Automated Regression Testing with Selenium

Containerizing Java Apps with Docker and Kubernetes