0
0
Selenium Pythontesting~8 mins

WebDriver setup (ChromeDriver, GeckoDriver) in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - WebDriver setup (ChromeDriver, GeckoDriver)
Folder Structure
my_selenium_project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── webdriver_factory.py
│   └── config/
│       └── config.yaml
├── drivers/
│   ├── chromedriver.exe (or chromedriver for Mac/Linux)
│   └── geckodriver.exe (or geckodriver for Mac/Linux)
├── requirements.txt
└── pytest.ini
Test Framework Layers
  • Driver Layer: webdriver_factory.py - handles setup of ChromeDriver and GeckoDriver instances.
  • Page Objects: Classes representing web pages, e.g., login_page.py, encapsulating locators and actions.
  • Tests: Test scripts using pytest, e.g., test_login.py, that use page objects and driver factory.
  • Utilities: Helper functions or classes, e.g., for waits or logging.
  • Configuration: config.yaml for environment variables, browser choice, and credentials.
Configuration Patterns
  • Use config.yaml to store environment URLs, browser type (chrome or firefox), and credentials.
  • webdriver_factory.py reads config to decide which driver to start.
  • Use environment variables or command line options to override config for CI/CD flexibility.
  • Keep driver executables in drivers/ folder and reference their paths in webdriver_factory.py.
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html or pytest-allure for readable HTML reports.
  • Configure pytest.ini to include report generation options.
  • Integrate tests in CI pipelines (GitHub Actions, Jenkins) to run tests on code push.
  • Store driver binaries in repo or download dynamically in CI setup scripts.
  • Fail tests clearly if driver setup fails, so CI feedback is immediate.
Best Practices
  • Use a WebDriver Factory: Centralize driver setup to easily switch browsers and manage driver options.
  • Keep Drivers Updated: Match driver versions with browser versions to avoid compatibility issues.
  • Use Explicit Waits: Avoid flaky tests by waiting for elements properly instead of fixed sleeps.
  • Separate Config from Code: Store environment and browser settings outside code for flexibility.
  • Handle Driver Paths Dynamically: Use relative paths or environment variables to locate drivers, supporting multiple OS.
Self Check

Where in this folder structure would you add a new utility function to wait for an element to be clickable?

Key Result
Centralize WebDriver setup in a factory module to manage ChromeDriver and GeckoDriver cleanly and flexibly.