Else statement is not running

Hi, all. The following is my code:

if (WebUI.verifyAlertPresent(3)) {

WebUI.acceptAlert()

} else if (WebUI.verifyElementVisible(findTestObject(‘Log out button/img(1)’))) {

WebUI.click(findTestObject('Log out button/img(1)'))

}

WebUI.closeBrowser()

This is data driven testing for login.
If successfully login, no error pop up and click log out button.
If not successful, error pop up will appear and dismiss it.

The following is the error log.

Start action : Statement - If (com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyAlertPresent(3))

09-20-2018 10:31:02 AM - [INFO] - Checking timeout

09-20-2018 10:31:06 AM - [FAILED] - No alert found after 3 second(s).

09-20-2018 10:31:06 AM - [FAILED] - Unable to verify alert present (Root cause: No alert found after 3 second(s).)

09-20-2018 10:31:06 AM - [END] - End action : Statement - If (com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyAlertPresent(3))

09-20-2018 10:31:06 AM - [FAILED] - Test Cases/Login FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Unable to verify alert present (Root cause: No alert found after 3 second(s).)

The test case: success login, no pop up, deemed as failed, and else statement is not run.

Is there something wrong with my code? Thanks in advance.

If you want to continue ‘else’ statement, then you need to add Failure Handling (Continue on Failure or Optional) parameter to ‘if’ statement, e.g:

if (WebUI.verifyAlertPresent(3, FailureHandling.OPTIONAL)) {

WebUI.acceptAlert()

} else if (WebUI.verifyElementVisible(findTestObject(‘Log out button/img(1)’))) {

WebUI.click(findTestObject('Log out button/img(1)'))

}

See more details about Failure Handling here

2 Likes

OMG! Thanks a lot. You saved me. Appreciate it. <3

Thank you. This worked for me. (Y)
Please add: FailureHandling.OPTIONAL
Correct syntax, e.g:

if (WebUI.verifyAlertPresent(3, FailureHandling.OPTIONAL)) {

WebUI.acceptAlert()

} else if (WebUI.verifyElementVisible(findTestObject(‘Log out button/img(1)’))) {

WebUI.click(findTestObject('Log out button/img(1)'))

}