Jenkins pipeline + Selenium Grid + Docker

Ben Jose
Selenium Grid
Published in
4 min readMar 23, 2020

--

In this story, I will be setting up Jenkins pipeline to execute a pytest selenium script on a Selenium Grid hub using Docker compose

Stage 1: Install Jenkins, Docker and Docker Compose

Follow this link for details. (Stage 1 and 2 in the link)

Stage 2: Setting up Jenkins

Navigate to Site address(refer above mentioned stage). Login using ‘Admin user’ and ‘Admin password’. The page them navigates to Jenkins Dashboard.

Install Allure plugin. From Jenkins main window navigate to Manage Jenkins → Manage Plugins → Available(Tab) → Search for Allure.

Manage Jenkins → Manage Plugins

Click on “Download now and install after restart”

Setting up Allure Commandline. From Jenkins main window navigate to Manage Jenkins → Global Tool Configuration → Scroll down and find Allure Commandline and set the commandline as ‘Allure’

Manage Jenkins → Global Tool Configuration

Create Jenkins project. From Jenkins main window click on ‘New Item’.

Enter the name of the project and select Pipeline. Click OK to continue

On the next window, scroll down to Advanced Project Options. Select Definition : Pipeline script from SCM.

Select SCM : Git

Set reporitory URL — https://gitlab.com/benjose22/pytest_selenium.git

Set Script Path — compose.Jenkinsfile

compose.Jenkinsfile can be found at git location here. The pipeline script consist of six stages.

/* Cloning the git branch*/
stage('Clone repository') {
checkout scm
}
/* Start docker-compose with five instances of Chrome */
stage('Start docker-compose') {
sh 'docker-compose up -d --scale chrome=5 --scale firefox=0'
}

/* This builds an image with all pytest selenium scripts in it */
stage('Build image') {
def dockerfile = 'pytest.Dockerfile'
app = docker.build("pytest-with-src","-f ${dockerfile} ./")
}
/* Execute the pytest script. Even on faliure proceed to next step */
stage('Execute script') {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh 'docker run --network="host" --rm -i -v ${WORKSPACE}/allure-results:/AllureReports pytest-with-src --executor "remote" --browser "chrome" .'
}
}
/* Delete the image which got created earlier */
stage('Remove image') {
sh 'docker rmi pytest-with-src -f'
}


/* Tear down docker compose */
stage('Teardown docker-compose') {
sh 'docker-compose down --rmi local'
}


/* Generate Allure Report */
stage('Allure Report') {
allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
}

Click on ‘Save’ and the pipeline is created.

Click on “Build Now” to start the build. You can see the progress of the build in “Build history”.

Once the build is completed, we can see the stats as below —

Dashboard once the run is complete

Note that the error(red Dot) you see are intentional so dont get panic :)

Click on “Allure Report” to see the results —

Allure main dashboard

You can further drill down to see testcase description, error details, screenshots etc

If you enable Blue Ocean plugin(from Manage Jenkins → Manage Plugins ), you get a better view.

View looks better with Blue Ocean

Hope you enjoyed the story. Please share your feedback for improvements.

--

--

Ben Jose
Selenium Grid

I like to explore the technologies developing to build quality software