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

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 2, 2024

We have provided a step-by-step Guide to Setup and Configure Jenkins with Selenium:

Our previous tutorial in the DevOps Series explained Ansible Roles and Integration with Jenkins in DevOps.

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

Suggested Read => Precise DevOps Training Tutorial Series

Jenkins is very useful and supports many plugins that you can integrate with such as Git, SVN, build pipelines 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, if you want to base the execution on time you can run the job every 20 minutes or if you want to base it on an event you can do so after a new commitment in a Git repository.

Integration of Jenkins with Selenium WebDriver

Integration Of Jenkins With Selenium WebDriver

 

In this article we covered the points given below:

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

Some of the Advantages of using Jenkins are:

  • It is 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 a wide variety of tools and technologies

Apart from Jenkins, we also 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 on a web application server.

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

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

(Note: Click on any image for an enlarged view)

java –jar

2) Press enter and check if your Jenkins.war file has 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.

Further Reading => Jenkins with Docker, Docker Compose, Docker Swarm

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

Jenkins UI

The Jenkins dashboard will load empty by default. I created a few Jenkins jobs 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 account 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 and refer to the code below:

TestNG xml file

Step #4)

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

Refer to the screenshot below:

library folder

Now, add all the jar files that are required to run your Selenium script:

add all jar files

Step #5) 

Create a batch file and use it in Jenkins

Create a batch file by following the steps below:

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

By doing this, Java –cp will compile and execute a .class file which is located in the bin directory and all our executable jar files are located in the lib directory and we are using a 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 was created properly, double-click the batch file and it will execute the code. Refer below code for batch file:

windows batch file

Step #6:

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

To add a batch file, follow the steps below:

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

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

a new job in Jenkins

3) Click Advanced 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 an option from the drop-down box and 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 the build now and see the build results on console output

console output

So far we have learned:

  • How to start Jenkins
  • How to configure Jenkins with Selenium
  • Create and execute your batch file 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

To schedule your batch file, perform the steps below:

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

Build triggers

  • To understand the 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 at the scheduled time.

How to add email notifications

Next, we will cover how to add email notifications. 

Refer below steps:   

  • Go to the “Manage Jenkins” section.
  • 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 the SMTP server name = smtp.gmail.com
  • Click on the advance link and check the Use SMTP Authentication check box
  • Provide username, password and SMTP port number; it is 465 for Gmail. Check the charset and make sure it is = UTF-8

Use SMTP Authentication

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

Run Selenium script through command line

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

Follow the below steps:

  • Open the 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 will see the test results in the UI

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

face error

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

Conclusion

The 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 and effective 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

Note: This tutorial is part of Selenium as well as the DevOps tutorial series. Click the link below for previous and upcoming tutorials from the DevOps series.

PREV Tutorial | NEXT Tutorial

Feel free to post your queries in the comments section below.

Was this helpful?

Thanks for your feedback!

Recommended Reading

19 thoughts on “Integration of Jenkins with Selenium WebDriver: Step-by-Step Tutorial”

  1. 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

    Reply
  2. 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”. Pleae help me with a solution

    Reply
  3. This is very useful info.

    I m trying to explore more on jubula apis usign an application called squirrel(https://sourceforge.net/projects/squirrel-sql/) owever, I am able to open the application using AUTAgent but not able connect and getting the following error.

    Exception in thread “main” org.eclipse.jubula.client.exceptions.CommunicationException: java.net.ConnectException: Could not connect to AUT: app123.
    at org.eclipse.jubula.client.internal.impl.AUTImpl.connectImpl(AUTImpl.java:128)
    at org.eclipse.jubula.client.internal.impl.AUTImpl.connect(AUTImpl.java:113)
    at framework.RS.main(RS.java:36)
    Caused by: java.net.ConnectException: Could not connect to AUT: app123.
    at org.eclipse.jubula.client.internal.impl.AUTImpl.connectImpl(AUTImpl.java:129)
    … 2 more

    My Code:
    public static void main(String[] args) throws InterruptedException {
    AUTAgent autagent = null;
    AUTConfiguration autconfiguration = null;
    AUTIdentifier id ;
    AUT aut = null;
    autagent = MakeR.createAUTAgent(“127.0.0.1”,60000);
    autagent.connect();
    String autId = “app123”;
    String name = “app”;
    autconfiguration = new SwingAUTConfiguration(name, autId,”squirrel-sql.bat”, “C:\\Users\\VenkataK\\Documents\\SQL”,null);
    id = autagent.startAUT(autconfiguration);
    Thread.sleep(60000);
    if(id!=null) {
    aut = autagent.getAUT(id, SwingComponents.getToolkitInformation());
    Thread.sleep(60000);
    aut.connect(60000);
    }
    }

    Reply
  4. Hi Ramya

    I also came across the same issue and got it fixed

    Just ensure 2 things:

    1. First: There should be NO space after semi colon between “bin;” and “lib/*”
    your command should be like this:
    java -cp bin;lib/* org.testng.TestNG testng.xml

    2. Second: The lib folder should contain selenium jars and testng jars

    Reply
  5. 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”

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

    Reply
  7. Hi
    What would be the bat file if the test project isn’t a TestNG project(Like Simple Selenium JAVA Project or junit or MAVEN project)

    Reply
  8. Getting this error message in console output – No such file: C:\Program Files\Java\jdk1.8.0_131\5\log. please help me

    Reply
  9. Hello,
    I have one question regarding to open real browser from jenkins why we need to use .war file in windows?
    why cant we simple run .exe file in windows?

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

    Reply
  11. • After we do deployment , A sanity test script will run automatically. This will report that the build we deployed has passed / failed the sanity script.

    b) For the sanity test script we will need the automated script from the test team so that it can be integrated in the pipeline.

    Reply
  12. 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”.

    Reply
  13. I need a .bat file for the below process. Could you please help.
    1. My scm is bitbucket-> so i gave my bitbucket url/credentials in jenkins.
    2. I have to navigate to the project in my bitbucket repo and execute mvn command (clean install)
    3. Then i have to navigate to a sub project inside it and again execute mvn command (clean test)

    Reply
  14. Hi, For this example, You use the local machine for installation of Java. if we want to run our test cases without local machine, what should we do?

    Reply
  15. 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.

    Reply

Leave a Comment