Unable to stop script when WebUI.waitForElementVisible fails

Hi,
I’ve got the following code:

WebUI.waitForElementVisible(findTestObject(‘Page_Surveillance 01/Check user’), 10,FailureHandling.STOP_ON_FAILURE)

Which fails with a warning in the console log:

[INFO] - Finding web element with id: ‘Object Repository/Page_Surveillance 01/Check user’ located by ‘By.xpath: id(“component-1028”)/div[@class=“map-name”][count(. | //[(text() = ‘Surveillance 01’ or . = ‘Surveillance 01’)]) = count(//[(text() = ‘Surveillance 01’ or . = ‘Surveillance 01’)])]’ in ‘10’ second(s)

04-30-2018 10:50:45 AM - [WARNING] - Web element with id: ‘Object Repository/Page_Surveillance 01/Check user’ located by ‘By.xpath: id(“component-1028”)/div[@class=“map-name”][count(. | //[(text() = ‘Surveillance 01’ or . = ‘Surveillance 01’)]) = count(//[(text() = ‘Surveillance 01’ or . = ‘Surveillance 01’)])]’ not found

04-30-2018 10:50:45 AM - [END] - End action : waitForElementVisible

04-30-2018 10:50:45 AM - [START] - Start action : Statement -

The problem is that the script continues to run, and I would like it to terminate.

What am I doing wrong?

Have also set the project settings to :
DefaultFailure handling for Test scripts: STOP_ON_FAILURE

Thanks!

OK, so I found the following article describing how to end the script with an assert:

This doesn’t quite do it for me as I need to handle the failure in a catch statement, and post some data to a webservice if anything fails.

Any idea if it’s possible to catch these types of warnings?

Thanks

Thomas,

waitForElementVisible() returns boolean value.

https://docs.katalon.com/display/KD/[WebUI]+Wait+For+Element+Visible

Groovy’s assert raises AssetionError when given with false argument.
http://groovy-almanac.org/catch-assertion-exceptions-in-groovy/

So how about this?

try {
    assert WebUI.waitForElementVisible(findTestObject('Page_Surveillance 01/Check user'), 
        10,FailureHandling.CONTINUE_ON_FAILURE)
} catch (AssertionError e) {
    println e.getMessage()
    // post some data to your webservice, etc}
1 Like

Works like a charm.

Thank you!

waitForElementVisbile() returns boolean value.
you can use below keyword and modify the same according to your need…
try{
boolean flag = WebUI.waitForElementVisible(findTestObject(to), maxTimeout, FailureHandling.STOP_ON_FAILURE)
if (flag == false) {
throw new Exception(“Element is not visible on page”)
}}catch(Exception ex){
KeywordUtil.markFailed("Elemenet is not visible on the page and exception occured due to :: "+ex.getMessage())
}

I do a similar thing for checking return values when using waitForElementVisible(), which is really useful as waitForElementVisible() dosnt appear to actually throw a StepFailedException (not sure why this is!).

waitForElementVisible() returns boolean value (True or False)

https://docs.katalon.com/display/KD/[WebUI]+Wait+For+Element+Visible

Instead use verify element with failure handling. i.e.Continue on Failure.OR Stop on Failure

WebUI.verifyElementVisible(findTestObject(‘To’))

WebUI.verifyElementPresent(findTestObject(‘TO’), 20)

This is an older topic but still relevant. I was struggling w the same issue and found that if you use ‘FailureHandling.CONTINUE_ON_FAILURE’ then Katalon will stick to the implicit wait defined in Settings. Or put another way, it ignores the integer value you’re trying explicitly declare for this step.

If you write it this way, your Test Script will throw an exception much quicker (or slower if that’s your use case).

try {
    assert WebUI.waitForElementVisible(findTestObject('Page_Surveillance 01/Check user'), 
        5,FailureHandling.STOP_ON_FAILURE)
} catch (AssertionError e) {
    println e.getMessage()
    // post some data to your webservice, etc}

I have a doubt about this.

I made a test case

import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser("http://demoaut.katalon.com/")

WebUI.waitForElementVisible(makeTO("//a[text()='Make Appointment']"), 5)   // (1)

TestObject disappointment = makeTO("//a[text()='Disappointment']")

WebUI.waitForElementVisible(disappointment, 5, FailureHandling.OPTIONAL)   // (2)

WebUI.waitForElementVisible(disappointment, 5, FailureHandling.CONTINUE_ON_FAILURE)  // (3)

WebUI.waitForElementVisible(disappointment, 5, FailureHandling.STOP_ON_FAILURE)  // (4)

WebUI.closeBrowser()

TestObject makeTO(String xpath) {
	TestObject to = new TestObject(xpath)
	to.addProperty("xpath", ConditionType.EQUALS, xpath)
	return to
}

The code calls WebUI.waitForElementVisible() 3 times with 3 FailureHandling options — OPTIONAL, CONTINUE_ON_FAILURE, STOP_ON_FAILURE. Here I specified a Test Object that points to non-existing web element, therefore all 3 times of Keyword invocation would fail. The question we ask is if the timeout value “5” is respected or not. I have the project’s default timeout for wait to be 30 seconds.

When I ran it, I saw the following output in the Console

2022-04-30 08:55:07.281 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-04-30 08:55:07.286 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/waitForElementVisible_CONTINUE
2022-04-30 08:55:08.261 DEBUG testcase.waitForElementVisible_CONTINUE  - 1: openBrowser("http://demoaut.katalon.com/")
...
2022-04-30 08:55:15.819 DEBUG testcase.waitForElementVisible_CONTINUE  - 2: waitForElementVisible(makeTO("//a[text()='Make Appointment']"), 5)
2022-04-30 08:55:15.823 DEBUG testcase.waitForElementVisible_CONTINUE  - 1: to = new com.kms.katalon.core.testobject.TestObject(xpath)
2022-04-30 08:55:15.831 DEBUG testcase.waitForElementVisible_CONTINUE  - 2: to.addProperty("xpath", EQUALS, xpath)
2022-04-30 08:55:15.849 DEBUG testcase.waitForElementVisible_CONTINUE  - 3: return to
2022-04-30 08:55:16.405 DEBUG testcase.waitForElementVisible_CONTINUE  - 3: disappointment = makeTO("//a[text()='Disappointment']")
2022-04-30 08:55:16.407 DEBUG testcase.waitForElementVisible_CONTINUE  - 1: to = new com.kms.katalon.core.testobject.TestObject(xpath)
2022-04-30 08:55:16.409 DEBUG testcase.waitForElementVisible_CONTINUE  - 2: to.addProperty("xpath", EQUALS, xpath)
2022-04-30 08:55:16.412 DEBUG testcase.waitForElementVisible_CONTINUE  - 3: return to
2022-04-30 08:55:16.415 DEBUG testcase.waitForElementVisible_CONTINUE  - 4: waitForElementVisible(disappointment, 5, OPTIONAL)
2022-04-30 08:55:22.484 INFO  c.k.k.c.webui.common.WebUiCommonHelper   - Unable to find the element located by 'By.xpath: //a[text()='Disappointment']'. Please recheck the objects properties to make sure the desired element is located. 
2022-04-30 08:55:22.521 WARN  k.k.c.w.k.b.WaitForElementVisibleKeyword - Web element with id: '//a[text()='Disappointment']' located by '//a[text()='Disappointment']' not found
2022-04-30 08:55:22.525 DEBUG testcase.waitForElementVisible_CONTINUE  - 5: waitForElementVisible(disappointment, 5, CONTINUE_ON_FAILURE)
2022-04-30 08:55:28.682 INFO  c.k.k.c.webui.common.WebUiCommonHelper   - Unable to find the element located by 'By.xpath: //a[text()='Disappointment']'. Please recheck the objects properties to make sure the desired element is located. 
2022-04-30 08:55:28.685 WARN  k.k.c.w.k.b.WaitForElementVisibleKeyword - Web element with id: '//a[text()='Disappointment']' located by '//a[text()='Disappointment']' not found
2022-04-30 08:55:28.691 DEBUG testcase.waitForElementVisible_CONTINUE  - 6: waitForElementVisible(disappointment, 5, STOP_ON_FAILURE)
2022-04-30 08:55:34.804 INFO  c.k.k.c.webui.common.WebUiCommonHelper   - Unable to find the element located by 'By.xpath: //a[text()='Disappointment']'. Please recheck the objects properties to make sure the desired element is located. 
2022-04-30 08:55:34.806 WARN  k.k.c.w.k.b.WaitForElementVisibleKeyword - Web element with id: '//a[text()='Disappointment']' located by '//a[text()='Disappointment']' not found
2022-04-30 08:55:34.808 DEBUG testcase.waitForElementVisible_CONTINUE  - 7: closeBrowser()
2022-04-30 08:55:34.961 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/waitForElementVisible_CONTINUE

I think that the specified timeout 5 was respected in all cases.

We can read the source of WebUI.waitForKeywordVisible keyword at

I think that the WebUI.waitForElementVisible keyword treats 3 options of FlowControl (OPTIONAL, CONTINUE_ON_FAILURE, STOP_ON_FAILURE) in a similar manner. Your observation does not explain the processing flow observable in the source code very well.