Easy way to fail a test

Hi,

Wondering if there are any keywords or easy ways to cause a test to fail if a condition is not met.

For example if you have a condition just like

if(condition){
FAIL TEST
}

For reference, I am currently using something like the following:

if (page1Value != page2Value) {
TestResult = ‘PASS’
}
else {
TestResult = ‘FAIL’
}

WebUI.verifyEqual(TestResult, ‘PASS’)

When I was hoping to use something like:

if (page1Value = page2Value) {
FAIL TEST
}

You can throw an Exception that will cause the failure, like:

or you can simply put in a false statement with a failure handling parameter, like:

WebUI.verifyTextPresent("Test Fail!", false, FailureHandling.STOP_ON_FAILURE)

The first one is probably better but the second one is simple.

1 Like

And a comparison uses double equal signs, like:

if (page1Value == page2Value) {
1 Like

A test case:

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

KeywordUtil.markFailedAndStop("The end of the world")

The output:

2022-10-20 21:20:49.261 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/FailDemo2 FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: The end of the world
	at com.kms.katalon.core.util.KeywordUtil.markFailedAndStop(KeywordUtil.java:29)
	at com.kms.katalon.core.util.KeywordUtil$markFailedAndStop.call(Unknown Source)
	at FailDemo2.run(FailDemo2:22)
	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:445)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:436)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:415)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:407)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:284)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1666268444371.run(TempTestCase1666268444371.groovy:25)

Yeah, realised that after typing :smiley: