0
0
Selenium Pythontesting~8 mins

Reading test data from JSON in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Reading test data from JSON
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   └── test_checkout.py
├── pages/
│   ├── login_page.py
│   └── checkout_page.py
├── data/
│   └── test_data.json
├── utils/
│   └── json_reader.py
├── config/
│   └── config.yaml
├── drivers/
│   └── chromedriver.exe
└── requirements.txt

This structure keeps test data in the data/ folder as JSON files. Utility code to read JSON is in utils/json_reader.py. Tests use this data to run with different inputs.

Test Framework Layers
  • Driver Layer: Manages browser drivers (e.g., ChromeDriver) to control browsers.
  • Page Objects: Classes representing web pages with methods to interact with page elements.
  • Tests: Test scripts that use page objects and test data to perform checks.
  • Utilities: Helper functions like JSON readers to load test data from files.
  • Configuration: Settings for environments, browsers, and credentials stored in config files.
Configuration Patterns
  • Environment Config: Use config/config.yaml to store URLs, browser types, and credentials.
  • Test Data: Store test inputs in JSON files inside data/ folder.
  • Loading Data: Use a utility function in utils/json_reader.py to read JSON and return Python dictionaries.
  • Parameterization: Tests read data from JSON and run multiple times with different inputs.
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML reports.
  • Integrate tests in CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Reports include which test data inputs passed or failed, helping track data-driven test results.
Best Practices
  • Separate Test Data: Keep test data outside test code for easy updates without code changes.
  • Reusable JSON Reader: Create a single utility function to read JSON files to avoid duplication.
  • Clear Naming: Name JSON keys clearly to match test input fields for easy mapping.
  • Parameterize Tests: Use test frameworks' parameterization features to run tests with multiple data sets.
  • Keep Data Small: Keep JSON test data focused and small for fast test runs and easy maintenance.
Self Check

Where in this folder structure would you add a new JSON file for test data related to user registration?

Key Result
Store test data as JSON files in a dedicated data folder and load them via utility functions for data-driven Selenium tests in Python.