automationScript

Parallel execution in Selenium using Jenkins

Jenkins is a very important tool which 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.

Requirements

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 senium tests parallel on jenkins, first we need to make sure that we have below setup.

  1. Master node and at least two live slave agents
  2. Access to a version control tool where code repo is placed.

Step by step process

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.

Selenium parallel execution with Jenkins requires at least two stages which are described below.

  1. Code checkout on all the connected slave agents (online execution systems)
  2. Execute tests (on connected slave agents)

The code below assumes that we have 4 slaves connected to the master node. 

  1. Stage Sync code on nodes: In this stage, automation code is checked out on all four slave nodes in the specified directory. Since we are using the keyword “parallel“, the subsequent steps will be run parallel.  This is handled by the function codeCheckoutOnNode. You can learn more about How to checkout code on the slave node.
  2. Stage Execute Tests:  Again this stage is composed of four steps, which will run in parallel; ExecutionThread1, ExecutionThread2, ExecutionThread3, ExecutionThread4. The method which is called in the step ExecutionThread can be written for any tool in so many ways. for example:
    1. To run using maven use mvn test
    2. To run using testng.xml use java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG testng.xml

Code

//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.
		}
	}
}
Subscribe for updates