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.

Integrat Jenkins with Selenium WebDriver

 

In this article we covered the below points:

  • Jenkins usage and integration with selenium
  • Creating a batch file and using it Jenkins
  • Scheduling Jenkins job and added email notification
  • And running selenium script from command line

Advantages of using Jenkins are:

  • It is a cross-platform and can be used on Windows, Linux, Mac OS, and Solaris environments
  • It is a free and open source tool
  • Widely used and well documented
  • Integration with wide variety of tool and technologies

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

  • Anthill
  • Bamboo
  • Cruise Control
  • Team City and many more.

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)

java –jar

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

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.

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:

  • Go to Jenkins dashboard
  • Click on manage Jenkins
  • Click on configure Jenkins
  • Click on JDK installation – In JDK name section enter the name, under Java Home section – give your java path

JDK installation

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:

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:

TestNG xml file

Step #4:

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

Refer the screenshot below:

library folder

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

add all jar files

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:


windows 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

a new job in Jenkins

3) Click Advance options

advance options

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

use custom workspace

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

run.bat command

7) Click on apply and save

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

console output

So far we have learned:

  • How to start Jenkins
  • How to configure Jenkins with Selenium
  • Creating your batch file and executing it through Jenkins.

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:

  • Go to dashboard and click on the Jenkins job
  • Click on configure and then on advanced  option
  • Then go to Build triggers and select Build periodically option and enter your cron job pattern

Build triggers

  • To understand cron job pattern follow this wiki link

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

  • Click on apply and save

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:

  • Go to the section ‘Manage Jenkins’
  • Click on configure system
  • Select Email notification

Email notification

  • Give your SMTP server address. I am using Gmail, as I can’t mention my official server address. To know your official server address, contact your network support team
  • I entered SMTP server name = smtp.gmail.com
  • Click on the advance link and check Use SMTP Authentication check box
  • Provide username, password and SMTP port number; it is 465 for Gmail. Check charset and make sure it is = UTF-8

Use SMTP Authentication

  • Check your email configuration settings by clicking on Test configuration button.
  • So, whenever the build passes or fails you will get the email notification.

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:

  • Open your command prompt and go to your project base path

project base path

  • Set class path for your script file; which means we are specifying that our binary and library files are stored in this location

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

Set class path

  • Execute your testng.xml file by typing the command – java org.testng.TestNG testng.xml

Execute your testng.xml file

  • When you press enter your script will start executing and you can see the test result in the UI

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

face error

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.




Recommended reading

8 comments ↓

#1 Prashant

Interviewer Question-:
“In your organization your team got four requirment from client and team has to deliver the same as a Tester you reported one bug but developr marked as “Deferred State” what you will do? How you will convince your test manager? How can we put a one bug in “Deferred State” in this small release if you enough time develop and test the application ? the interviewers opnion was you can not put one bug in “Deferred State” how can you argu interviewer”

#2 Braj

Hi first of all I would like to thank you for putting together a very nice article.

I just had few questions related to Jenkins and SVN

1. How would you run a particular SVN revision number from through Jenkins or how can you add it to bash.

2. How would you run multiple SVN tags from one Jenkins job

3. Is there a way to select different machines from bash file

#3 Ranjith

What about integrating Appium in Jenkins for Mobile Automation?
Could you please show me how you start appium in jenkins?

#4 Azharuddin Khan

Hi,
Instead of Local Workspace, i need to use SVN .. How i can do that using jenkins.
Since in this example,i am seeing Browser is not appearing while running jenkins job. how we can make it browser visible.

#5 Konstantin Firsanov

This is a very good post, giving step by step guide of configuring Jenkins with Selenium webdriver. If someone writes auto-tests in Python, using PyTest framework, the guide for configuring CI environment, that includes Jenkins, Selenium webdriver python and Allure – the powerful reporting tool – is here https://www.blazemeter.com/blog/how-automate-testing-using-selenium-webdriver-jenkins-and-allure

#6 Rajesh Podugu

This is very good tutorial even liable person understand entire process very easily , one of the best tutorial i fallowed. Thanks a lot sir.

#7 farooq

Got the error as “Error: Could not find or load main class org.testng.TestNG”.

#8 farooq

I am able to execute the same through testng.xml from eclipse but when I configured it in jenkins or running through command prompt I got the error as “Error: Could not find or load main class org.testng.TestNG”.

Leave a Comment