0
0
Selenium Pythontesting~8 mins

GitHub Actions integration in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - GitHub Actions integration
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   └── test_checkout.py
├── pages/
│   ├── login_page.py
│   └── checkout_page.py
├── utils/
│   ├── driver_factory.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── reports/
│   └── test_report.html
├── .github/
│   └── workflows/
│       └── selenium-tests.yml
├── requirements.txt
└── README.md
  
Test Framework Layers
  • Driver Layer: utils/driver_factory.py creates and manages Selenium WebDriver instances.
  • Page Objects: pages/ contains classes representing web pages with methods for interactions.
  • Tests: tests/ holds test scripts using pytest that call page objects.
  • Utilities: utils/ has helper functions and driver setup code.
  • Configuration: config/ stores environment settings and credentials in YAML files.
  • CI/CD Integration: .github/workflows/selenium-tests.yml defines GitHub Actions workflow to run tests automatically.
Configuration Patterns

Use YAML files in config/ to store environment variables like URLs, browsers, and credentials.

Example config.yaml:

environment: staging
base_url: "https://staging.example.com"
browser: chrome

Example credentials.yaml (keep private, do not commit secrets):

username: "testuser"
password: "securepassword"

GitHub Actions workflow uses secrets stored in GitHub repository settings for sensitive data.

Workflow example snippet to run tests on multiple Python versions and browsers:

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.10, 3.11]
        browser: [chrome, firefox]
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Run Selenium tests
        env:
          BROWSER: ${{ matrix.browser }}
          BASE_URL: ${{ secrets.BASE_URL }}
          USERNAME: ${{ secrets.USERNAME }}
          PASSWORD: ${{ secrets.PASSWORD }}
        run: |
          pytest tests/ --html=reports/test_report.html
  
Test Reporting and CI/CD Integration
  • Use pytest-html plugin to generate HTML test reports saved in reports/.
  • GitHub Actions uploads test reports as artifacts for review after runs.
  • Configure GitHub Actions to run tests on pull requests and merges to main branch.
  • Fail the workflow if any test fails, providing clear pass/fail feedback in GitHub UI.
  • Optionally integrate notifications (email, Slack) on test results using GitHub Actions steps.
Best Practices
  1. Keep secrets out of code: Use GitHub Secrets for credentials, never commit passwords.
  2. Use matrix strategy: Test multiple Python versions and browsers automatically.
  3. Isolate tests: Each test should be independent and clean up after itself.
  4. Use explicit waits: Avoid flaky tests by waiting for elements properly in page objects.
  5. Generate readable reports: Use HTML reports for easy understanding of test results.
Self Check

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

Key Result
Organize Selenium Python tests with clear layers and automate runs using GitHub Actions workflows.