Selenium with Jenkins Integrationin Test Automation
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.
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.
Why to Integrate Selenium with Jenkins
Integrating Selenium with Jenkins can bring numerous benefits:
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
- Go to the Jenkins dashboard, click New Item.
- Enter a name for your job, select Freestyle Project, and click OK.
- 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
- Under the Build Environment section, select
Provide Node & npm bin/ folder to PATH
if you’re using a JavaScript-based Selenium project. - 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:
- Under Build section, click
Add Build Step > Invoke Top-Level Maven Targets.
- 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 can automatically run your Selenium tests as soon as 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 as soon as 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
As soon as 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
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:
- Go to the job’s main page.
- Click on the
Build History
section and select the latest build. - Under the Console Output, you can view logs of the build and test execution.
- The Test Result page will show the number of tests passed, failed, or skipped.
Important Points
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.