Using CSV Files for Test Data in Selenium Python

In automation testing with Selenium using Python, managing test data efficiently is key to building scalable and reusable test frameworks. One of the simplest and most popular ways to handle structured test data is by using CSV (Comma-Separated Values) files. CSV files provide a lightweight, human-readable format that’s easy to create, modify, and parse in Python.

In this blog, we’ll explore how to use CSV files for test data in Selenium Python projects—why it’s useful, how to implement it, and best practices for organizing your test data.


Why Use CSV Files for Selenium Testing?

Simplicity: CSV files are plain text files that are easy to create and edit with any text editor or spreadsheet tool like Excel or Google Sheets.

Separation of Code and Data: Storing test data externally keeps your test scripts clean and flexible.

Data-Driven Testing: Easily run the same test logic with multiple sets of data.

Version Control Friendly: Unlike binary files, CSV files are easily trackable in Git or other version control systems.


Sample Test Scenario

Let’s say we are testing a login form that requires a username and password. Instead of hardcoding credentials in the test script, we’ll store them in a CSV file.

Sample CSV (testdata.csv):

pgsql


username,password

testuser1,pass123

testuser2,wrongpass

adminuser,admin@123

Reading CSV Data in Python

Python’s built-in csv module allows for easy reading of CSV files. Here’s a basic example:


python

Copy

Edit

import csv


def read_csv_data(filepath):

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

        data = csv.DictReader(csvfile)

        return [row for row in data]

This function returns a list of dictionaries, each representing a row in the CSV file.


Using CSV Data in Selenium Test

Here’s how you can integrate CSV data with a Selenium login test:


python

Copy

Edit

from selenium import webdriver

from selenium.webdriver.common.by import By

import time

import csv


def read_csv_data(filepath):

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

        data = csv.DictReader(csvfile)

        return [row for row in data]


def login_test(data):

    driver = webdriver.Chrome()

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


    for row in data:

        username = row['username']

        password = row['password']


        driver.find_element(By.ID, 'username').clear()

        driver.find_element(By.ID, 'username').send_keys(username)


        driver.find_element(By.ID, 'password').clear()

        driver.find_element(By.ID, 'password').send_keys(password)


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

        time.sleep(2)  # Wait to observe result (should be replaced with WebDriverWait)


        print(f"Tested login with: {username} / {password}")

        driver.refresh()


    driver.quit()


if __name__ == "__main__":

    data = read_csv_data("testdata.csv")

    login_test(data)


Best Practices

Use Headers in CSV: Makes parsing with DictReader easier and the data more readable.

Use Relative Paths: Keep test data in a dedicated folder like ./testdata/testdata.csv.

Avoid Hardcoded Waits: Use WebDriverWait instead of time.sleep() for more stable tests.

Modularize Your Code: Separate test logic, data reading, and browser actions for cleaner structure.

Handle Errors Gracefully: Add try-except blocks for better test resilience.


Conclusion

Using CSV files for test data in Selenium Python is a practical and efficient way to implement data-driven testing. It enhances test coverage without duplicating code and allows non-developers to contribute to test scenarios by editing CSV files. Whether you’re testing login forms, registration, or search functionalities, CSV data integration can significantly streamline your testing workflow. 

Learn Selenium with Pyhton Training Hyderabad

Read More:  Creating Page Object Model (POM) with Python
Read More:  Logging in Selenium Python Scripts
Read More:  Generating Test Reports in Selenium Python

Visit IHUB Talent Institute Hyderabad
Get Direction

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Installing Java and Eclipse IDE for Selenium Automation

How Flutter Works Behind the Scenes