How to send SMS / Email Notifications when a test case fails

So for a few weeks I have been working on expanding my code coverage, and looking into all sorts of solutions for an additional notification stream for a potential emergent issue.

Long story short, I think I have found a pretty sweet solution thats super simple to set up, with the Gmail Plugin.

So the first thing you will want to set up is your Rest API object, its simple enough to do.
WebService Request Documentation

Step 1: Right click “Object Repository”

Step 2: Select “New” → “Web Service Request”

Step 3: Name and select the type of request, for this example I used a RESTful → Click “OK”

Step 4: Set up your Web Service Object

CONFIGURE WEB SERVICE REQUEST EXAMPLE

My RESTful API Test object is CALLED “RateFinder”

This API call its super simple, there is only 1 variable for the specific state. that variable doesn’t need to be defined within the API, in this instance my variable is a Parameter on the URL string, then my headers and a JSON body to send with the request.

URL
POST: BASEURLGOESHERE

Query Parameters
Name: Insert variable name such as: varStateAbv
Value: Insert variable name with syntax for Rest API such as: ${varStateAbv}

HTTP Header Tab

You will need to adjust this to match your API specifically, for my example I have my Content type and api key.

HTTP Body Tab

{   
	"StateCode": "${varStateAbv}",   
	"ExgState": "",   
	"SmartLinkCode": "" 
	
}

Verification Tab

import static org.assertj.core.api.Assertions.*
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webservice.verification.WSResponseManager
import groovy.json.JsonSlurper
import internal.GlobalVariable as GlobalVariable

RequestObject request = WSResponseManager.getInstance().getCurrentRequest()
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()
assertThat(response.getResponseText()).contains('**INSERT EXPECTED JSON HERE**')
assertThat(response.getResponseText()).contains('**INSERT EXPECTED JSON HERE**')
assertThat(response.getResponseText()).contains('**INSERT EXPECTED JSON HERE**')
assertThat(response.getResponseText()).contains('**INSERT EXPECTED JSON HERE**')
assertThat(response.getStatusCode()).isIn(Arrays.asList(200, 201, 202))
println (response.getResponseText())
println (response.getStatusCode())

Next I built my Custom Keywords for date and time. I decided to break them out instead of the traditional timestamp. but you can set yours up however you want.
Custom Keyword Documentation

Step 5: Right click “Keyword”

Step 6: Select “New” → “Package” and name the package you want to host your keywords, for this example I used “Special”

Step 7: Rick click your new package i.e “Special” and select “New” → “Keyword” and name your keyword what ever you want, I named mine “CurrDate” and another “CurrTime”.

CONFIGURE CUSTOM KEYWORDS

Custom Keyword for Date with formatting

package special

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Date
import internal.GlobalVariable

public class CurrDate {

	@Keyword
	def now = new Date()


	def timeStamp(){
		//String NOW = now;
		SimpleDateFormat timestamp = new SimpleDateFormat("MMM dd yyyy")
		return timestamp.format(now)

	}
}

Custom Keyword for Time with Formatting

package special

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Date
import internal.GlobalVariable

public class CurrTime {

	@Keyword
	def now = new Date()


	def timeStamp(){
		//String NOW = now;
		SimpleDateFormat timestamp = new SimpleDateFormat("hh:mmaa")
		return timestamp.format(now)

	}
}

Next we build the test steps, you can do this in manual or script mode. Personally I like scripted better

Step 8: Right click “Test Cases”

Step 9: Select “New” - > “Test Case”

Step 10: Configure your Test Case!

There will need to be a few fields you have to fill from the CustomKey/Plugin word made by @Hari which can be found here. Gmail Plugin

Configure your Test Case

I like to add my variables first so Click on the “Variables Tab” and add your custom variables as needed. For this example we have varStateAbv with a Default value of “CA” and varState with a default value of “California”

Next go to the script tab and insert this script. Keep in mind you will need to edit fields, and test steps to match your specific needs.

API - SMS/EMAIL TEST CASE SCRIPT

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import static org.assertj.core.api.Assertions.*
import com.kms.katalon.core.testobject.ResponseObject as ResponseObject
import com.kms.katalon.core.webservice.verification.WSResponseManager as WSResponseManager
import groovy.json.JsonSlurper as JsonSlurper
import special.*

def date = CustomKeywords.'special.CurrDate.timeStamp'()
def time = CustomKeywords.'special.CurrTime.timeStamp'()


try {
    WS.sendRequestAndVerify(findTestObject('API/RateFinder', [('varStateAbv') : varStateAbv]), FailureHandling.STOP_ON_FAILURE)
	
}
catch (Exception e) {
	
    CustomKeywords.'com.testwithhari.katalon.plugins.Gmail.sendEmail'('**ENTER FROM EMAIL ADDRESS HERE**', 'ENTER FROM PASSWORD HERE', 
        '**INSERT_PHONE_NUMBER_HERE@vtext.com, **ADD ANY OTHER NUMBER OR EMAIL ADDRESS**', 'Failed to find plans for: ' + varState + ' at '+ time + ' on '+ date, 'The Rate Finder API has failed to find any plans for: ' + varState + '.  Please sign into katalon analytics to check the logs. If there is an outage notify ...')
	
} 

This test script is simple, the “Try” will execute my API Request, and run the verification that I set up on the WebService Object. If it fails, the catch will trigger the sendMail custom keyword/plug that was built by @Hari from there you just simply need to fill in the parameters and VIOLA you have a SMS alert system for when your API fails.

We set this up to run on Test Ops at very regular intervals, I have a MySql Datafile that has all of the states we need to test against to find the rates. It takes about 50seconds-1min to run through 47 iterations of the API Request, and as soon as any fail I get an email, and a text so I can investigate. The message sent via Text seems to be limited to only 145 characters.

How does an email plugin send a SMS Text?

Many people don’t realize you can send SMS from an email, its a super simple/easy way to get great notifications. You just need to know the phone number, and the cellphone carrier so you can get the syntax to send email. In my test I used my number which is on verizon.

List of carriers and the syntax to send a text message.

  • Alltel: phonenumber @message.alltel.com
  • AT&T: phonenumber @txt.att.net
  • T-Mobile: phonenumber @tmomail.net
  • Virgin Mobile: phonenumber @vmobl.com
  • Sprint: phonenumber @messaging.sprintpcs.com
  • Verizon: phonenumber @vtext.com
  • Nextel: phonenumber @messaging.nextel.com
  • US Cellular: phonenumber @mms.uscc.net
3 Likes