• info@helpingtesters.com
  • helpingtesters

SUBSCRIBE TO OUR NEWSLATER





Integration of Jenkins with Selenium WebDriver

January 10, 2018
Jenkins, Continuous Integration, Jenkins in Continuous Integration, Jenkins and Selenium WebDriver, Jenkins and WebDriver

In this tutorial, we will learn about Continuous Integration using Jenkins. There are a lot of tools which can implement Continuous Integration in which Jenkins is the most popular. 

In this tutorial, we would be covering the below points

 

 

  • Introduction to Jenkins
  • Why we use Jenkins
  • Downloading and Installing Jenkins
  • Configure Jenkins with Selenium
  • Running selenium script from command line
  • How to add Email notifications

Introduction to Jenkins

Jenkins is an open source server tool build with Java application which allows continuous integration and continuous delivery of projects. As it is a Java Application it is platform independent which does not require any specific platform. We can use it the same way whether we are on windows are on MAC are any other operating system. It is used to build and test software projects continuously and makes it easier to integrate changes made to the project.

Common uses of the Jenkins

  • Deployment of the released artifact with custom scripts.
  • Support for scheduled builds and for automation test execution.
  • Builds snapshot and release artifacts for your application.
  • Can run scripts on remote machines
  • Send notifications of your test report.
  • Schedule your automation at any time.

What is Continuous Integration?

Continuous Integration is a process in which the developers are required to commit changes to the source code in a shared repository like Git several times a day or more frequently. On commit made in the repository built is made which allows the team to detect the problems as early as possible.

What is Jenkins?

Jenkins is an open source automation tool written in Java that allows Continuous Integration developed by Hudson lab. Jenkins is used for Continuous Integration, it means that multiple people can deploy on the server and do the development. It will be installed on a server where the central build will take place and it will keep an eye on the repository whether the build is successful or not when multiple people are checking-In and checking-out from the server.

Why Jenkins Used

  • For continuous integration and continuous delivery.
  • generally, when we work in teams we have different developers who develop the code and keep on working by changing or enhancing the code.
  • As the developer works on his own machine and changes the code, developers will check-in the code into the shared repository like GIT, SVN etc..
  • When developers change the code and check-in the code into the shared repository.
  • At the end of the day when we have a bug in the code the build fails.
  • it will be very difficult for the developers to identify where exactly code fails and at what point in time.
  • It will be difficult to go back and check all the code which was created on that day which leads to a lot of confusion, so here Jenkins comes in to picture to resolve this problem.
  • As soon as if any developer commits the code into the shared repository, it will take that latest code and trigger a build and the build notification will send out in case if there is any problem or error with the build and will be notified as soon as build gets triggered and build completes.

Another Practical Example: Why we use Jenkins

  • Before moving forward first we need to know about SVN(Subversion) or GIT. It is basically a tool which could maintain our code in a team.
  • Suppose if there are multiple members in our project like Member1, Member2, etc……..they can access the project which is lying on a server on which our selenium code is lying. We can download the code on to our local machine and start working on it.

  • if Member2 uploads a New code, then all the other members can download the latest version of the code and start working on it.
  • This is what Git/SVN do, they help us to maintain the version of our code on the server as well as multiple people. While working, they can download the code from the server or the repository.
  • But the problem we generally face when using SVN is, suppose Member1 downloads the code on to his machine, say 3 Java files and same is the case with Member2, downloads same 3 Java files on his machine.
  • Now, Member1 makes changes on his machine as per his needs who doesn’t know that Member2 also doing changes in the same files, where both of them are not interacting with each other.
  • When Member1 modifies the files everything will work fine on his local system and similarly, member2 makes the changes and everything will work fine on his machine.
  • When Member1 checks in the code back in the repository then everything starts working where the status of the repository will be “OK”.
  • But, Member2 isn’t aware of the changes made to the file by member1, so as he checks-in the system “FAILS”.
  • If the build fails on the server then our project will be unstable, where nothing is going to run and when in near future Member3 who doesn’t know that build failed on the server can download the code which is not working into his workspace which leads to a lot of confusion.

How does Jenkins help in Continuous Integration?

 It builds the system when deployment is done

  • If the build fails, Jenkins can send immediate emails to people working on the project.
  • There is no Time lapse if the build fails immediately the email goes to the people in the project that build has failed where we need to look to this at a very urgent basis so that people can start working on it.
  • Periodically build our projects, where we can schedule builds that every 12’o clock in the night the build should happen or it should happen in every minute or every hour.

Advantages of Jenkins

  • It is an open source tool with great community support.
  • The installation makes easier.
  • It has more than 1000+ plugins to make the work easier.
  • It is built with Java and hence, it is portable to almost all the major platforms.
  • It provides support for scheduled builds & automation test execution.
  • It can be used to Publish results and send email notifications whenever build pass/fail.
  • It can execute ANT and Maven Targets.
  • We can link Jenkins with Git & SVN.

Downloading and Installing Jenkins

Step 1: Go to Google and type Download Jenkins

Step 2: Click on the link https://jenkins-ci.org

Step 3: Click Download button

Step 4: Click on Past Releases

Step 5: Click on 2.73.3 version and Download Jenkins.war

 

Step 6: Go to the location of the war file where it is located on My PC it is D:\> java -jar Jenkins.war

Step 7: Open the command prompt and type java –jar and enter path of .war file

Step 8: Press enter and check if your Jenkins.war file started to run and check the status information on the command prompt console. Every time when we want to start, we need to provide below command in the path where exactly our Jenkins lies and it starts running Jenkins D:\> java -jar Jenkins.war

 

 We can see that Jenkins is fully up and running

  

Step 9: Now check whether your Jenkins is ready to use. Open up the browser and type the URL localhost:8080 and click Enter and we see the window as below as it will show you Jenkins UI. By default, Jenkins will start at 8080 port no. It will load the Jenkins dashboard empty by default.

Configure Jenkins with Selenium

Step 1: To use Selenium with Jenkins you need to configure Jenkins with Selenium. Open up the browser and type the URL localhost:8080 and click Enter and we see the window as below

 Step 2: To Configure Jenkins click on Manage JenkinsStep 3: Click on Configure System As of now we have to give only Java path so that Jenkins can identify where exactly our java is installed.

Step 4: Navigate to JDK section and Click on Add JDK button. Click on JDK installation – In JDK name section enter the name, under Java Home section – give your path to Java where it stored.

Step 5: Un-check Install automatically checks box so Jenkins will only take java which we have mentioned above.

 

Step 6:  Give the JDK name as JAVA_HOME and Specify the JDK path

 

Step 7: Once done click on Apply and Save.

Step 8: Your Jenkins is now 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 9: Open Eclipse and Create a New Project

Project name: Jenkins

Step 10: Create a Package under Project

Name: Jenkins_Sample

Step 11: Create a Class under Package

Name: Jenkins

package Jenkins_Sample;

import org.testng.annotations.Test;

public class Jenkins {

@Test

public 
void testJenkins() 
{

WebDriver driver = new FirefoxDriver();

driver.get(“http://www.facebook.com”);

System.out.println(driver.getTitle());

driver.quit();

}

}

Create an XML file, to configure it with batch file

 


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

<test name="Test">

<classes>

<class name="Jenkins_Sample.Jenkins"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite →

Right Click testng.xml->Click Run As->Click TestNG Suite

Output: Welcome to Facebook-Log In, Sign Up

Step 12:  Running xml file through command prompt. Go to your project root directory and create a library folder and place all library files

Step 13: Add all your jar files which are required for running your Selenium script:

Step 14: Open command prompt->Go to directory and set the classpath. While setting classpath we will set the path of bin folder and libs folder (inside libs we have all libraries)

C:\Workspace\JenkinsDemo> set classpath=C:\Workspace\Jenkins\bin;

Specify the path for library location

C:\Workspace\JenkinsDemo> set classpath=C:\Workspace\Jenkins\lib\*;

Step 15: Execute testng.xml file through command prompt

Creating a Batch file and using it in Jenkins

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

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 the TestNG framework so specify org.testng.TestNG. Also, specify the name of xml file, in our case, it is testng.xml

Step 2: Save the file in double coats with the .bat extension. In my case, I have saved as run.bat

Step 3: To verify whether the batch file is created properly, double-click the batch file. In my case, it is run.bat and we can see that batch file running.

Step 4: Adding a batch file in Jenkins. Go to the Jenkins dashboard, and click on New Item to create a new job in Jenkins

Step 5: Enter an item name and select the freestyle project and Click OK button

Step 6: Click Advanced options

Step 7: Click on use custom workspace checkbox and give your Selenium script project workspace path: C:\Workspace\JenkinsDemo

Step 8: Then go to Build and Select “Execute Windows batch command” option from drop down box

Step 9: Give your batch file name here – run.bat  and click on Apply and Save

Step 10: Click on build now and see the build result on console output

Step 11. Check Build history and click Console output

Step 12: Verify the output

How to add Email Notifications

Step1: Go to the Jenkins dashboard, and click on ‘Manage Jenkins’

Step 2: Click on configure system

Step 3: Navigate to Email notification

Step 4: Click on Advanced button

Step 5: Give your SMTP server address

  • Click on the advance link
  • Check Use SMTP Authentication check box
  • Provide Username, password
  • Check Use SSL check box
  • Give SMTP port number; it is 465 for Gmail.
  • Check charset and make sure it is = UTF-8
  • Check Test configuration by sending test e-mail checkbox

  • Click on Test Configuration
  • Whenever the build passes or fails you will get the email notification.

I have been in software testing for a decade. Maintaining HelpingTesters.com website to make a common platform to share my knowledge with everyone or where others can share to help out other testers. If you are willing to help than let me know at info@HelpingTesters.com

About the author

HelpingTestersTeam administrator

I have been in software testing for a decade. Maintaining HelpingTesters.com website to make a common platform to share my knowledge with everyone or where others can share to help out other testers. If you are willing to help than let me know at info@HelpingTesters.com

Leave a Reply

Your email address will not be published.