Data-Driven Testing using Selenium Python and Excel
In the world of test automation, data-driven testing is a powerful approach that allows you to run the same test logic against multiple sets of data inputs. This reduces redundancy, improves coverage, and ensures your tests are scalable and maintainable. When combined with Selenium and Python, and powered by data from tools like Excel, you can create flexible and robust test suites.
In this blog, we'll explore how to implement data-driven testing in Selenium Python using Excel files as the source of input data.
What is Data-Driven Testing?
Data-driven testing (DDT) is a software testing method where test scripts read input and expected output values from external data sources like Excel, CSV, or databases. The main advantage of DDT is separation of test logic and test data, which improves reusability and simplifies updates.
Instead of writing multiple test cases with different data, you write one test case and run it with multiple datasets.
Why Use Excel as a Data Source?
Excel is widely used for data entry and reporting, making it a convenient choice for testers and stakeholders. It allows:
Easy editing and storage of test inputs
Structured data representation
Integration with tools like openpyxl or pandas in Python
Setting Up the Environment
Before you begin, install the required packages:
bash
pip install selenium openpyxl
Also, ensure you have the proper web driver (like ChromeDriver) configured for Selenium.
Reading Data from Excel Using openpyxl
Assume you have an Excel file testdata.xlsx with the following content in Sheet1:
Username Password
user1 pass123
user2 pass456
Use the following code to read it:
python
from openpyxl import load_workbook
def read_excel_data(path):
workbook = load_workbook(filename=path)
sheet = workbook.active
data = []
for row in sheet.iter_rows(min_row=2, values_only=True):
data.append(row)
return data
Integrating Excel Data with Selenium Tests
Here's how you can combine Excel data with Selenium to perform login tests:
python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
def login_test(username, password):
driver = webdriver.Chrome()
driver.get("https://example.com/login")
driver.find_element(By.ID, "username").send_keys(username)
driver.find_element(By.ID, "password").send_keys(password)
driver.find_element(By.ID, "loginBtn").click()
time.sleep(2)
title = driver.title
if "Dashboard" in title:
print(f"[PASS] Login successful for user: {username}")
else:
print(f"[FAIL] Login failed for user: {username}")
driver.quit()
# Run the test for each data row
excel_data = read_excel_data("testdata.xlsx")
for user, pwd in excel_data:
login_test(user, pwd)
Benefits of This Approach
Reusability: One test function works for all data sets.
Maintainability: Test data changes don't require script changes.
Scalability: Easily scale to hundreds of test inputs.
Business-friendly: Stakeholders can modify test data in Excel.
Conclusion
Data-driven testing with Selenium Python and Excel allows for efficient, scalable, and maintainable test automation. By externalizing test inputs, you reduce code duplication and increase the flexibility of your tests. Whether you're testing login forms, search functionality, or form submissions, integrating Excel with Selenium using Python offers a practical and professional approach to automated testing.
Learn Selenium with Python Training Course
Read More: Validating Page Titles in Selenium Python
Read More: Automating Login and Logout with Selenium Python
Read More: Reading and Writing Cookies in Selenium Python
Visit Quality Thought Training Institute in Hyderabad
Get Direction
Comments
Post a Comment