How to trigger alerts from Katalon studio if condition failed

Hello,

I am new for Katalon studio. Can someone share solution for this ?

I want email alert setup code with IF condition in the script. Suppose IF condition fails, Katalon studio has to trigger to recipients list.

Currently I am getting value from application using snap value,

String value = WebUI.getText(findTestObject(‘Equip_OR/Page_/span_8282086’)

IF(value !=0) then I want to trigger alert.

Please share solution for this.

Thank you.

There is no built-in functionality, but you can implement your own. This is my solution:

public static void sendMail(String messageBody) {
    final String username = "myemailaddress@gmail.com";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("receptients@mail.com"));
        message.setSubject("Test warning");
        message.setContent(messageBody, "text/html");

        System.out.println("Sending...");

        Transport.send(message);

        System.out.println("Sent.");

    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

And then call this method in your test.

Thanks for your quick response…
I have already built in this code in Eclipse for selenium testing purpose. How can I use this email trigger code in Katalon Studio ?
Once Class method created in Katalon studio then how can I call this specific class in IF condition step ?

Thank you.

Just put the code into custom keyword and call it in the test case.

String value = WebUI.getText(findTestObject(‘Equip_OR/Page_/span_8282086’)

if(value != 0) {
	MailKeyword.sendMail("The value is not 0.")
}

Thank you so much. Could you please suggest where I need to create sendMail class in Katalon studio. I tired to create in different places - Include --> Scripts --> Packages, but its not working for me.

Could you please share step by step process to create a class method in Katalon Studio ?

Thank you so much in Advance!!

It’s under Keywords folder in left menu.

20190628-123252

You can create a package and a keyword (which is actually Java class) in it.

I created keyword class file under package, but it saved with sendMail.groovy and getting below message.

image

Error message : image

I think I need to import javax.mail jar file into katalon studio. Can you please suggest me where can I upload jar files in katalon studio to resolve this issue ?

Thank you so much.

Oh yes, sorry. You can import external libraries in main menu - Project - Settings - External Libraries. Finally restart Katalon Studio.

Now I am getting this error message when I run test case in katalon studio.

06-28-2019 05:22:17 PM Test Cases/NewTest

Elapsed time: 2m - 1.816s

Test Cases/NewTest FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: MailKeyword for class: Script1561622964037
at NewTest.run(NewTest:35)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1561722732583.run(TempTestCase1561722732583.groovy:21)

MailKeyword is only placeholder for actual class name. Replace it with your class name.

Hi Marek,

I am not able to save class file with .java, it automatically saving with .groovy in Katalan Studio.

Could you please suggest me that how can I save classfile.java in keywords -> launch --> classfilename.java ?

Thank you so much…

Actually you don’t have to use java suffix explicitly. Katalon uses Groovy, which is basically Java with added features. Just use Java code wherever you want and don’t mind. :slight_smile:

https://groovy-lang.org/

Hi Marek,

Again I received same error message, please find the below message.

Elapsed time: 1m - 54.982s

Test Cases/NewTest FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: reportTest for class: Script1561622964037
at NewTest.run(NewTest:35)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1561726442351.run(TempTestCase1561726442351.groovy:21)

and I found this message (added screenshot) for messagebody (with underline) in class java script.

image

Test case code is

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 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 internal.GlobalVariable as GlobalVariable

//import java.io.IOException;
//import java.*;

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘application URL’)

WebUI.navigateToUrl(‘redirecting another URL’)

WebUI.delay(60)

WebUI.verifyElementText(findTestObject(‘Equip_OR/Page/span_REPORTING’), ‘REPORTING’)

WebUI.click(findTestObject(‘Equip_OR/Page/button_VEHICLES’))

WebUI.delay(30)

String value = WebUI.getText(findTestObject('Equip_OR/Page/span_8282086'))

if (value != 0) {

reportTest.sendMail("The value is not 0.")

}

Can you please confirm me whether it is correct or not ?

You miss import for your new class in the test (press Ctrl - Shift - O for fix)

Underlined ‘messageBody’ variable is input parameter in my method - do you have the same one?

Hi Marek,

I applied your suggested solution —> Ctrl - Shift - O, but still I am getting same error message.

07-01-2019 02:13:13 PM Test Cases/NewTest

Elapsed time: 2m - 1.019s

Test Cases/NewTest FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: reportTest for class: Script1561622964037
at NewTest.run(NewTest:34)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1561970589788.run(TempTestCase1561970589788.groovy:21)

Yes I used the same code and Underlined ‘messageBody’ variable is input parameter in method.

The first thing you must fix is the missing import for reportTest class. You can write it manually.

This is my java class - reportTest.groovy under Launch package in Katalon studio.

package launch;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.ParameterList
public class reportTest {

public void SendMail(){
	// TODO Auto-generated method stub

	Properties props = new Properties();
	props.put("mail.smtp.host", "hostanme");
	props.put("mail.smtp.port", "port");

	Session session = Session.getDefaultInstance(props,

			new javax.mail.Authenticator(){

				protected PasswordAuthentication getPasswordAuthentication() {

					return new PasswordAuthentication("email", "password");
				}

			});

	try {

		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress("email"));
		message.addRecipient(Message.RecipientType.TO,new InternetAddress("email"));
		message.setSubject("Test warning");
		message.setContent(messageBody, "text/html");


		System.out.println("Sending...");

		Transport.send(message);

		System.out.println("Sent.");

	} catch (MessagingException e) {
		e.printStackTrace();
	}
}

}

I ran this command as per your suggestions Ctrl - Shift - O in Testcases and received the below code.

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import java.io.IOException;
import java.*;



WebUI.openBrowser('')

WebUI.navigateToUrl('application URL')


WebUI.navigateToUrl('secure URL')

WebUI.delay(60)

WebUI.verifyElementText(findTestObject('Equ_OR/span_EQU REPORTING'), 'REPORTING')

WebUI.click(findTestObject('Equ_OR/button_VEHICLES PRODUCTION'))

WebUI.delay(30)

//String filterField = , )



	String abc = WebUI.getText(findTestObject('Equ_OR/Page/span_8282086'))
	
if (abc != 0) {
	
	
	reportTest.sendMail("The value is not 0.")
	
	}

still it is showing " reportTest.sendMail " with underlined in script.

Please suggest me to change anything in class or test case script.

Thank you in advance!!!

Add import launch.reportTest to your test case.

By the way, according to conventions every Java class should start with upper case.