Integration of Jenkins with Selenium WebDriver: Step-by-Step Tutorial

Step by step guide to Setup and Configure Jenkins with Selenium:

Jenkins is an open source tool written in Java. It provides continuous delivery and continuous integration service for software development.  It automates your manual task of code deployment process from development box – QA – Stage – Production.

Jenkins supports many plugins which you can integrate such as Git, SVN, build pipeline and many more. 

The basic functionality of Jenkins is to execute a predefined list of steps on the basis of time and some events.

For example, when you want to base the execution on time you can run a job every 20 minutes or if you want to base it on an event you can do so after a new commit in a Git repository.

 

In this article we covered the below points:

Advantages of using Jenkins are:

Apart from Jenkins, we have many more tools in the market such as:

What You Will Learn:

Jenkins usage and integration with selenium

Follow the below step-by-step procedure to use Jenkins with Selenium

Step #1:

Download Jenkins from the official website of Jenkins – Jenkins. Download the latest .war file. Jenkins can be started via the command line or can run in a web application server.

Refer to the below steps for the execution through the command line:

1) Open the command prompt and type java –jar and enter path of .war file

(Note: Click on any image for enlarged view)

2) Press enter and check if your Jenkins.war file started to run and check the status information on the command prompt console.

It should show – Jenkins is fully up and running

3) Now check whether your Jenkins is ready to use; by default, it uses port 8080.

Type “http://localhost:8080” in the browser and press enter. It will show you Jenkins UI.

It will load the Jenkins dashboard empty by default. I created some Jenkins job in the above screenshot as an example and hence, it did not load empty.

Step #2:

To use Selenium with Jenkins you need to configure Jenkins with Selenium.

Follow the below steps:

The radio button, Install automatically is checked by default. You need to uncheck it because it will automatically update with the new Java version and there might be a possibility that Selenium doesn’t support the new Java version. It is better to uncheck it. Now click on apply and save.

Your Jenkins is configured with Selenium and is now ready to be used with Selenium. Both Jenkins and Selenium code is written in Java. Hence, if you give the Java path then internally it will communicate and process your job.

Step #3

Now, create a Selenium script and a TestNG XML file. This TestNG XML file will be required for creating a batch file and we will use that batch file in Jenkins. Refer below TestNG code:

Refer below TestNG code:


package session_2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class jenkins_demo
{

@Test
publicvoidtestgooglrsearch(){

WebDriver driver = newFirefoxDriver();
//it will open the goggle page
driver.get("http://google.in"); 
//we expect the title “Google “ should be present 
String Expectedtitle = "Google";
//it will fetch the actual title 
String Actualtitle = driver.getTitle();
System.out.println("Before Assetion " + Expectedtitle + Actualtitle);
//it will compare actual title and expected title
Assert.assertEquals(Actualtitle, Expectedtitle);
//print out the result
System.out.println("After Assertion " + Expectedtitle + Actualtitle + " Title matched ");
 }
}

Output: Before Assertion GoogleGoogle

After Assertion, GoogleGoogle Title matched

PASSED: testgooglrsearch

 Create a TestNG xml file, refer below code:

Step #4:

Go to your project root directory and create a library folder.

Refer the screenshot below:

Now, add all your jar files which are required for running your Selenium script:

Step #5

Creating a batch file and using it in Jenkins

Create a batch file by following the below steps:

1) Open the notepad and type-: Java –cp bin;lib/* org.testng.TestNG testng.xml

By doing this, Java –cp will compile and execute .class file which is located at bin directory and all our executable jar file are located at lib directory and we are using TestNG framework so specify org.testng.TestNG. Also, specify the name of xml file which will trigger the expected TestNG script.

2) Save the file with .bat extension and check the type of file. It should be “windows batch file”. To cross check whether the batch file is created properly, double click the batch file and it will execute the code. Refer the below code of batch file:



Step #6:

Next, we need to add a batch file in Jenkins.

For adding the batch file follow the below steps:

1) Go to the Jenkins dashboard, create a new job in Jenkins

2) Click on new item and enter the item name and check the freestyle project radio button

3) Click Advance options

4) Click on use custom workspace and give your Selenium script project workspace path: “E:\Automation_workspace\Demo-testNG

5) Then go to Build and Select option from drop down box, execute your build through Windows batch command

6) And give your batch file name here – “run.bat

7) Click on apply and save

8) Click on build now and see the build result on console output

So far we have learned:

As you all are aware Jenkins is a very powerful tool which is mainly used for running nightly builds. Hence, we shall now learn how to schedule your build and send email notifications to the concerned team.

Scheduling  Jenkins job

For scheduling you batch file, perform the below steps:

I entered * * * * * which means it will run my job every minute

There is no manual intervention. After scheduling the script, it will run on the scheduled time.

How to add email notifications

Next, we will cover how to add email notifications.

Refer the below steps:

Running Selenium script through command line

We will now see how we can run Selenium script through command prompt. This part has nothing to do with Jenkins. I am sharing this to give extra insights on Selenium.

Follow below steps:

E:\Automation_workspace\Demo-testNG > set classpath = E:\Automation_workspace\Demo-testNG\bin;E:\Automation_workspace\Demo-testNG\lib\*;

Sometimes while executing your script you may face error which says, “Could not find or load main class org.testng.TestNG”

Then you need to close your command prompt and again set your class path as mentioned above and repeat the same steps. Your error will get resolved and the script will run.

Conclusion

Integration of Jenkins with selenium provides you to run your script each time there is any change in software code and deploy the code in a new environment. With Jenkins, you can save execution history and test reports.

In short, Jenkins is very useful when you have test cases ready and you want them to run using a single click. We can create or schedule a build to run the test cases using a batch file.

Further reading => Integrate Selenium with Maven project

If you have any queries using Jenkins with Selenium, feel free to post your queries in comments below.