Mastering Jenkins and Selenium for Automated Browser Testing

Have you ever felt frustrated trying to keep up with the demanding release pace expected these days? As an application testing veteran with over 10 years under my belt, I‘ve been there too!

The endless regression cycles to validate new features and bug fixes drain productivity for developers and testers alike. But it doesn‘t have to be this way.

By combining an automated continuous integration (CI) server called Jenkins and a popular browser test framework named Selenium, one can set up a pipeline to run UI tests automatically whenever new code gets checked in. Talk about a game changer!

In this guide, I‘ll walk you through step-by-step how to configure these open-source tools to eliminate repetitive manual testing forever.

Here is a quick overview of the key concepts we‘ll cover:

Jenkins – The leading open-source CI/CD automation server used by over 65% of organizations according to Statista. It runs predefined scripts whenever changes pushed to source control to build, test, analysis, and deploy applications automatically.

Selenium – The most widely adopted browser test framework globally used by millions of testers based on GitHub stars. It supports automating UI validation across different browsers like Chrome, Firefox, and Safari using a friendly Selenium WebDriver API.

Integrating Them — By wiring Jenkins to run Selenium browser tests automatically whenever developers check-in new code, one gains instant feedback identifying regressions early without tester involvement. This is the foundation of a mature CI/CD pipeline.

And much more! By the end of this guide, you’ll be a Jenkins and Selenium expert ready to implement test automation across your own systems. Let‘s get started friend!

Chapter 1 – Jenkins and Selenium Background

Before jumping into using Jenkins and Selenium…

What Exactly is Jenkins?

In the simplest terms, Jenkins is an automation engine to trigger tasks whenever predefined events occur. Created by Kohsuke Kawaguchi initially at Sun Microsystems, it has grown to become the most widely adopted open-source CI/CD (Continuous Integration/Continuous Delivery) tool on the market.

Some key capabilities it brings to the table:

Automated Builds — Jenkins continuously watches source code repositories for changes via "webhooks". The second a developer checks-in new code, it triggers compiling, automated testing, code analysis, and packaging of the application without any human intervention.

Declarative Pipelines — Jenkinsfile syntax allows codifying complex multi-stage workflows spanning different environments using a simple script that gets checked into Git. This ensures the pipeline stays up-to-date with the latest branches.

Easy Distributed Execution — Scaling test execution is easy by adding "Jenkins agents" to run jobs in parallel leveraging a central coordination server. No need to manually dispatch tests across infrastructure.

Plugin Architecture – With over 1500 plugins, Jenkins functionality expands far beyond what comes out of the box to integrate with virtually every other DevOps tool like Docker, Kubernetes, AWS, and more.

Based on the State of CI/CD Report 2022, 65% of organizations leverage Jenkins as the engine behind their automated pipelines. Given its maturity and ubiquity, Jenkins is likely already running behind the scenes!

And What About Selenium?

Since most modern applications contain a user interface delivered through web browsers, testing them requires actually simulated users interacting with those UIs. This is exactly what Selenium specializes in.

Created by Jason Huggins back in 2004 while at ThoughtWorks, Selenium has grown to become the most widely used browser test framework available today.

Unlike other testing tools, it provides an entire browser automation library for replicating real user workflows in applications hosted anywhere in the world.

Capabilities include:

Multiple Languages — Out of the box, Selenium supports writing test scripts using Java, C#, Python, JavaScript/NodeJS, and more. This allows leveraging existing team skills.

Cross-Browser — Configuring tests to execute across Chrome, Firefox, Edge, Safari and others ensures compatibility testing on how the app renders across environments it will be used.

Distributed Execution — Selenium Grid enables allocating browser tests to run in parallel leveraging a hub speaking to many nodes, drastically cutting test cycles.

Mobile/Responsive Testing — Beyond desktop environments, teams can validate mobile experiences on real device emulators and simulators accessible from Selenium Grid.

As evidence of its popularity, Selenium has over 865,000 stars on GitHub and over 45,000 StackOverflow questions tagged. It‘s by far the most ubiquitous browser test framework available today.

Chapter 2 – Installing Jenkins

With the background out of the way, let‘s shift gears into actually setting up Jenkins!

The first step is to get Jenkins server installed somewhere:

# Install Java Runtime 
sudo apt install default-jdk

# Add Jenkins apt repository
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list

# Update and install Jenkins
sudo apt update
sudo apt install jenkins

This leverages the official Jenkins apt repo to install the latest Long Term Support release.

With hundreds of plugins and capabilities, having ample CPU cores and RAM available is recommended for Jenkins server.

By default Jenkins runs on port 8080 and uses /var/lib/jenkins to store job configurations and build artifacts.

Upon first visiting Jenkins in the browser, an admin password is required:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

This unlocks access to customize Jenkins by installing plugins and creating your first build pipelines!

Chapter 3 – Configuring Selenium

Shifting gears, let‘s look at setting up Selenium next…

To write and execute Selenium browser test scripts, several components need to be in place:

Browser Driver Executables

For each browser like Chrome, Firefox, etc. there is a platform-specific driver executable like ChromeDriver, GeckoDriver, etc. which Selenium utilizes behind the scenes to interface with the browser, execute commands, and relay back results.

Language Bindings

Beyond the core Selenium library, individual language bindings like selenium-java, selenium-python, selenium-c# need to be installed matching the languages your team writes tests in. These provide wrappers translating Selenium commands into native method calls of that language.

Development Environment

Whether IDEs like IntelliJ or Eclipse for Java, Visual Studio for C#, or PyCharm for Python, having a coding environment setup makes writing Selenium scripts easier.

Helpful Libraries

For Java tests, utilities like TestNG or JUnit can provide test runners, assertions, decorators, and reports to build on top of Selenium APIs.

With those prerequisites met, you are ready to start scripting out Selenium based browser tests!

Let‘s walk through a simple example…

// Import Selenium WebDriver classes
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyTest {

  public static void main(String[] args) {

    // Initialize ChromeDriver
    System.setProperty("webdriver.chrome.driver","/path/to/chromedriver");

    // Launch new browser instance
    WebDriver driver = new ChromeDriver();

    // Navigate to the website 
    driver.get("https://www.my-app.com");

    // Locate element by CSS selector
    WebElement search = driver.findElement(By.cssSelector("#search")); 

    // Send text to the search box 
    search.sendKeys("My Search Query");

    // Close the browser
    driver.quit();

  }

}

This walks through initializing a new Chrome browser, navigating to our web app, locating a search box by its CSS selector id, sends keystrokes to input search text, and lastly closes the browser cleanly.

From here you can begin writing real test scripts leveraging Selenium‘s rich API surface to simulate anything end users might do!

Chapter 4 – Integration Game Plan

Alright, now the fun part…wiring Jenkins to run Selenium scripts automatically!

Here is an overview of how we will join these two tools:

Jenkins Selenium Integration Diagram

The core steps we will walk through:

  1. Create new Jenkins freestyle job
  2. Configure job to trigger Selenium tests
  3. Script out sample Selenium test script
  4. View test execution results
  5. Implement best practices

By combining Jenkins continuous integration capabilities with Selenium for browser test automation, it provides a compelling way to shift left and prevent defects before they ever impact end users!

Chapter 5 – Configuring Jenkins Jobs

Let‘s start by setting up Jenkins…

  1. From the Jenkins dashboard, click New Item

  2. Provide job name as selenium-tests, choose Freestyle Project

  3. Under Source Code Management select None

  4. In the Build section, add Execute Windows batch command

    java -cp bin;lib/* org.testng.TestNG testng.xml
  5. Save changes

Now whenever this Jenkins job runs, it will trigger our Selenium test suite execution automatically!

But first we need some tests…

Creating Sample Selenium Script

I like organizing my test projects using a standard Maven directory structure:

selenium-tests
|- src
   |- test
      |- java
         |- MyTest.java
|- testng.xml
|- pom.xml

MyTest.java contains the Selenium test logic:

import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class MyTest {

  @Test
  public void loginTest() {

    // Test logic goes here

  }

}

testng.xml defines test suites:

<?xml version="1.0" encoding="UTF-8"?>  
<suite name="LoginSuite">
  <test name="UserLogin">
    <classes>
      <class name="MyTest"/>
    </classes>
  </test>  
</suite>

And pom.xml handles Selenium dependencies:

<dependencies>
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.7.2</version>
  </dependency>
</dependencies>

With this project scaffolding in place, you can being authoring Selenium test scripts that will automatically execute via Jenkins!

Chapter 6 – Viewing Test Results

Once tests are running, viewing execution reports is crucial for debugging failures:

The TestNG Results Plugin can integrate Selenium script results directly into Jenkins.

  1. From Manage Plugins search for TestNG Results Plugin

  2. Install plugin and restart Jenkins

  3. In job configuration scroll to Post-build Actions

  4. Enable Publish TestNG Results

  5. Provide relative path to *test-output/.xml** files

This will embed detailed test runtime outputs like:

Sample TestNG Results

Debug failing tests quicker by:

  • Enabling video recordings to see interactions
  • Capturing browser logs/network traffic
  • Integrating log analysis tools like Logz.io

Failures become learning opportunities to improve test coverage!

Chapter 7 – Scaling Best Practices

As your automated test suites grow, here are some key best practices to follow:

Adopt Version Control

Maintaining all test scripts under source control enables tracing history over time as scope evolves. GitHub, GitLab, BitBucket help here.

Segregate Test Types

Categorize validation by risk levels like smoke tests, integration, end-to-end, performance, etc. Then break into parallel Jenkins jobs to isolate.

Parameterize Tests

Avoid hard-coding values in scripts. Pass inputs/configs externally through CSV datasets, property files, environment variables, etc.

Implement Page Objects

Encapsulate page element locators and interactions behind helper classes instead of spreading through tests. This reduces duplication.

Tag Tests for Allocation

Utilize markers like @smoke, @frontend, etc. so Jenkins can selectively run subsets of tests in parallel intelligently based on capability.

These patterns help sustain test suites as scope grows over thousands of tests running on Jenkins!

Chapter 8 – Running Tests In Parallel

Manually running test suites sequentially can take hours for large apps.

Thankfully Jenkins simplifies executing tests in parallel…

Step 1: Provision Multiple Agents

Agents are the workers that run jobs. Having multiple enables parallelism.

Step 2: Segregate Tests

Break tests into logical groups based on app area or execution time using tags.

Step 3: Allocate Tagged Tests

At runtime, selectively target relevant agents to run specific test groups in parallel:

Jenkins Parallel Test Execution

Adding more agents allows running vastly more tests in the same time across an entire application!

Chapter 9 – Cross Browser Testing

Another best practice is validating UI behavior across the browsers and devices your customers use:

  • 85% of users leverage Chrome, Safari, Edge and Firefox.

  • Over 63% of traffic now originates from smartphones.

Supporting this matrix manually is impractical. Instead, leverage Selenium Grid:

High Level Selenium Grid Architecture

Grid Hub orchestrates tests executing on Nodes with different browsers setup.

Run cross-environment validations in parallel without any additional coding!

For even more flexibility, cloud testing providers offer Grid as a Service with thousands of real desktops, mobile devices, and browsers accessible on-demand for integration into CI pipelines.

This expands test coverage to match real-world usage.

Chapter 10 – Testing Internal Systems

Sometimes web apps are only hosted internally like:

  • Employee portal sites
  • Admin consoles
  • Business intelligence dashboards

Selenium can directly test these private tools as well through Jenkins!

The magic lies in establishing a secure tunnel from Jenkins into remote internal infrastructure.

BrowserStack Local Testing Architecture

This allows controlling browsers inside the firewall as if they were local while configuring tests externally from Jenkins.

Special plugins handle all the tunnel creation/teardown automatically around test lifecycle.

No need to open insecure firewall policies just to enable test automation!

Chapter 11 – Integrations & Customizations

A major benefit of Jenkins is being able to customize and adapt pipelines to unique team needs through plugins.

Jenkins Ecosystem

Some examples:

  • Git/SVN plugins to access source control
  • Slack/Teams plugins for notifications
  • Email Ext plugin for approvals and sign off
  • Jira plugin for requirements traceability
  • Pytest/JUnit/Nunit plugins for other test frameworks
  • The list goes on…!

Whatever tools your organization leverages, chances are there is a Jenkins plugin to integrate things into your automated workflows.

Chapter 12 – Recommended Reading

If you made it this far, congrats friend! 🎉

You now understand core concepts around leveraging Jenkins and Selenium to set up automated test pipelines.

Here are some recommended resources if you would like to dive deeper:

As next steps consider:

  • What scripts can be automated leveraging Selenium?
  • Where might parallel execution help accelerate test cycles?
  • What other systems could integrate with Jenkins?

Wishing you best of luck with test automation! Now go harness the power of Jenkins and Selenium to take deployments to the next level!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.