Automated Test Execution with Selenium and Jenkins: A DevOps Guide

The DevOps DojoThe DevOps Dojo
4 min read

Description: In this comprehensive guide, we'll explore how to use Selenium WebDriver for automated browser testing and integrate it into a Jenkins pipeline for continuous testing. We'll automate the testing of web applications across different browsers using Selenium, Docker, and Jenkins, and cover the setup of a robust CI/CD pipeline that includes test result aggregation.


Introduction

In modern DevOps workflows, testing is not a one-time activity but an ongoing process. Continuous testing ensures that code changes do not break existing functionality and that new features perform as expected. Selenium and Jenkins are two of the most popular tools for achieving this goal.

  • Selenium is an open-source tool for automating web browsers.

  • Jenkins is a widely-used automation server that enables continuous integration and delivery.

Combining these tools helps teams implement automated testing as a crucial step in the CI/CD pipeline. This article walks you through integrating Selenium tests into Jenkins, using Docker to handle cross-browser testing, and visualizing test results.


Prerequisites

Before we dive in, ensure you have the following:

  • Basic knowledge of Selenium and Jenkins

  • Jenkins installed (locally or on a server)

  • Docker installed

  • Git installed


Step 1: Writing Selenium WebDriver Tests

Selenium supports multiple languages like Java, Python, C#, and JavaScript. For this guide, we will use Python.

Install the required packages:

pip install selenium pytest

Sample Selenium Test (Python)

# test_google_search.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

def test_google_search():
    driver = webdriver.Chrome()
    driver.get("https://www.google.com")
    assert "Google" in driver.title
    elem = driver.find_element("name", "q")
    elem.send_keys("DevOps community")
    elem.send_keys(Keys.RETURN)
    time.sleep(2)
    assert "DevOps" in driver.page_source
    driver.quit()

Use pytest to run the test:

pytest test_google_search.py

You should see a green bar indicating success if all assertions pass.


Step 2: Creating a Dockerized Environment for Cross-Browser Testing

Cross-browser compatibility is essential in production applications. Docker helps run tests on various browser environments without requiring installation on your local machine.

We’ll use Selenium Grid with Docker.

Docker Compose File for Selenium Grid

# docker-compose.yml
version: '3'
services:
  selenium-hub:
    image: selenium/hub:4.15.0
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

  chrome:
    image: selenium/node-chrome:4.15.0
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  firefox:
    image: selenium/node-firefox:4.15.0
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

Run the grid:

docker-compose up -d

Your Selenium Grid should now be available at http://localhost:4444/ui.

Modify Test to Use Remote WebDriver

# test_remote_google_search.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

def test_google_search():
    driver = webdriver.Remote(
        command_executor='http://localhost:4444/wd/hub',
        desired_capabilities=DesiredCapabilities.CHROME)

    driver.get("https://www.google.com")
    assert "Google" in driver.title
    elem = driver.find_element("name", "q")
    elem.send_keys("DevOps community")
    elem.send_keys(Keys.RETURN)
    time.sleep(2)
    assert "DevOps" in driver.page_source
    driver.quit()

Step 3: Setting Up Jenkins for Continuous Testing

Install Jenkins plugins:

  • Git plugin

  • Pipeline plugin

  • HTML Publisher plugin (for test reports)

Jenkins Job Configuration

We’ll use a Jenkins Pipeline (declarative syntax).

Jenkinsfile

pipeline {
    agent any

    environment {
        PYTHONUNBUFFERED = 1
    }

    stages {
        stage('Clone repository') {
            steps {
                git 'https://github.com/your-org/selenium-tests.git'
            }
        }

        stage('Set up Docker Selenium Grid') {
            steps {
                sh 'docker-compose -f docker-compose.yml up -d'
            }
        }

        stage('Install dependencies') {
            steps {
                sh 'pip install -r requirements.txt'
            }
        }

        stage('Run tests') {
            steps {
                sh 'pytest --html=report.html --self-contained-html'
            }
        }

        stage('Publish Report') {
            steps {
                publishHTML(target: [
                  allowMissing: false,
                  alwaysLinkToLastBuild: true,
                  keepAll: true,
                  reportDir: '.',
                  reportFiles: 'report.html',
                  reportName: 'Test Report'
                ])
            }
        }
    }

    post {
        always {
            sh 'docker-compose -f docker-compose.yml down'
        }
    }
}

Step 4: Aggregating and Visualizing Test Results

Using Pytest HTML Reports

We've already used the --html=report.html flag to generate an HTML report. The HTML Publisher Plugin in Jenkins makes this available under the build details.

You can also integrate with:

  • Allure Reports

  • JUnit Test Results Plugin

To use Allure:

pip install allure-pytest
pytest --alluredir=allure-results

Then use Jenkins' Allure plugin to publish the results.

Triggering Tests Automatically

To enable continuous testing:

  1. Set up a GitHub webhook to trigger builds on commit.

  2. Schedule tests using Jenkins cron syntax (e.g., H/15 * * * * for every 15 mins).


Best Practices for Automated Testing with Selenium and Jenkins

  • Use Page Object Model to organize test code.

  • Use Docker volumes for consistent test environments.

  • Parallelize tests for speed.

  • Handle flaky tests with retries and better selectors.

  • Clean up resources to avoid container bloat.


Troubleshooting Common Issues

IssueSolution
Selenium tests fail in Jenkins but pass locallyEnsure Jenkins agents have display environments or use headless mode
Docker containers not connectingCheck Docker network settings or use bridge mode
Jenkins workspace errorsUse cleanWs() to clear the workspace before build

Conclusion

Automating test execution using Selenium and Jenkins helps deliver reliable, quality software at scale. This integration enables continuous feedback and faster releases while ensuring cross-browser compatibility.

By combining Docker, Selenium Grid, and Jenkins Pipelines, you achieve a scalable, maintainable, and powerful automated testing solution.


References


0
Subscribe to my newsletter

Read articles from The DevOps Dojo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

The DevOps Dojo
The DevOps Dojo