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.

  • Master node and at least two live slave agents (if you want to run parallel tests)
  • Access to a version control tool where code repo is placed. it can be git / svn etc.
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.

  • Code checkout on all the connected slave agents (online execution systems)
  • Execute tests (on connected slave agents)

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

  • 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.
  • Stage Execute Tests:  Again this stage is composed of four steps, which will run in parallelExecutionThread1ExecutionThread2ExecutionThread3ExecutionThread4. The method which is called in the step ExecutionThread can be written for any tool in so many ways. for example:
  • Run selenium tests with jenkins-
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.

Related Posts