Selenium is the most popular open source automated testing tool for Web applications. Jenkins is an open source continuous integration tool written in Java. In this tutorial, we are going to integrate Jenkins with Selenium to get the most out of both tools.
Integrating Selenium with Jenkins has many advantages and it takes automation testing to the next level. This integration process permits testers to continuously test and integrate source code advancements in the software development process. An added advantage of Jenkins and Selenium integration is that it automates the code arrangement from the development environment to the testing, staging and production environments.
Jenkins has many features like the ability to integrate with SVN and GIT, and to define continuous integration pipelines. The key functionality of the Jenkins CI tool is to execute predefined tasks to continuously check for code changes in different modules and integrate them.
The advantages of integrating Selenium with Jenkins are listed below:
- Selenium and Jenkins are both open source tools and have a very large knowledge bank and community support.
- These are cross-platform tools that you can use on Windows, Linux, Mac OS and Solaris environments.
- Testing and continuous integration can be done simultaneously.
- They support a wide variety of technologies and tools in the application development process.
The Selenium-Jenkins integration process
The Jenkins and Selenium integration process is divided into three steps. Let’s discuss each step in detail.
Step 1: Downloading Jenkins
1. Open the Jenkins official site in your Web browser to download the jenkins.war file.
2. Download the jenkins.war file from https://jenkins.io/download/ as displayed in Figure 1 and save it to the desktop or some other folder.
3. After completing the download, you will get the jenkins.war file, which has to be executed to install Jenkins.
Step 2: Jenkins integration with Selenium WebDriver
1. Go to the location where jenkins.war is stored.
2. Open a command prompt CMD in your system, then go to the project home category and start the Jenkins server by using the following command:
Begin cmd> Project_home_Directory> java – jar jenkins.war
3. When you run the command, java – jar jenkins.war, the Jenkins server is up and running. By default, Jenkins will run on the 8080 port so you can explore the Jenkins UI there. Open any Web browser and enter the URL http://localhost:8080.
Now that Jenkins is up and running, we need to create a goal to execute the test case using the Selenium plugin.
4. After installing the Selenium plugin in Jenkins, we are done with the installation process. Now we have to design Jenkins goals to also recognise different tools like Java, Maven and so on (Figure 3).
Goto > Manage Jenkins
Click on Configure the System (Figure 4).
Next, we go to the JDK area and click on the Add JDK button to let Jenkins know the exact path of Java (Figure 5).
- Uncheck the Install automatically check box so that Jenkins will just take Java, which we specified earlier.
- Name it JAVA_HOME and specify the JDK way.
In Selenium Jenkins continuous integration, we have a very effective component that allows you to arrange email notifications for the client. This is optional but in case you need to design email notifications, then making small settings at the time of arranging Jenkins solves the issue.
As shown in Figure 6, you can change the login details and click on Apply.
Once you are done with the changes, click on Save and then Apply. Congratulations, your Selenium WebDriver integration with Jenkins has been done perfectly.
Step 3: Executing Jenkins Selenium WebDriver testing
We can execute test cases in Jenkins using four different methods, as shown in Figure 7.
In this tutorial, we are going to execute this using the Window batch command.
1. First, you need to create a batch document, and then add a similar cluster record to Jenkins. Let’s divide this task into three sub-parts—A, B and C.
Part A: To create batch documents, set the classpath of TestNG so that we can execute the testng.XML file. Our task structure should look like what’s shown in Figure 8.
Part B: The open command evokes and sets the classpath. While setting the classpath, we will set the way bin and libs are organised:
Home index > set classpath=C:\Users\Learnautomation\bin;C:\Users\Learn-automation\libs\*;
Part C: Open any Notepad or text editor and write the command given below, then save it as a .bat document.
java – cp bin;libs/* org.testng.TestNG testng.XML
2. Now we have to make a task in Jenkins which will execute our build. Open Jenkins on a Web browser (http://localhost:8080). Click on ‘New Item’. Here ‘item’ is nothing but a task. Give the item’s name, select ‘Build a free-style software project’ and then click on the ‘OK’ button (Figure 9).
Next, navigate to Advanced Project Options, select the utilisation custom work space, and in the index we will determine the project home catalogue. Now determine the Add Build step.
Click on the ‘Execute Windows batch’ command. In the command area, do mention the batch command which we created earlier, and click on Save and then Apply.
3. We are ready to run the build. Open Jenkins in the Web browser and click on Build Now.
4. You can check the build history in the Console Output and confirm the outcome. Selenium integration with Jenkins runs the script file every time there is any change in source code or in a situation where code is moving from one environment to another. Jenkins saves information related to execution history and test reports. It’s very valuable when running Selenium from Jenkins with your prepared test cases and you can run them using a single click. You can also schedule the test cases using the batch document in Jenkins.
Jenkins and Selenium are the most powerful tools, independently, and when we integrate Selenium in Jenkins the benefits are multiplied. It’s advisable to integrate Selenium and Jenkins in your projects to get the maximum from them. If you have any queries, please feel free to write to me.