Test Case failure handling/options

Hey folks,
Is there a quick way to write a code that handles test case failure and can take another actionn when a test case fails?

So I know I can use “CONTINUE_ON_FAILURE” on every step… but looking for a Test Case solution.

Something in this method:
If this test case fails
then
{call on test case 2}

or something like
if this testcase fails
then
{delete the data created}

import com.kms.katalon.core.exception.StepFailedException
...
try {
    //Something in this method
} catch (StepFailedException ex) {
    //call on test case 2
} finally {
    //delete the data created
}

Hi,
how would this try-catch block know if the test case failed, not just a test step? let’s say the test failed at any given steps… once the test case fails (at any step), I need the test to do something.
I basically needs to know the result of the test case fail… is there a way to get that?

i was thinking something along this line:
def status = test case?
If status of test case == ‘Fail’
// then do this
else
//do this

does that make sense?

Have you tried what I suggested?

May be not.

If you want to ask further, please try what you were suggested to do. Otherwise I can not continue.

I suppose you don’t understand the basic of Java Programing language: Exception, try { … } catch { … } .

Unless you understand what Exception is, how try {} catch {} works, you would not be able to reach to a solution. You should buy a good Java book, such as Core Java I, and read the first few chapters.

1 Like

No, you can not.

When a Katalon Test Case failed, almost always it will NOT gently return any result. It will just throw an StepFailedException and disappear.

The closest you can get to what you want is to use something like the following…

First, let’s look at how a typical test case is presented:

// Imports

// Step 1

// Step 2
...
// Step n

To gain some control over all steps AND “take another action when a test case fails”, you can give Katalon a single entry point into your own class. Technically, this is a class within a class, an inner class, since your test case itself lives inside a run() method of a class you cannot ordinarily see.

In the following, you can place your test steps inside the constructor for the new inner class:


// Imports

public class Test {

  Test() {

    // Step 1

    // Step 2
    ...
    // Step n
  }
}

try {
  new Test()
  passThisTest()
} catch(Exception e) {
  failThisTest(e.message)
}

You can code passThisTest() and failThisTest() to do anything you want.

This makes no sence.
If this case fails, it fails.

Perhaps you may need to re-phrase?
Like in if this step fails do that, otherwise do something else ?

Sorry for the late response…
So I’ve tried to do something of this nature, I have a test ase that uses Key Stroke, but I’m trying to suport that action for both Windows and Mac.
i.e -

try {

	//	Windows Key Stroke
	WebUI.sendKeys(findTestObject('Page_Notes Page/div_notes_editor'), 
		Keys.chord(Keys.CONTROL, 'a'))
	
} catch (StepFailedException ex) {
	//  Mac Key Stroke
	WebUI.sendKeys(findTestObject('Page_Notes Page/div_notes_editor'),
		Keys.chord(Keys.COMMAND, 'a'))
	
}

The test case passes in Mac… but it’s not really doing the keystroke. Most likely i’m doing it wrong.

can’t I use TearDown() function on specific tests that needs to do something if entire test fails? In theory, will it not call that function in the end of the test run whether it fails or pass?

I have never used TearDown. I know nothing about it.

I’m sorry, but that is a bullshit way to do it. You shouldn’t do try-catch for something like this, StepFailedException could throw for any number of reasons, outside an operating system not having a certain key.

Try this instead:

public final class GeneralWebUIUtils {
	public static Keys GetCommandKey() {
		final String os = System.getProperty("os.name")

		if (os.toUpperCase().contains("WINDOWS"))
			return Keys.CONTROL;

		return Keys.COMMAND;
	}
}

Save that to keyword, and then you can be like:

WebUI.sendKeys(findTestObject('Page_Notes Page/div_notes_editor'),
		Keys.chord("${GeneralWebUIUtils.GetCommandKey()}A"))
1 Like

Brilliant! Thanks a bunch!

2 Likes