Looking for a keyword behaviour - KeywordUtil.markPassedAndStop

I have a business scenario to be tested, where if a condition is met, then mark the testcase as passed and termiate the test case execution and continue with other test case execution in Testuite.

String portMenu = "miami"

println('Execute Step 1')
println('Execute Step 2')

if(portMenu =="miami") {
	
	println ("Exit the TC and mark the TC as passed.")
	KeywordUtil.markPassedAndStop(" Test Success")
}

println('Execute Step 3')
println('Execute Step 4')
println('Execute Step 5')

I am looking for a keyword behaviour KeywordUtil.markPassedandStop(“”) which doesn’t exist. Is there a workaround to acheive it. [Want to handle in if block, without using the else block]

You can mimic this:

1 Like
String portMenu = "miami"

println('Execute Step 1')
println('Execute Step 2')

if(portMenu != "miami") {
    println('Execute Step 3')
    println('Execute Step 4')
    println('Execute Step 5')
}

Does this work for you?

Thanks Russ, In my case it wouldn’t fit.

Below is my case.

@kazurayam could you please tell, how to achieve it.

What do you want to do?

You haven’t described your problem at all.

Please, don’t let others guess what you want.

markpassed and stop the execution.

Have you tried writing any code?

Please show what you have done.

I used the return keyword as per below thread.

Stopping test case if it’s successful - Product Forums / Katalon Studio - Katalon Community

import com.kms.katalon.core.exception.StepErrorException;
import com.kms.katalon.core.exception.StepFailedException;
import com.kms.katalon.core.logging.ErrorCollector;
import com.kms.katalon.core.logging.KeywordLogger;

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




public static void markPassedandStop(String message) {
	
	final KeywordLogger logger = KeywordLogger.getInstance(KeywordUtil.class);
	logger.logPassed(message);
	ErrorCollector.getCollector().setKeywordPassed(true);
	return
}
String portMenu = "miami"
boolean doStuff = true

println('Execute Step 1')
println('Execute Step 2')

if(portMenu =="miami") {
	doStuff = false
	println ("Exit the TC and mark the TC as passed.")
}

if(doStuff) {
    println('Execute Step 3')
    println('Execute Step 4')
    println('Execute Step 5')
}

You could also try closures:

doStuffBlock = {
    println('Execute Step 3')
    println('Execute Step 4')
    println('Execute Step 5')
}

if(portMenu != "miami") doStuffBlock()

If Groovy can handle the logic inline, you don’t need the if {}:

doStuff && doStuffBlock()
2 Likes

I like @Russ_Thomas’ approach (avoid “GOTO”). His approach helps making my code structured better.

I do not like the approach that uses the markPassedAndStop keyword. It is a sort of “GOTO” statement. It tends to make my code unstructured. It will make my code like a “Spaghetti”.

In Java/Groovy programming, you would/should never use “GOTO”. It is not necessary. No reason to introduce new “GOTO” named markPassedAndStop.

Yes, of course.
Let me try:

String portMenu = "miami"
doStuffBlock = {
	println('Execute Step 3')
	println('Execute Step 4')
	println('Execute Step 5')
}

boolean doStuff = true

println('Execute Step 1')
println('Execute Step 2')

doStuff && doStuffBlock()

This test case example gave me:

2022-11-19 07:48:41.256 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-11-19 07:48:41.260 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/RussApproach3
Execute Step 1
Execute Step 2
Execute Step 3
Execute Step 4
Execute Step 5
2022-11-19 07:48:41.880 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/RussApproach3

I tuned the test case a bit (true → false):

String portMenu = "miami"
doStuffBlock = {
	println('Execute Step 3')
	println('Execute Step 4')
	println('Execute Step 5')
}

boolean doStuff = false

println('Execute Step 1')
println('Execute Step 2')

doStuff && doStuffBlock()

this game me:

2022-11-19 07:50:52.624 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-11-19 07:50:52.627 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/RussApproach3
Execute Step 1
Execute Step 2
2022-11-19 07:50:53.188 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/RussApproach3

This is clean, isn’t it?

It is far easier to read the code and understand what it will do, than the approach using “GOTO” keyword

1 Like

Very, as was my intention in suggesting it.

That’s fair. However, you shouldn’t necessarily file all such mechanisms in the same box as GoTo.

For example, there are good use cases for using throw to achieve “interrupts” in high-level languages that are not available via other means.

1 Like

Just for fun:

String mychoice = "miami"
//String mychoice = "vegas"

do1stBlock = {
	String expected = "miami"
	println('Execute Step 1')
	println('Execute Step 2')
	return (mychoice == expected)
}

do2ndBlock = {
	println('Execute Step 3')
	println('Execute Step 4')
	println('Execute Step 5')
}

do1stBlock() && do2ndBlock()

This produced:

2022-11-21 10:00:20.265 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/RussApproach4
Execute Step 1
Execute Step 2
Execute Step 3
Execute Step 4
Execute Step 5
2022-11-21 10:00:21.107 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/RussApproach4

I changed it to:

//String mychoice = "miami"
String mychoice = "vegas"
...

Then it produced:

2022-11-21 10:02:14.693 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/RussApproach4
Execute Step 1
Execute Step 2
2022-11-21 10:02:15.344 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/RussApproach4

do1stBlock() && do2ndBlock() makes a chain of closure calls. The chain is conditional. The following closure (do2ndBlock) is executed only when the preceding closure (do1stBlock) returned true.

We can extend the conditional chain of closures as long as we like:

do1stBlock() && do2ndBlock() && do3rdBlock() && do4thBlock() && do5thBlock() ...

This style will motivate you to structure your code as a composition of code blocks, not as a single sequence of a lot of statements with GOTO occasionally inserted.

Each component clousre can stop the chain by returning false. This behavior would be similar to markPassedAndStop keyword.

This might be a fly-weight alternative to Test Suite with capability to stop conditionally.

Of course, this style is advanced one; it is only possible using the “Script mode” of Test Case editor. You can not use this style in the “Manual mode” at all. — I don’t mind this, as I never use the Manual mode.