0
0
Selenium Pythontesting~8 mins

Reading test data from CSV in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Reading test data from CSV
Folder Structure
selenium-python-project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── csv_reader.py
│   └── config/
│       └── config.yaml
├── test_data/
│   └── login_data.csv
├── requirements.txt
└── pytest.ini
  
Test Framework Layers
  • Driver Layer: Handles browser setup and teardown using Selenium WebDriver.
  • Page Objects: Classes representing web pages, encapsulating locators and actions.
  • Tests: Test scripts that use page objects and test data to perform validations.
  • Utilities: Helper modules like csv_reader.py to read CSV files and provide data.
  • Config: Configuration files (e.g., YAML) for environment settings, URLs, and credentials.
  • Test Data: CSV files storing test inputs, separated from code for easy updates.
Configuration Patterns

Use a config.yaml file to store environment URLs, browser choices, and credentials. Load this config in tests or fixtures.

Example config.yaml snippet:

environment:
  url: "https://example.com"
browser: "chrome"
credentials:
  username: "testuser"
  password: "password123"
  

Use Python's csv module in csv_reader.py to read test data from CSV files in test_data/. This keeps test data separate and easy to maintain.

Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html or allure-pytest for clear HTML reports.
  • Integrate tests in CI/CD pipelines (GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Reports help quickly see which tests passed or failed, including data-driven tests using CSV inputs.
Best Practices
  • Separate Test Data: Keep CSV files outside code to update test inputs without changing code.
  • Reusable CSV Reader: Create a utility function to read CSV and return data in a usable format (list of dicts).
  • Data-Driven Tests: Use pytest parameterization to run tests with multiple data rows from CSV.
  • Clear Naming: Name CSV columns clearly to match test parameters for easy mapping.
  • Handle Exceptions: Add error handling when reading CSV to avoid test crashes on bad data.
Self Check

Where would you add a new CSV file for test data about user registration?

Answer: In the test_data/ folder, as this folder holds all CSV test data files.

Key Result
Organize test data in CSV files under a dedicated folder and use utility functions to read them for data-driven Selenium tests.