Error Handling using If else

Test case should pass even the condition is in the if statement is true and test cases should fail if the condition is in the else statement is true, but in both the cases test cases are going green

if(Deletedmsg.contains(“is deleted successfully”)) {
System.out.println(“DataSet is deleted and testcase is sucessful”+Deletedmsg);
}

else {
	FailureHandling.STOP_ON_FAILURE
	System.out.println("DataSet is not deleted and testcase is fail"+Deletedmsg);
}
WebUI.click(findTestObject('Object Repository/Historical/deleted_Sucessful_popup_Ok'))

The above phrase will not cause your test case to fail if you have it by itself. The STOP_ON_FAILURE is just a defined reference within the “FailureHandling” routine. By itself, the STOP_ON_FAILURE does not do anything. As an example, try this:

import com.kms.katalon.core.model.FailureHandling as FailureHandling

println("The value is " + FailureHandling.STOP_ON_FAILURE)

In order for the defined reference to be effective, it has to be within a parameter list of a Katalon Studio statement. When the phrase is within a parameter list, then the “FailureHandling” method is called. It’s within the “FailureHandling” method that the appropriate action is done. So, if you want your test case to fail, then perhaps you can try:

else {
    println("DataSet is not deleted and testcase is fail ${Deletedmsg}");
	WebUI.verifyTextPresent("Martin is King!", false, FailureHandling.STOP_ON_FAILURE)
}
WebUI.click(findTestObject('Object Repository/Historical/deleted_Sucessful_popup_Ok'))
2 Likes

KeywordUtil would be useful for you.

https://api-docs.katalon.com/com/kms/katalon/core/util/KeywordUtil.html

Try copy&paste the following and run it.

import com.kms.katalon.core.util.KeywordUtil

// String Deletedmsg = '''is deleted succesfully'''
String Deletedmsg = '''is inserted accidentaly'''

if (Deletedmsg.contains("is deleted successfully")) {
    System.out.println("DataSet is deleted and testcase is sucessful: " + Deletedmsg);
}
else { // See https://api-docs.katalon.com/com/kms/katalon/core/util/KeywordUtil.html
    String msg = "DataSet is not deleted and testcase failed: " + Deletedmsg
    KeywordUtil.markFailedAndStop(msg)
    //KeywordUtil.markFailed(msg)
    //KeywordUtil.markWarning(msg)
    //KeywordUtil.logInfo(msg)
}
System.out.println("FIN")

Please try other methods of KeywordUtil and see how they work.

1 Like

Thank you for your support

Thank you for your great support @kazurayam and @grylion54. If this work for you, please mark one of the comment as a possible solution for your problem @ShivaRaju. Thank you all for all the contributions to Katalon Community.