Creating a Data-Driven Framework with Selenium and Python
Creating a Data-Driven Framework with Selenium and Python
A data-driven framework allows you to run the same test with multiple sets of input data, improving test coverage and maintainability.
Steps to Build a Data-Driven Selenium Framework in Python
1. Set Up Your Environment
Install Selenium:
bash
Copy
Edit
pip install selenium
Install pandas or openpyxl to read data files (CSV, Excel):
bash
Copy
Edit
pip install pandas openpyxl
2. Prepare Test Data
Create a data file (e.g., testdata.xlsx) with columns like:
username password expected_result
user1 pass1 success
user2 pass2 failure
3. Read Data in Python
Use pandas to load data:
python
Copy
Edit
import pandas as pd
data = pd.read_excel('testdata.xlsx')
4. Write Selenium Test Script
Here’s an example of a data-driven test for a login page:
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
import pandas as pd
import time
# Load data
data = pd.read_excel('testdata.xlsx')
# Set up WebDriver (example with Chrome)
driver = webdriver.Chrome()
def login_test(username, password, expected):
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, 'submit').click()
time.sleep(2) # wait for page to load
# Example validation
if expected == 'success':
assert "Dashboard" in driver.title
else:
error = driver.find_element(By.ID, 'error').text
assert "Invalid credentials" in error
# Run tests for all data rows
for index, row in data.iterrows():
login_test(row['username'], row['password'], row['expected_result'])
driver.quit()
5. Organize Your Framework
Test Data: Store Excel/CSV files in a /data folder.
Test Scripts: Write reusable functions and test cases in /tests.
Utilities: Helpers for reading data, logging, reporting, etc.
Configuration: Store URLs, browser settings in a config file.
6. Enhance with pytest for Better Reporting
Install pytest:
bash
Copy
Edit
pip install pytest
Example test case with parameterization:
python
Copy
Edit
import pytest
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
data = pd.read_excel('testdata.xlsx')
@pytest.fixture(scope="module")
def driver():
driver = webdriver.Chrome()
yield driver
driver.quit()
@pytest.mark.parametrize("username,password,expected", data.values)
def test_login(driver, username, password, expected):
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, 'submit').click()
time.sleep(2)
if expected == 'success':
assert "Dashboard" in driver.title
else:
error = driver.find_element(By.ID, 'error').text
assert "Invalid credentials" in error
Run tests with:
bash
Copy
Edit
pytest test_login.py -v
Summary
Use external data files (Excel/CSV) to drive tests.
Read test data using pandas.
Create reusable Selenium functions to perform UI actions.
Integrate with pytest for parameterized tests and reporting.
Structure your framework for maintainability and scalability.
Learn Selenium Python Training in Hyderabad
Read More
Using unittest Framework with Selenium in Python
How to Read Data from Excel or CSV for Selenium Test Automation
Selenium with Python for Testing Login Pages
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment