GitHub Actions for Selenium Python

End-to-End CI/CD for Automation Testing | Full Workflow & YAML Examples

← Back to Selenium Python Training

📌 Module 1: Introduction to GitHub Actions

📌 Module 2: Folder & Workflow Structure

project/
 ├── tests/
 │    ├── test_login.py
 │    ├── test_checkout.py
 ├── requirements.txt
 ├── pytest.ini
 └── .github/
      └── workflows/
            └── selenium.yml
  

📌 Module 3: Create First GitHub Actions Workflow

Location: .github/workflows/selenium.yml

name: Selenium Python CI

on:
  push:
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout Code
      uses: actions/checkout@v3

    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'

    - name: Install Dependencies
      run: pip install -r requirements.txt

    - name: Run Selenium Tests
      run: pytest -v --disable-warnings
  

📌 Module 4: Running Selenium with Browsers

Using Chrome in GitHub Actions via ChromeDriver

- name: Install Chrome
  run: sudo apt-get install google-chrome-stable

- name: Install ChromeDriver
  run: |
    sudo apt-get update
    sudo apt install chromium-chromedriver
    sudo ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin/chromedriver
  

📌 Module 5: Selenium Python Test Example

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def test_google_title():
    options = Options()
    options.add_argument("--headless")
    driver = webdriver.Chrome(options=options)

    driver.get("https://google.com")
    assert "Google" in driver.title

    driver.quit()
  

📌 Module 6: HTML Report Generation

Install plugin:

pip install pytest-html

Update workflow:

- name: Run Tests with HTML Report
  run: pytest --html=report.html
  

📌 Module 7: Upload Test Reports in GitHub Actions

- name: Upload HTML Report
  uses: actions/upload-artifact@v3
  with:
    name: Selenium-Test-Report
    path: report.html
  

📌 Module 8: Parallel Testing (Matrix Build)

jobs:
  selenium-tests:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: ["3.9", "3.10"]
        browser: ["chrome", "firefox"]

    steps:
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}

      - name: Run Tests
        run: pytest -v
  

📌 Module 9: Secrets & Environment Variables

Store secrets in:

GitHub → Settings → Secrets → Actions

Use inside workflow:

env:
  BASE_URL: ${{ secrets.BASE_URL }}
  PASSWORD: ${{ secrets.PASSWORD }}
  

🚀 Final CI/CD Pipeline for Selenium Python (Complete YAML)

name: Selenium Python CI/CD

on: [push, pull_request]

jobs:
  selenium:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - uses: actions/setup-python@v4
      with:
        python-version: '3.10'

    - run: pip install -r requirements.txt

    - run: sudo apt-get install chromium-browser chromium-chromedriver

    - run: pytest --html=report.html

    - uses: actions/upload-artifact@v3
      with:
        name: Selenium-HTML-Report
        path: report.html