Saturday, July 15, 2017

How to schedule Selenium tests in Jenkins and Send Email Notification?

Cron Pattern:

Jenkins uses Cron Pattern to schedule the test/build executions. It is used for specifying the time, it is a five field structure (* * * * *).

Based our requirement the fields are set to run the tests periodically or at regular intervals of time. The Cron Pattern fields are defined below:


Setting up values decides the execution period let us some example below.

Example 1: To run the tests for every 10 mins, Pattern = H/10 * * * *

Example 2: To run the tests 10th minute of every hour, Pattern= 10 * * * *

Example 3: To run the tests at 6 AM, Pattern=H 6 * * *. To run the tests at 7 PM, Pattern=H 19 * * *

Example 4: To run the tests for every 5 hrs, Pattern= * H/5 * * *

Example 5: To run the tests at 10.30 PM on Sunday, 30 22 * * 0. This will trigger tests on every Sunday at 10.30 PM

Example 6: To run the tests at 5 AM from Monday to Friday, Pattern= 0 5 * * 1-5

Enough examples I guess, You can write the based your requirement.

Scheduling Selenium Tests:


Assuming a batch file and testng.xml are created already for a sample selenium test. Otherwise please refer  my previous post How to execute Selenium Test Cases from Command Prompt in TestNG?

As a prerequisite Jenkins should be configured with Gmail before proceeding further. If it is not configured please refer Configuration of email (Gmail) in Jenkins.

Step 1: Below is the Selenium test I am considering to Schedule. And I am making this test intentionally to Fail. We will come to know why we are making the test to fail.


Step 2: You can see the batch file "build.bat"I have created in project home directory in the below screen shot.


Step 3: Launch Jenkins and create a new Job say scheduleTestDemo.

Step 4: Click on Configure.


Step 5: Enter some description and click on Advanced.


Step 6: Select Use custom workspace and enter the project home directory path in the Directory text filed.


Step 7: Go to Build section and then select Execute Window Batch Command from the drop down list.


Step 8: Enter batch file name build.bat in the command text filed. Click Apply button.


Step 9: Now Go to Post build actions section and Select Email Notification from the drop down list.


Step 10: Add the recipient email id and click on Apply.


Step 11: Scroll down to Build Triggers section and Check Build Periodically checkbox. And I want my test to run for every 5 minutes, enter the Cron Pattern as shown in the below. Click Apply and then Save. The build/test execution starts...


Step 12: Please observe the build execution timing in the Build History, for every five the tests are executed. 


Step 13: Now check the recipient's email account, since each time the tests are failed an email notification has been sent.


Important point to be noted here is Jenkins will send the notification mails only when Build fails, it doesn't send emails if Build Passes. This is the property of Jenkins. That's why we are making our test to fail in the script. Refer the below screen for more details.


Hope this information is helpful to you. Please share with your friends. Thanks!!

#HappyLearning #HappyTesting

No comments:

Post a Comment

DevOps 01: What is DevOps and How it benefits organizations?

DevOps is a culture in an organization, where the development team and operations team help each other by sharing information, process and t...