Custom Keyword Triggered at start of Test Case

Hey guys, I have been working on expanding some code coverage, and created some testing logic for a fall back.

Example:

Zip code entered into site searching for medical plans, no plans can be found for specific zip code so a new page loads indicating that.

I have one test case that tests End to End on successful zip codes, and I have a few bad zip codes to check error handling in order to make sure the error page loads.

I have another Test case (FallBack) for testing the error page, I created some logic if a specific test step fails and the error page loads to call a different test case.

I have the test case/test suite working really well, only issue I recently discovered is when the “Fallback” test case runs from a zip code that should have passed I don’t have any sort of notification from the failure. Recently we had an outage, and the test suite said everything was solid, because in practice it was… One failed fallback ran, and verified that the error page loads.

So my solution is I want to implement a custom keyword to send an email when the fallback execution starts.

I have written the java class, and created the custom keyword, I need to test it to make sure it works, but I am unsure how to trigger/call the custom keyword. So that the email is sent when a test class starts, I am not sure on the scripting syntax since it won’t be tied to an element but to a Test Case.
CustomKeywords.'emails.email_Send(NO_IDEA_WHAT_TO_PUT_HERE)

Am I bonkers? This seems like it shouldn’t be very difficult.

Can someone point me in the right direction.

Example of custom Keyword

package emails

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 internal.GlobalVariable
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
  
public class SendEmail {

	@Keyword

 public static void main(String[] args) {
  
  String host="smtp.office365.com";
  final String user="FROM_EMAIL_ADDRESS_HERE";  //change accordingly
  final String password="FROM_EMAIL_PASSWORD_HERE";  //change accordingly
	
  String to="TO_EMAIL_ADDRESS_HERE";  //change accordingly
  
   //Get the session object
   Properties props = new Properties();
   props.put("mail.smtp.host",host);
   props.put("mail.smtp.auth", "true");
	 
   Session session = Session.getDefaultInstance(props,
	new javax.mail.Authenticator() {
	  protected PasswordAuthentication getPasswordAuthentication() {
	return new PasswordAuthentication(user,password);
	  }
	});
  
   //Compose the message
	try {
	 MimeMessage message = new MimeMessage(session);
	 message.setFrom(new InternetAddress(user));
	 message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
	 message.setSubject("Test");
	 message.setText("This is simple program of sending email using JavaMail API");
	   
	//send the message
	 Transport.send(message);
  
	 System.out.println("message sent successfully...");
   
	 } catch (MessagingException e) {e.printStackTrace();}
 }
}
    public class SendEmail {

    // If your method is static, you don't need @Keyword
    // @Keyword

    // Don't use the name *main* - it has special meaning in java, not needed in groovy
     public static void sendEmail(String[] args) {
      
      String host="smtp.office365.com";
      // other stuff here...
  }
}

In your test case:

import static emails.*

sendMail(yourArgs)

yourArgs is an array (list) of strings which you need to insert into the sendMail method.

For example, if the to argument is in the first position in the array:

// String to="TO_EMAIL_ADDRESS_HERE";  //change accordingly
String to = args[0]

hi,

this is odd
public static void main(String[] args)

do not use in custom keywords main method use like
public static void sendEmail(<parameter>, <parameter>, <parameter N>)

@Russ_Thomas I was unable to get the array list to work, just kept throwing some random office 365 error, and gmail would just throw invalid credential errors despite having double checked and verified I had the right credentials. However I was able to get this one to work flawlessly.

CUSTOM KEYWORD

package emailSends

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 internal.GlobalVariable
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*

class sendFallbackFailure {
	
	public static void sendMail(final String from,final String password,String to,String sub,String msg){
		//Get properties object
		Properties props = new Properties()
		props.put("mail.smtp.starttls.enable", "true")
		props.put("mail.smtp.host", "smtp.gmail.com")
		props.put("mail.smtp.auth", "true")
		props.put("mail.smtp.port", "587")
		
		//get Session
		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(from,password)
					}
				})
		//compose message
		try {
			MimeMessage message = new MimeMessage(session)
			message.addRecipient(Message.RecipientType.TO,new InternetAddress(to))
			message.setSubject(sub)
			message.setText(msg)
			//send message
			Transport.send(message)
			System.out.println("message sent successfully...")
		} catch (MessagingException e) {throw new RuntimeException(e)}
	}
}
public class SendMailSSL{
	public static void main(String[] args) {
		//from,username,password,to,subject,message
		sendFallbackFailure.send("From@email.com","FromPass","to@email.com","SubMsg","BodyMsg")

		//change from, password and to
	}
}

SCRIPT ON TEST CASE

> sendMail('FROM EMAIL', 'FROM PASS​', 'TO EMAIL', 
>     'This State: *~*INSERT TEST CASE VARIABLE HERE*~* has failed to load any state plans', 'This zip code: *~*INSERT TEST CASE VARIABLE HERE*~* failed to load any state plans.  Please sign into analytics.katalon.com to view the debug logs.')

I would like to use the test case variables to insert the details into the subject line and body text. What would be the best way to handle it??

Somewhere between finally getting it to work the way I want, my kid tinkering on my laptop while I made dinner and when I posted above it broke… My bet is, it was the kiddo, but what ever he broke I can’t see easily, I’ve tried to undo.

Update I found my issue this is back to working well.