If statement ignores else

Hello guys, i wrote some code and when i’m trying to make an if-else statement it fails. Here is my code:

if (WebUI.verifyElementPresent(findTestObject(‘Object Repository/if-else/Page_Operator/div_Operator already has activ’), 4) == true) {
WebUI.click(findTestObject(‘if-else/Page_Operator/button_Clear’))
} else {
WebUI.click(findTestObject(‘vbm.vtb.ge/Page_Operator/click_phone’))
}

I’ve tried too many things but nothing works. It works when it goes in if statement, but if IF statement is false the test stops. I’ve also tried FailureHandling.OPTIONAL, with that it works but is there any way to make it without failurehandling ?

No, there isn’t.

Basically, verifyElementPresent returns either true or exception (WebElementNotFoundException IIRC). That’s why else statement is not reachable if you don’t use FailureHandling.OPTIONAL. => This option supress the exception and throws only a warning.

You can try to use WebUI.waitForElementPresent(…) method, which returns boolean value and exception shouldn’t be thrown in case of failure.

2 Likes

Well, Thank you, now with waitForElementPresent I don’t need to change failureHandling, thanks for helping me.