waitForElementPresent with STOP_ON_FAILURE


Hi, I have a problem by using waitForElementPresent function with STOP_ON_FAILURE.
As per attached screen, the test does not stop and continues. Only fails to a subsequent verification. Does it happen to you too? I’m using version 10.1.1

Thank you

1 Like

Yes.

waitForElementPresent keyword silently ignores the 3rd argument “FailureHandling”.
In other word, waitForElementPresent never fails. It never throws a StepFaildException to stop the test case.

Why waitForElementPresent accepts the 3rd argument, which is ignored anyway ? — Don’t ask me. Ask Katalon.

if you want STOP_ON_FAILURE to be respected, you should use verifyElementPresent instead.


This is an old issue:

You should read the source of the keyword implementations.

waitForElementPresent

    @CompileStatic
    public boolean waitForElementPresent(TestObject to, int timeOut, FailureHandling flowControl)
    throws StepFailedException {
        return WebUIKeywordMain.runKeyword({
            boolean isSwitchIntoFrame = false
            try {
                WebUiCommonHelper.checkTestObjectParameter(to)
                isSwitchIntoFrame = WebUiCommonHelper.switchToParentFrame(to, timeOut)
                WebElement foundElement = null
                try {
                    foundElement = WebUIAbstractKeyword.findWebElement(to, timeOut)
                    if (foundElement != null) {
                        logger.logPassed(MessageFormat.format(StringConstants.KW_LOG_PASSED_OBJ_X_IS_PRESENT, to.getObjectId()))
                    }
                    return true
                } catch (WebElementNotFoundException e) {
                    logger.logWarning(MessageFormat.format(StringConstants.KW_MSG_OBJ_IS_NOT_PRESENT_AFTER_X_SEC, [to.getObjectId(), timeOut] as Object[]), null, e)
                    return false
                }
            } finally {
                if (isSwitchIntoFrame) {
                    WebUiCommonHelper.switchToDefaultContent()
                }
            }
        }, flowControl, RunConfiguration.getTakeScreenshotOption(), (to != null) ? MessageFormat.format(StringConstants.KW_MSG_CANNOT_WAIT_OBJ_X_TO_BE_PRESENT, to.getObjectId())
        : StringConstants.KW_MSG_CANNOT_WAIT_FOR_OBJ_TO_BE_PRESENT)
    }

verifyElementPresent

    @CompileStatic
    public boolean verifyElementPresent(TestObject to, int timeOut, FailureHandling flowControl) throws StepFailedException {
        return WebUIKeywordMain.runKeyword({
            boolean isSwitchIntoFrame = false
            try {
                WebUiCommonHelper.checkTestObjectParameter(to)
                isSwitchIntoFrame = WebUiCommonHelper.switchToParentFrame(to, timeOut)
                WebElement foundElement = null
                foundElement = WebUIAbstractKeyword.findWebElement(to, timeOut)
                if (foundElement != null) {
                    logger.logPassed(MessageFormat.format(StringConstants.KW_LOG_PASSED_OBJ_X_IS_PRESENT, to.getObjectId()))
                }
                return true
            } catch (WebElementNotFoundException ex) {
                WebUIKeywordMain.stepFailed(ExceptionsUtil.getMessageForThrowable(ex), flowControl, null, true)
            } finally {
                if (isSwitchIntoFrame) {
                    WebUiCommonHelper.switchToDefaultContent()
                }
            }
            return false
        }, flowControl, RunConfiguration.getTakeScreenshotOption(), (to != null) ? MessageFormat.format(StringConstants.KW_MSG_CANNOT_VERIFY_OBJ_X_IS_PRESENT, to.getObjectId())
        : StringConstants.KW_MSG_CANNOT_VERIFY_OBJ_IS_PRESENT)
    }

PLS compare these 2 codes. You will find the concrete difference.

Actually, the difference is only 1 line, that causes the different behavior of 2 keywords on failure.

I think the use of the WaitForElementPresent keyword is not the right option here., If the code implemented is spected to fail when the element you are waiting for is not present in the screen, then I recommend you to use VerifyElementPresent with STOP_ON_FAILURE handling.

WaitForElementPresent is an strategy to avoid static waits on the code and make your test smart agains loading content in the page.