Run your Selenium tests inside docker container in Jenkins-Part 2
In my previous post I shared details on running selenium tests inside a docker container on your windows machine. Now let’s see how to run Jenkins in docker and then run selenium tests in Jenkins (inside a docker container).
Jenkins is most widely used CI/CD tool for building, testing and deploying applications.
What is Jenkins?
Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.
Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed.
Installing Jenkins (in Docker)
Pre-requisites
- Understanding of Jenkins, Docker, Docker networking, Docker volumes
Please follow instructions from here — https://www.jenkins.io/doc/book/installing/docker/
For Windows — https://www.jenkins.io/doc/book/installing/docker/#on-windows
Create a bridge network in docker
docker network create jenkins
Run a docker:dind Docker image
docker run
--name jenkins-docker
--rm
--detach
--privileged
--network jenkins
--network-alias docker
--env DOCKER_TLS_CERTDIR=/certs
--volume C:\Users\msuni\Docker\Jenkins-Volume\jenkins-docker-certs:/certs/client
--volume C:\Users\msuni\Docker\Jenkins-Volume\jenkins-data:/var/jenkins_home
--publish 2376:2376 docker:dind
--storage-driver overlay2
Customize official Jenkins docker image
Step 1: Create Dockerfile using the following content
FROM jenkins/jenkins:lts-jdk11USER rootRUN apt-get update && apt-get install -y lsb-releaseRUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \https://download.docker.com/linux/debian/gpgRUN echo “deb [arch=$(dpkg — print-architecture) \signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \https://download.docker.com/linux/debian \$(lsb_release -cs) stable” > /etc/apt/sources.list.d/docker.listRUN apt-get update && apt-get install -y docker-ce-cliUSER jenkinsRUN jenkins-plugin-cli — plugins “blueocean:1.25.6 docker-workflow:1.29”
Step 2: Build your docker image
docker build -t jenkins-with-docker:0.1 .
Step 3: Run your custom image as container in Docker
docker run --name jenkins-blueocean
--restart=on-failure
--detach
--network jenkins
--env DOCKER_HOST=tcp://docker:2376
--env DOCKER_CERT_PATH=/certs/client
--env DOCKER_TLS_VERIFY=1
--publish 8080:8080
--publish 50000:50000
--volume C:\Users\msuni\Docker\Jenkins-Volume\jenkins-data:/var/jenkins_home
--volume C:\Users\msuni\Docker\Jenkins-Volume\jenkins-docker-certs:/certs/client:ro jenkins-with-docker:0.1
Step 4: Follow post-installation wizard to complete the remaining steps.
Step 5: Create the admin user — follow these instructions.
After completing installation steps, you can now browse to http://localhost:8080/login and use the new admin user credentials to login.
In my previous post, I created a custom image “seleniumtests-in-docker”. In order to use the image from Jenkins, I pushed that image to docker hub public repository.
Pre-requisites:
- Sign up here: https://hub.docker.com/signup
- You will be prompted to login using your email/id. You will asked to confirm your email address as well.
- After login, I selected Personal plan for $0 (which is the default plan with access to create unlimited public repositories).
Push custom image to docker hub
Step 1: Create your repository
Step 2: Create a new tag for your local image before it is pushed to docker hub
docker tag seleniumtests-in-docker:1.0 <username>/selenium-in-docker-demos:1.0
Step 3: Login to Docker Hub using docker cli
docker login -u <username> -p <password>
Step 4: Push your local image to docker hub. Run this command from your machine (replace username with yours)
docker push <username>/selenium-in-docker-demos:1.0
My image is available here: https://hub.docker.com/r/sunipro2022/selenium-in-docker-demos
Now that the image is ready for use, let’s create a new pipeline in Jenkins to run selenium tests.
Create Jenkins Pipeline
Step 1: Login to Jenkins (http://localhost:8080/)
Step 2: Create a new Item
Enter item name, select Pipeline and click on OK.
You should see your pipeline configuration page. Navigate to Pipeline section.
You can either use pipeline script option (enter groovy script in the editor directly — another language to learn I guess :) ) or import your Jenkins file from SCM (like Git).
I used the following pipeline script to run the image previously uploaded to Docker hub.
pipeline {
agent anystages {
stage('Run demo tests') {
steps {
sh('docker run sunipro2022/selenium-in-docker-demos:1.0 ')
}
}
}
}
Pipeline logs
So far, the pipeline executed tests with default browser (chrome).
Let’s see how to run cross browser tests using the same image.
Update Jenkins pipeline to use parameters
For more information on Jenkins parameters, please refer to this documentation: https://www.jenkins.io/doc/book/pipeline/syntax/#parameters
Step 1: Update pipeline script to add the parameter in parameters section, and update the docker run command to pass BROWSER parameter value as environment variable.
pipeline {
agent any
parameters {
string defaultValue: 'Chrome', name: 'BROWSER', description: 'Select between Chrome and Firefox browsers.'
}
stages {
stage('Run demo tests') {
steps {
sh("docker run --rm --env 'env_browser_param=${params.BROWSER}' sunipro2022/selenium-in-docker-demos:1.0 ")
}
}
}
}
Apply pipeline changes.
Submit “Build with parameters” as shown below.
Conclusion:
This post explains how to work with Jenkins and docker in your local environment. I hope it will be useful to newbies like me who are in their journey to learn Jenkins, Docker & looking to run Selenium tests from Jenkins.
I appreciate you taking time to read my post.