0
0
Selenium Pythontesting~8 mins

Running Selenium in CI pipeline in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Running Selenium in CI pipeline
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   ├── test_checkout.py
│   └── __init__.py
├── pages/
│   ├── login_page.py
│   ├── checkout_page.py
│   └── __init__.py
├── utils/
│   ├── driver_factory.py
│   ├── config.py
│   └── __init__.py
├── reports/
│   └── (test reports generated here)
├── requirements.txt
├── pytest.ini
├── conftest.py
└── .github/
    └── workflows/
        └── ci.yml
  
Test Framework Layers
  • Driver Layer: utils/driver_factory.py creates and manages Selenium WebDriver instances, supporting different browsers and headless mode for CI.
  • Page Objects: pages/ contains classes representing web pages with methods to interact with page elements.
  • Tests: tests/ holds test scripts using pytest that call page objects and assert expected behavior.
  • Utilities: utils/config.py manages environment variables and test settings; conftest.py defines pytest fixtures for setup and teardown.
  • Configuration: pytest.ini configures pytest options; requirements.txt lists dependencies.
  • CI Pipeline: .github/workflows/ci.yml defines GitHub Actions workflow to run tests on push or pull requests.
Configuration Patterns
  • Environment Variables: Use environment variables to set browser type, headless mode, and URLs to avoid hardcoding.
  • Driver Factory: Centralize WebDriver creation to switch browsers easily and enable headless mode for CI.
  • pytest.ini: Configure test markers, add options like verbosity, and specify test paths.
  • Secrets Management: Store sensitive data like credentials securely in CI environment variables, not in code.
  • CI Workflow: Define jobs to install dependencies, run tests with pytest, and collect reports.
Test Reporting and CI/CD Integration
  • Test Reports: Generate JUnit XML or HTML reports using pytest plugins for easy viewing and integration.
  • CI Integration: GitHub Actions workflow runs tests on code push or pull request, providing pass/fail status in PR checks.
  • Artifacts: Save test reports and screenshots as build artifacts for later review.
  • Notifications: Configure notifications (email, Slack) on test failures to alert the team quickly.
Best Practices
  • Use Headless Browsers in CI: Run tests in headless mode to save resources and speed up execution.
  • Isolate Tests: Ensure tests can run independently and clean up after themselves to avoid flaky results.
  • Centralize Configuration: Manage environment settings and driver options in one place for easy updates.
  • Use Explicit Waits: Avoid flaky tests by waiting for elements properly instead of fixed delays.
  • Keep Tests Fast and Focused: Run only essential UI tests in CI to keep feedback quick; use other tests for broader coverage.
Self Check

Where in this folder structure would you add a new page object for the "User Profile" page?

Key Result
Organize Selenium tests with clear layers and use CI pipelines to run headless browser tests automatically.