0
0
Selenium Pythontesting~5 mins

Running Selenium in CI pipeline in Selenium Python

Choose your learning style9 modes available
Introduction

Running Selenium tests in a CI pipeline helps catch website bugs automatically every time code changes. It saves time and keeps the website working well.

You want to check your website works after every code update.
You want to run browser tests automatically without opening a browser yourself.
You want to find bugs early before your users see them.
You want to run tests on different browsers or environments automatically.
You want to make sure new code does not break old features.
Syntax
Selenium Python
steps:
  - name: Run Selenium tests
    run: |
      python -m venv venv
      . venv/bin/activate
      pip install selenium
      python your_selenium_test.py

This example shows a simple CI step to run Selenium tests using Python.

You usually need to install browser drivers and run browsers in headless mode in CI.

Examples
Run Selenium tests locally by installing Selenium and running the test script.
Selenium Python
pip install selenium
python your_selenium_test.py
Example Selenium test script that runs Chrome in headless mode, useful for CI environments without a display.
Selenium Python
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
print(driver.title)
driver.quit()
GitHub Actions workflow example to run Selenium tests in a CI pipeline.
Selenium Python
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: |
          python -m venv venv
          . venv/bin/activate
          pip install selenium
      - name: Run Selenium tests
        run: |
          . venv/bin/activate
          python your_selenium_test.py
Sample Program

This Python script runs a Selenium test in headless Chrome, suitable for CI pipelines. It opens example.com, prints the page title, then closes the browser.

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

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
print(driver.title)
driver.quit()
OutputSuccess
Important Notes

Use headless mode to run browsers without a display in CI.

Make sure the CI environment has the correct browser driver installed (e.g., chromedriver).

Use options like --no-sandbox and --disable-dev-shm-usage to avoid common CI errors.

Summary

Running Selenium in CI helps catch bugs automatically.

Use headless browsers and install drivers in CI.

Automate tests in your pipeline for faster feedback.