How to integrate Selenium with Jenkins – Step-by-step Example

Given the popularity of cloud technology, knowing How to integrate Selenium with Jenkins pipeline has become very important.

Jenkins is the most famous tool for CI-CD. With the rapidly evolving software development industry, testing is also adopting the “Shift Left” strategy. With this, testing a build as soon as the deployment is done has become very important. 

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.

How to integrate Selenium with Jenkins Pipeline- example script
node('master') {
  def directory = 'directory path for running the automated tests'
  timestamps {
    stage('Sync code on nodes') {
      echo 'running build' + env.BUILD_NUMBER
      codeCheckoutOnNode(env.NodeName, directory, '*/' + env.BranchName)
    }
    stage('Execute Tests') {
      echo 'executing Tests'
      node(env.NodeName) {
        dir(directory) {
          executeTest();
        }
      }
    }
  }
}

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() {
  //write code to execute tests from cmd line or any other way.
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.