Tutorial: Integration of Jenkins with Selenium WebDriver

Tutorial: Integration of Jenkins with Selenium WebDriver

In this tutorial, we will learn about Continuous Integration using Jenkins. There are lot of tools which can implement Continuous Integration in which Jenkins and Git is a part of them.

In this tutorial we covered the below points

  • Introduction to Jenkins
  • Why we use Jenkins over SVN
  • 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 Jenkins 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 anytime.

Why Jenkins Used?

Jenkins used for continuous integration and continuous delivery which means that 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 in to shared repository like GIT,SVN etc..When developers changes the code and check-in the code into shared repository and at the end of the day when we have bug in the code the build fails where it will be very difficult for the developers to identify where exactly code fails and at what point of time. It will be difficult to go back any check all the code which was created on that day which leads to lot of confusion, so here Jenkins comes in to picture to resolve this problem.

As soon as if any developer commits the code into shared repository, Jenkins will take that latest code and trigger a build and the build notification will sent 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.

Why we use Jenkins over SVN

Before moving forward first we need to know about SVN(Subversion). 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.

For example, 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. Suppose 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 every thing 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 in to his work space which leads to lot of confusion.

Problems while using SVN

  • People can modify same files at 1 time or at different time.
  • We doesn’t know that build has failed.
  • Time lapse

To overcome this situation we need a tool through which we can inform the members of the project that something went wrong on the server. That’s why we use a tool called Jenkins.

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

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 several times a day or more frequently. Every commit made in the repository is then built which allows the team to detect the problems as early as possible.

How Jenkins helps in Continuous Integration?

  • It builds the system when deployment is done
  • If the build fails, Jenkins can send immediate emails to people working in 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.
  • Using Jenkins we can 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.
  • 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

 Step1: Go to Google and type Download jenkins

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

Step3: Click Download button

Step4: Click on Past Releases

Step5: Click on 2.73.3 version and Download jenkins.war

Step6: Go to the location of the war file where it is located in My PC it is D:\> java -jar jenkins.war

 

location in your drive

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

Step8: 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 Jenkins ,we need to provide below command in the path where exactly our Jenkins lies and it start running jenkins

D:\> java -jar jenkins.war

jenkins.war

We can see that Jenkins is fully up and running

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

Step1: 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

To Configure Jenkins click on Manage Jenkins

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

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 of Java where it stored

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

Give the JDK name as JAVA_HOME and Specify the JDK path

Once done click on Apply and Save.

 

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.

Open Eclipse and Create a New Project

Project name: Jenkins

Create a Package under Project

Name:Jenkins_Sample

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

Running xml file through command prompt

Go to your project root directory and create a library folder and place all library files

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

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\*;

Execute testng.xml file through command prompt

Creating a batch file and using it in Jenkins

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

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, in our case it is testng.xml

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

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.

Adding a batch file in Jenkins.

For adding the batch file follow the below steps:

1) Go to the Jenkins dashboard, and click on New Item to create a new job in Jenkins

2) Enter an item name and select the freestyle project and Click OK button

3) Click Advance options

4) Click on use custom workspace checkbox and give your Selenium script project workspace path: “C:\Workspace\JenkinsDemo

5) Then go to Build and Select “Execute Windows batch command” option from drop down box

6) Give your batch file name here – “run.bat”  and click on Apply and Save

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

8. Check Build history and click Console output

9. Verify the output

How to add Email Notifications

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

Step2: Click on configure system

Step3: Navigate to Email notification

Step4: Click on Advance button

Step5: 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.
0

Post a comment

Login to Portal