Step by step guide to Setup and Configure Jenkins with Selenium:
Our previous tutorial in the DevOps Series explained about Ansible Roles and Integration with Jenkins in DevOps.
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.
Suggested Read => Precise DevOps Training Tutorial Series
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:
- 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 the 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 a 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.
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 the path of a .war file
(Note: Click on any image for enlarged view)
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(""); //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 #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 a .class file which is located at bin directory and all our executable jar file is located at 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 is created properly, double-click the batch file and it will execute the code. Refer the below code of batch file:
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
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
Note: This tutorial is a part of Selenium as well as DevOps tutorial series. Click the below link for previous and next tutorials from the DevOps series.
PREV Tutorial | NEXT Tutorial
Feel free to post your queries in the comments section below.
15 thoughts on “Integration of Jenkins with Selenium WebDriver: Step-by-Step Tutorial”
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”
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
What about integrating Appium in Jenkins for Mobile Automation?
Could you please show me how you start appium in jenkins?
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.
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
This is very good tutorial even liable person understand entire process very easily , one of the best tutorial i fallowed. Thanks a lot sir.
Got the error as “Error: Could not find or load main class org.testng.TestNG”.
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”.
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?
Getting this error message in console output – No such file: C:\Program Files\Java\jdk1.8.0_131\5\log. please help me
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
• 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.
can anyone help on above task
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
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)