Not able to catch exception from builtin keywords

Scenario:
I want to create my own keyword, which can deal with few unexpected situations.
Example: Click an element, if other element would receive the click, wait a second and try again.
WebUI.click() throws StepFailedException.

POC:

public class WebUIOverride {
	public static void click(TestObject to) {
		try {
			WebUI.click(to)
		} catch (StepFailedException ex) {
			KeywordUtil.logInfo("StepFailedException caught.")
			if(ex.getMessage().contains("Other element would receive the click")) {
				KeywordUtil.markWarning("Other element would receive the click. Trying again after 1 second...")
				WebUI.delay(1)
				WebUI.click(to)
			} else {
				KeywordUtil.markFailed("Exception caught: " + ex)
			}
		}
	}
}

Problem:
Even if the StepFailedException occurs, it is not caught at custom keyword level. First error seems to be caught in this class:

ERROR c.k.k.core.keyword.internal.KeywordMain ❌ Unable to click on object 'Object Repository/xxx' (Root cause: org.openqa.selenium.WebDriverException: unknown error: Element \<img xxxyyy> is not clickable at point (2519, 136). Other element would receive the click:...

Later, StepFailedException appears, but it is never caught.

ERROR c.k.katalon.core.main.TestCaseExecutor ❌ WebUIOverride.click(findTestObject("xxx")) FAILED. Reason: com.kms.katalon.core.exception.StepFailedException: Unable to click on object 'Object Repository/xxx' (Root cause: the same as above)

Question:
How to handle such a situation? I tried to catch even Throwable class, but with no success.

1 Like

WebUI.click(TestObject, FailureHandling) has internal logic to handle exception. It handles the exception according to the 2nd argument: FailureHandling. So the caller script would never catch the StepFailedException.

You can read the source of WebUI.click() at https://github.com/katalon-studio/katalon-studio-testing-framework/blob/master/Include/scripts/groovy/com/kms/katalon/core/webui/keyword/builtin/ClickKeyword.groovy
You will find that WebUI.click() uses org.openqa.selenium.WebElement#click(). You can mimic it. For example,

import org.openqa.selenium.WebElement
...
class ... {
    public static void click(TestObject to) {
        try {
            WebElement webElement = WebUIAbstractKeyword.findWebElement(to)
            webElement.click()
       } catch (Exceptio ex) {
           ...
       }
}

I know, I am working on a workaround using pure WebDriver’s methods,
but I am wondering why there’s “…throws StepFailedException” statement in method’s documentation.
I wouldn’t mention it at all to prevent confusion.

1 Like

@Marek_Melocik mmhm … let me guess …
The StepFailedException is thrown by the keyword?
And you are trying to catch it in a custom keyword …

If you really need to catch that specific error type, you have to do it in the test-case.
Otherwise follow the @kazurayam workaround or you may have to use meta-programming to override the method

LE: i cannot find any API docs for com.kms.katalon.core.exception so, the above is just a blind guess …

Well, that was my initial approach - WebUI.click() should throw StepFailedException.

20190121-124801

But as @kazurayam said, it is consumed somewhere else.
Actually, using click() on WebElement solved the problem. Exception can be caught and handled as I need.

1 Like

This is where I suppose it is consumed, from the log you provided.
Looks like indeed it is raised using the Keyword annotation, but not catch-able in that scope …
Interresting

LLE: the only place I found reference to this exception is in the BuiltInKeywords class, which is just using it and throwing to the above scope …

https://api-docs.katalon.com/studio/v4.6.0.2/api/com/kms/katalon/core/keyword/BuiltinKeywords.html