0
0
Selenium Pythontesting~8 mins

Python environment setup in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Python environment setup
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── login_page.py
│   └── search_page.py
├── utils/
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── drivers/
│   └── chromedriver.exe (or chromedriver for Mac/Linux)
├── requirements.txt
├── pytest.ini
└── conftest.py
  
Test Framework Layers
  • Driver Layer: Manages browser drivers (e.g., ChromeDriver) to control browsers.
  • Page Objects: Python classes representing web pages with methods to interact with page elements.
  • Tests: Test scripts using pytest that call page objects to perform actions and assertions.
  • Utilities: Helper functions for common tasks like waits, logging, or data handling.
  • Configuration: Files storing environment settings, URLs, credentials, and browser options.
Configuration Patterns

Use config.yaml to store environment URLs and browser settings. Use credentials.yaml to keep usernames and passwords safe and separate from code.

Use pytest.ini to configure pytest options like markers and test paths.

Use conftest.py to define fixtures that setup and teardown browser sessions and load config data.

Example config.yaml snippet:

environment:
  base_url: "https://example.com"
browser: "chrome"
  
Test Reporting and CI/CD Integration

Use pytest built-in reporting with options for verbose output and junit XML reports.

Integrate with CI/CD tools like GitHub Actions or Jenkins to run tests automatically on code changes.

Example pytest command for reports:

pytest tests/ --junitxml=reports/results.xml -v
  

Reports help quickly see which tests passed or failed after each run.

Best Practices
  • Keep browser drivers in a dedicated drivers/ folder and update them regularly.
  • Separate test data and credentials from code using config files and environment variables.
  • Use pytest fixtures in conftest.py for reusable setup and teardown logic.
  • Organize tests and page objects clearly to keep code easy to maintain.
  • Use explicit waits in Selenium to handle dynamic page elements reliably.
Self Check

Where in this folder structure would you add a new page object for the Login page?

Key Result
Organize Python Selenium tests with clear folders for tests, page objects, config, and drivers using pytest fixtures and config files.