How to Integrate your Selenium Project with Jenkins

Chaya Thilakumara
11 min readJun 8, 2018

--

In this article, we will examine what Jenkins is, and how to set it up. How to integrate selenium with Jenkins and some of features Jenkins provides can also be glanced through.

Jenkins is open source java tool which helps in all types of software tasks such as building, testing, and deploying. It is a cross platform and can be used in windows, Mac OS and Linux etc.

Integration of Jenkins with selenium helps you to run your scripts whenever there is a change in software code. Jenkins helps to schedule your build to run the test cases from a batch file, save your test reports and to save the execution history. Using Jenkins we could also publish results and send email notifications to all the concerned team members.

Jenkins provides continuous integration and continuous delivery service for software development.

How to set up Jenkins locally in your computer….

Download the latest .war file from Jenkins official web site. https://jenkins.io/download/

  1. Open command prompt and type java -jar and type the path of .war file or drag and drop Jenkins .war file.

Command : java -jar <path of the .war file>

Command : java -jar <path of the .war file>

Press enter to run Jenkins .war file. After running the Jenkins .war file completely, check the status information in the command prompt console as “Jenkins is fully up and running”.

Status in the command prompt console : “Jenkins is fully up and running”

2. Your Jenkins is ready to be used from “http://localhost:8080”. Enter the URL and press enter and then, you will see Jenkins UI.

Jenkins URL : http://localhost:8080

After installing Jenkins, let’s configure Jenkins with Selenium.

3. Go to Jenkins dashboard, and click on Manage Jenkins tab. Then, click on Global Tool Configuration, and click on JDK installation. Provide JDK name under JDK Name section, and provide your java path in JAVA_HOME section.

Global Tool Configuration: JDK installations

Uncheck the radio button, “Install automatically”, as there could be a possibility that selenium doesn’t support, if new java version is update. Then, Save and Apply the changes.

4. Create your Selenium script and a TestNG XML file.

As a sample, I have provided the TestNG code below.

Sample TestNG code

To create XML file go to New > Other and select XML and press Next.

New > Other > Select XML

Provide the File name and select the folder you want to create the XML document.

As an example File name : SeleniumTesting.xml and the folder : under src folder

Create a TestNG xml file, refer the code below :

Class name in the xml file should be the qualified class name. Highlight the class name, and right click “copy qualified Name” to copy the qualified Name.

Highlight and right click on the class name to copy the qualified name
As an example : “seleniumTestSampleTestcases.LogInFindMyFare” is the qualified name

This line is called the XML prolog:
<?xml version=”1.0" encoding=”UTF-8"?>

This line is optional, and it should come first in the document, if it is available in the text. XML documents can contain international characters like à, è, ì, ò and ù. So, UTF-8 is there to encode those characters for XML document.

A <suit> is the top level tag which represents a suite, and there are some more tags like <classes>, <packages>, <groups> in an xml file. The name is one of the attributes which represents the name of the suit, classes and packages.

You can denote package name instead of the class name.

Parallel attribute specifies whether the TestNG should use different threads to run your tests. Accepted values are (false | true | none | methods | tests | classes | instances).

Skipfailedinvocationcounts attribute specifies whether to skip failed invocations. Accepted values are (true | false).

Verbose attribute is used in the <suite> tag in the xml file.

The verbose accept the values from 1 to 10. As an example : verbose=”1". If you enter small number for verbose, you will get less details, for the test results in the Eclipse IDE console. If you want to get more test results, you need to assign a larger number.

Lets see, how can we use verbose attribute to get the log details in the Eclipse IDE console

verbose=”1"
The log details of the test results that are displayed in the console with a very short info as shown in the image below. From verbose one, the test suite name, total number of tests run, failure number of test cases and skip test cases are displayed.

Now, let’s see the test results for verbose “2”. This time we got more test result details in the console. I got test name as an additional log detail.

5. Create a library folder.
Go to your project directory, and create a library folder (lib), and add all your jar files which are required to run your Selenium script.

jar files which are required to run Selenium script.

6. Create batch file

run.bat file

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

Java -cp command will execute all the .class files in bin and all jar files in lib.

.class file in bin folder

TestNG framework is specified from the org.testng.TestNG command.

Save the file with bat extension, and it should be a “windows batch file”.

Double click the file “run.bat” to run the script

7. Add bat file to jenkins

To create new job in jenkins, click on create new jobs, enter an item name, select Freestyle project, and enter OK button. Go to Advance option, and tick on Use custom workplace, and provide Selenium script project Directory path. As an example: “C:\Users\Gihan\chaya\Ustock”.

Scroll down, and go to Build tab, and Select the option “Execute Windows batch command” from Add build step drop down box. This option executes your build through Windows batch command.

Give your batch file name in “Command” text box — run.bat

Click on Apply, and Save. Then, click on Build Now, and see the build result on console output.

Console Output
Test results in the console

Running nightly builds!!!

Let’s look at how to schedule your build, and send emails to concerned teams.

8. Go to dashboard, and click on the Jenkins job, enter an item name, select Freestyle project, and press OK button. Scroll General tab, and tick Build periodically checkbox under Build Triggers.

9. Enter your cron job pattern.

Cron job.. what it means !!!

Cron can run commands on a schedule you specify over and over again. As an example, you may need to run your backup every day at a particular time, and in that situation, Cron job helps you to schedule your job easily.

Let me explain a cron job using an example :

(i) First field is the minute field, this specifies how many minutes pass the hour that you want the command to be run.

(ii) The next field is the hour field, this specifies the hour of the day when you want the command to be run.

So, basically these two fields together specify the time of the day when the command should be run. In this case, I needed the command to be run in 5 minutes after 11 PM.

(iii) The next field is the Day of the month your command to be run. Notice here I have used an asterisk character, and that means everyday day of the month I want the command to be run. If you want to run your command on a particular day of the month, you can use day of the month here. For an example, if you want to run your command on the 17th of every month, then you can put number 17 in the field.

(iv) Next field is the month field, this specifies the month of the year when the command should be run. In this case again, I have used an asterisk character, and that means I want to run the command in every month of the year.

(v) The last field is the day of the week. As far as Cron is concerned, Sunday is day zero and Saturday is day 6. So you can specify one particular day of the week you want command to be run. For an example, if I put 5, then the command will run only on Friday or I can put a range. Here, I have used 1–6 which means the command will run on Monday, Tuesday, Wednesday , Thursday, Friday, and Saturday, but not on Sunday.

Now, you know about Cron job pattern, and so, you can enter your Cron job pattern to continue to schedule Jenkins job.

I entered 5 23 * * 1–6 which means it will run on Friday, 1 June 2018 23:05:29 o’clock IST; would next run at Saturday, 2 June 2018 23:05:29 o’clock IST. Click on Apply, and Save.

Without any manual intervention, your scheduled script will run on the scheduled time.

Builds execute in every 1 minute, this is because I have used * * * * * as my cron job.

How to add email notifications

Next, we will see how to add email notifications. As the first step, let’s set all email configuration settings to send email notifications.

10. Go to the section ‘Manage Jenkins’, click on configure system, and scroll up-to Email notification tab.

  • Give your SMTP server address. I am using Gmail, and so, I have entered SMTP server name as smtp.gmail.com.
  • Click on the Advance button, and check Use SMTP Authentication check box. Provide User Name (email), Password (password of your email) and SMTP port number; it is 465 for Gmail. Check charset, and make sure that it is UTF-8.
  • You can check your email configuration settings by clicking on “Test configuration by sending test email” check box.
  • You will receive a test email, if you have set email configuration settings correctly.
Test email

Now, I have entered all email configuration settings to send emails, and let’s see how we can send an editable email notification and an email notification.

First let’s see how to send an email notification,

11. Select Configure tab under your job, select Email Notification under Post-build Actions section.

You can add recipient email address. White spaces will separate the list of recipient addresses. You can tick Send e-mail for every unstable build or Send separate e-mails to individuals who broke the build based on your requirement.

From this method you can’t send an editable email. You will receive default email for failed builds and for builds which are treated as back to normal.

Build History

Failed build : Jun 2 2012 2.54 PM.

Console output for failed builds

Display finished status as : FAILURE

Email with the default email content for failed builds

Build failed in Jenkins email: Default email content for failed builds

Send email when your build has executed successfully that means your build is successfully executed after a failure.

Display finished status as : SUCCESS

Email with the default email content for builds which are treated as back to normal

Jenkins build is back to normal email: with the Jenkins URL
Build History of Jenkins

Now, let’s see how to send an editable email notification,

12. Select Configure tab under your job, select Email Notification under Post-build Actions section.

Scroll down to get an Editable email notification, and click on Advance settings to add different Triggers required to send an Editable email notification.

Triggers: Unstable (Test Failures)/Failure -> Success, Unstable (Test Failures) — Still, Unstable (Test Failures) — 1st, Unstable (Test Failures), Test Regression, Test Improvement, Success, Status Changed, Script — Before Build, Script — After Build, Not Built, Fixed, Failure -> Unstable (Test Failures), Failure — X, Failure — Still, Failure — Any, Failure — 2nd, Failure — 1st, Before Build, Always, Aborted.

List of triggers

I have set an editable notification email for two triggers, the first trigger is if the build failed for any reason and the second trigger is if build success.

Trigger 1: Failure-Any , Trigger 2: Success

You can send email notifications to any group. As an example: to your recipient list, developers, requestor etc. Based on your requirement, you can provide email(s) for Recipient List and for Reply-To List.

Content type can be in html, in plain text or from both html and plain text. You can go through the sample content that I have given.

Attachments can be attached to the email from Attachments field, provide the path of your attachment. As an example: “**/emailable-report.html”, this is to sent the TestNG emailable report. Build Log can be attached to the email, if you set Attach Build Log drop down to Attached Build Log or to Compress and Attached Build Log.

Trigger : Failure Any
HTML email content for trigger : Failure Any
Trigger : Success
HTML email content for trigger : Success
Email for Failed Jenkins Build Information
Attached reports: Emailable report and Build log
Emailable report : Failed Build
Build log : Failed Build
Email for Success Jenkins Build Information
Attached reports: Emailable report and Build log
Emailable report : Success Build
Build log : Success Build

I hope that this post was able to assist you to implement Selenium Continuous Integrations successfully. Let’s meet with another exciting article…Till then, Happy Testing !!!!

--

--

Chaya Thilakumara

Pursue your passion, and everything else will fall into place.