Integrate Selenium with Jenkins for Continuous Integration (CI/CD) in Test Automation- Master Advanced Selenium

CI/CD has become indispensable for ensuring that code changes are frequently tested and deployed. When we integrate Selenium with Jenkins, this ensures even faster testing. By automating test runs using Jenkins, you can ensure that any new code is tested thoroughly before being pushed to production, thus reducing bugs and improving the overall quality of your product.

In this blog post, I will walk you through the step-by-step process to integrate Selenium with Jenkins. This guide will help you automate your Selenium tests and set up a robust CI/CD pipeline. We will cover everything from setting up Jenkins to running Selenium tests automatically as part of your build process.

Why to Integrate Selenium with Jenkins

Integrating Selenium with Jenkins can bring numerous benefits:

Automated Test Execution: Jenkins triggers tests automatically on each code change.

Improved Build Quality: Regular testing ensures that bugs are caught early.

CI/CD Pipeline Automation: Helps developers deliver new features faster by integrating automated testing into the build process.

Scalability: Jenkins can be used to distribute Selenium tests across multiple nodes for parallel execution.

Steps to Integrate Selenium with Jenkins

Step 1: Setting up Jenkins

If you haven’t already, you need to install Jenkins and configure it. Here’s how to get started.

1.1 Install Jenkins

1. Download the Jenkins .war file from Jenkins Official Website.

2. Open a terminal and navigate to the directory where the .war file is located.

3. Run the following command:

java -jar jenkins.war

4. Jenkins will now be available on http://localhost:8080. Follow the on-screen instructions to complete the installation.

1.2 Install Required Plugins

For Selenium integration, you need to install a few Jenkins plugins:

1. Navigate to Jenkins dashboard.

2. Go to Manage Jenkins > Manage Plugins.

3. Under the “Available” tab, search for:

  • Git Plugin (for version control)
  • Maven Integration Plugin (for building the project)
  • Selenium Plugin (for executing Selenium tests)

4. Select these plugins and click Install without restart.

Step 2: Configuring the Jenkins Job

Now that Jenkins is installed and running, let’s configure a new job to run Selenium tests.

2.1 Create a New Jenkins Job

  1. Go to the Jenkins dashboard, click New Item.
  2. Enter a name for your job, select Freestyle Project, and click OK.
  3. Under the General tab, check the option Git for version control and provide the repository URL of your Selenium project.

2.2 Configure Build Environment

  1. Under the Build Environment section, select Provide Node & npm bin/ folder to PATH if you’re using a JavaScript-based Selenium project.
  2. For Java-based projects, ensure you configure JDK in the Jenkins global configuration.

2.3 Add a Build Step

You need to tell Jenkins how to build and execute your tests. If you’re using Maven, follow these steps:

  1. Under Build section, click Add Build Step > Invoke Top-Level Maven Targets.
  2. Provide the Goals and Options field with:
clean test

This tells Maven to clean the workspace and run the tests.

2.4 Post-Build Actions

1. To view the results of your Selenium tests, under Post-build Actions, click Add post-build action > Publish JUnit test result report.

2. Enter the location of your test report:

**/target/surefire-reports/*.xml

This will allow Jenkins to display the test results on the job’s dashboard.

Step 3: Running Selenium Tests with Jenkins

The industry standard practice to integrate selenium with jenkins is adopted so that, Jenkins will automatically run your Selenium tests whenever there is a change in your code repository.

3.1 Triggering Tests Automatically

To trigger your tests automatically with each commit:

1. Under Build Triggers, select GitHub hook trigger for GITScm polling.

2. Configure a webhook in your GitHub repository to trigger Jenkins when a new commit is pushed. Go to your GitHub repo > Settings > Webhooks and add your Jenkins server URL:

http://<Jenkins-URL>:8080/github-webhook/

3.2 Running the Tests

Whenever you push new code, Jenkins will trigger the job, execute the Selenium tests, and publish the results.

Step 4: Parallel Execution of Selenium Tests

To speed up your CI/CD pipeline, you can configure Jenkins to run Selenium tests in parallel across multiple nodes.

4.1 Set Up Jenkins Slave Nodes

1. Go to Manage Jenkins > Manage Nodes and Clouds.

2. Add new nodes for parallel execution.

3. Configure Selenium Grid to distribute the tests across multiple nodes.

4.2 Modify Your TestNG or JUnit XML for Parallel Execution

In your TestNG XML configuration file, set up parallel execution:

<suite name="Suite" parallel="tests" thread-count="4">
   <test name="Test1">
      <classes>
         <class name="com.example.tests.SampleTest1"/>
      </classes>
   </test>
   <test name="Test2">
      <classes>
         <class name="com.example.tests.SampleTest2"/>
      </classes>
   </test>
</suite>

Now, Jenkins will distribute your Selenium tests across multiple nodes, dramatically speeding up the execution time.

Step 5: Viewing Test Results and Logs

Once the build completes, you can view the results directly on the Jenkins dashboard:

  1. Go to the job’s main page.
  2. Click on the Build History section and select the latest build.
  3. Under the Console Output, you can view logs of the build and test execution.
  4. The Test Result page will show the number of tests passed, failed, or skipped.

Important Points to Integrate Selenium With Jenkins

Use Selenium Grid to run tests on multiple browsers or environments simultaneously.

Leverage Docker containers to spin up isolated Selenium environments using Selenium’s official Docker images.

Integrate with AWS Lambda to run headless Selenium tests in the cloud without the need to manage infrastructure.

Monitor Jenkins jobs with tools like Prometheus or Grafana to ensure performance and stability.

To Integrate Selenium with Jenkins is a powerful way to automate your testing process, ensuring higher quality releases and faster deployment cycles. By following the steps outlined in this tutorial, you’ll set up a reliable CI/CD pipeline for running Selenium tests and improve the overall effectiveness of your test automation strategy.

Don’t forget to subscribe.:)

Please visit https://automationscript.com/how-to-integrate-selenium-with-jenkins/ to see how to create a simple job to run selenium test on Jenkins. This is the simplest way to integrate selenium with jenkins.


Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts