0
0
Selenium Pythontesting~5 mins

GitHub Actions integration in Selenium Python

Choose your learning style9 modes available
Introduction

GitHub Actions lets you automate tasks like testing your Selenium Python code every time you change it. This helps catch problems early and saves time.

You want to run Selenium tests automatically when you push code to GitHub.
You want to check that your web app works on different browsers without doing it manually.
You want to get notified if your Selenium tests fail after a code update.
You want to save time by running tests in the cloud instead of your own computer.
Syntax
Selenium Python
name: Selenium Python Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y chromium-browser chromium-chromedriver
          python -m pip install --upgrade pip
          pip install selenium
      - name: Run Selenium tests
        run: python -m unittest discover tests

The on section tells GitHub when to run the workflow, like on code push or pull requests.

The jobs section defines what tasks to run, here it sets up Python and runs Selenium tests.

Examples
This example runs tests only when you push code to GitHub.
Selenium Python
on: push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Run tests
        run: python -m unittest discover tests
This example runs tests only on pull requests targeting the main branch.
Selenium Python
on:
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python 3.12
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install Selenium
        run: |
          sudo apt-get update
          sudo apt-get install -y chromium-browser chromium-chromedriver
          python -m pip install --upgrade pip
          pip install selenium
      - name: Run tests
        run: python -m unittest discover tests
Sample Program

This workflow runs Selenium Python tests automatically every time you push code to GitHub.

Selenium Python
name: Selenium Python Tests

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y chromium-browser chromium-chromedriver
          python -m pip install --upgrade pip
          pip install selenium
      - name: Run Selenium tests
        run: python -m unittest discover tests
OutputSuccess
Important Notes

Make sure your Selenium tests do not require a graphical browser or use a headless browser like Chrome Headless.

You can add more steps to test on different Python versions or browsers.

Summary

GitHub Actions can run Selenium Python tests automatically on code changes.

Workflows are defined in YAML files inside the .github/workflows folder.

This saves time and helps catch bugs early by testing your web app automatically.