Verify Element Present giving warning even if the element is not present

Hello Team,

I have below piece of code which uses Verify Element Present to see if the element is present then do this else do this

if (WebUI.verifyElementPresent(findTestObject('3_Page_PDP/Price_Before_Sale'), 0, FailureHandling.OPTIONAL)) 

{
    pdp_product_price_1 = WebUI.getText(findTestObject('3_Page_PDP/Price_After_Sale'), FailureHandling.STOP_ON_FAILURE).replace(
        '$', '')

     pdp_product_price = new BigDecimal(pdp_product_price_1)

    WebUI.println('The Price of Product on PDP page is : ' + pdp_product_price)
} 

else 

{
    pdp_product_price_1 = WebUI.getText(findTestObject('3_Page_PDP/PDP_Product_Price_No_Sale')).replace('$', '')

    pdp_product_price = new BigDecimal(pdp_product_price_1)

    WebUI.println('The Price of Product on PDP page is : ' + pdp_product_price)
}

But whenever the page doesn’t have Price_Before_Sale element the code is still trying to find the element for about 30 seconds and then give warning about that.

You invoke this:

This 1 line does try to find the element for about 30 seconds and then give warning about that. After emitting warning message, WebUI.verifyElementPresent() returns boolean value.

1 Like

Are you questioning about timeout = 0 you specified, but in fact it waits for 30 seconds?

Possibly timeout = 0 is not effective.

But you can try timeout = 1? Should be good enough.

1 Like

The source code of WebUI.verifyElementPresent is avaliable at

Starting from there, you can spot where the timeout value is evaluated. See

Line #729:

            float timeCount = 0;
            long miliseconds = System.currentTimeMillis();
            while (timeCount < timeOut) {
                try {
                    List<WebElement> webElements = null;
                    if (objectInsideShadowDom) {
                        webElements = doFindElementsInsideShadowDom(testObject, timeOut, webDriver, cssLocator,
                                parentObject, shadowRootElement);
                        return webElements;
                    } else {
                        webElements = webDriver.findElements(defaultLocator);
                        if (webElements != null && webElements.size() > 0) {
                            logger.logDebug(MessageFormat.format(
                                    StringConstants.KW_LOG_INFO_FINDING_WEB_ELEMENT_W_ID_SUCCESS, webElements.size(),
                                    testObject.getObjectId(), defaultLocator.toString(), timeOut));
                            return webElements;
                        }
                    }
                } catch (NoSuchElementException e) {
                    // not found element yet, moving on
                }
                timeCount += ((System.currentTimeMillis() - miliseconds) / 1000);
                Thread.sleep(500);
                timeCount += 0.5;
                miliseconds = System.currentTimeMillis();
            }

If you specify timeout = 0, then while (timeCount < timeOut) will immediately quit. Therefore specifying timeout = 0 makes no effect.

1 Like

Hello @kazurayam

I tried your solution and changed the timeout to 1 and it worked. But it still gives the warning that object not found.

Its an if statement and I tested it on a page where the same object was not there. Sorry I am new to this test automation and maybe it is dumb question.

You can just ignore the message.

Alternatively if you don’t want to wait at all, then you should use other keyword. For example

def text = WebUI.getText(findTestObject('3_Page_PDP/Price_Before_Sale'), FailureHandling.OPTIONAL)
if (text != null && text.length > 0) {
    ...
} else {
    ....
}

I would prefer using verifyElementPresent with appropriate secons of timeout while I would ignore the message.

1 Like

Thanks @kazurayam

I will go with your suggestion and use verifyElementPresent

waitForElementPresent might be an alternative.
https://docs.katalon.com/katalon-studio/docs/webui-wait-for-element-present.html

This would not emit any message when the element is found not present.

1 Like