How to configure selenium with Jenkins – Parallel Execution complete tutorial
Let’s take a look at how to configure Selenium with Jenkins. Since Jenkins is a very important tool that helps us in many ways in test automation such as Remote execution, Parallel execution, CI CD pipeline test execution, etc. Here we have a sample pipeline script for running the tests on slave machines connected to the master, function to get console output in an external log file using groovy.
System Requirements for Selenium with Jenkins Integration
I am sure you are aware of the term CI/CD since you are here to learn “How to run parallel Selenium tests in Jenkins Pipeline“. In order to run the selenium tests parallel on Jenkins, first we need to make sure that we have the below setup.
Step-by-step process to configure Selenium with Jenkins
The entire process to configure selenium with Jenkins is to create an infrastructure where code will be downloaded to a system and then execution will be triggered with the help of Jenkins. This is done with the help of the Jenkins pipeline job.
Jenkins pipeline job can be divided into multiple stages. These stages can be independent or interdependent. A stage is nothing but a group of statements pooled together to run after another(or in parallel). It is like steps(as in the English language) of a complex process.
The reason I emphasized “steps” is that, In Jenkins, we also have a keyword Steps.
In order to achieve parallel execution using Selenium with jenkins We need to perform at least two stages which are described below.
The code below assumes that we have 4 slaves connected to the master node.
To run using maven use-
mvn test
To run using TestNG.xml use
java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG testng.xml
Complete Pipeline script to run selenium tests with Jenkins
//This script uses env.NodeName= any node which is selected during build as a parameter
// You can replace it with actual Node name e.g. 'CorpResosurceVM1' (in quotes as a string)
node('master') {
def directory = 'directory path for running the automated tests'
timestamps {
stage('Sync code on nodes') {
echo 'running build' + env.BUILD_NUMBER
parallel checkout1: {
codeCheckoutOnNode(env.NodeName, directory, '*/' + env.BranchName)
},
checkout2: {
codeCheckoutOnNode(env.NodeName, directory, '*/' + env.BranchName)
},
checkout3: {
codeCheckoutOnNode(env.NodeName, directory, '*/' + env.BranchName)
},
checkout4: {
codeCheckoutOnNode(env.NodeName, directory, '*/' + env.BranchName)
}
}
stage('Execute Tests') {
echo 'executing Tests'
parallel ExecutionThread1: {
executeTest(nodeName1) //name of the node here
},
ExecutionThread2: {
executeTest(nodeName2) //name of the node here
},
ExecutionThread3: {
executeTest(nodeName3) //name of the node here
},
ExecutionThread4: {
executeTest(nodeName4) //name of the node here
}
}
}
}
def codeCheckoutOnNode(nodeName, path, branchName) {
try {
retry(2) {
timeout(time: 300, unit: 'SECONDS') {
node(nodeName) {
ws(path) {
echo 'Syncing ' + nodeName
// write here gitcheckout or any other version control tool checkout command for the specified branch
//Get help from Pipeline Syntax option if not sure how to write the command in
//Jenkins Pipleine script
}
}
}
}
} catch (all) {
echo '==========================checkout code failed here.========================='
echo nodeName
echo '============================================================================='
}
}
def executeTest(nodeName) {
directory = 'some path here on the node'
node(nodeName) {
dir(directory) {
//write code to execute tests from cmd line or any other way.
}
}
}
Discover more from Automation Script
Subscribe to get the latest posts sent to your email.