How can I create a correct decision statement in katalon?

I need to create a decision statement like this:

  • insert username
  • insert password
  • click login
  • if user is already connected click Yes to login and disconnect the other session (in this case: check if the message is present and click Yes)
  • else: if the message is not present, the login is ok
if (WebUI.verifyElementPresent(findTestObject('button_yes'), 20)) {

    WebUI.click(findTestObject('button_yes'))

} else (WebUI.verifyElementNotPresent(findTestObject('button_yes'), 20)) {

    WebUI.findWebElement(findTestObject('bar_s'))
}

What I get is:

  • if the message is present, everything works
  • if the message is absent, the if check fails and the test stops there, it doesn’t get to the else

The error says: =============== ROOT CAUSE ====================
Caused by: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/vbds/vbds_yes’ located by ‘//div[contains(text(),‘Yes’)]’ not found

but the problem is that when that object is not present, it should execute the else, instead it does not, it stops at the if and fails the test

1 Like
// insert username

// insert password

// click login

boolean b = WebUI.waitForElementVisible(findTestObject(....), 20)  // test whether the user is already connected

if (b) {
    // the user is already connected
    WebUI.click(/* Yes */)
    ... do something
} else {
    // the user is NOT connected yet
    ... do something else
}

The WebUI.verify* keyword will terminate your test (as default) by throwing a StepFailedException when the condition is not met. Now you do not want to stop the test, but want to continue processing. Then you should refrain from the verify* keywords.

You should rather use WebUI.wait* keyword which will NEVER throw any Exception, will wait for the condition to be true, and will eventually return a boolean value after the timeout expires. With wait* keyword, you will have a chance to refer to the returned value on which your “if” statement can make a decision.

2 Likes

You can use below code
if (WebUI.verifyElementPresent(findTestObject(‘button_yes’), 5, FailureHandling.OPTIONAL)) {

WebUI.click(findTestObject('button_yes'), FailureHandling.OPTIONAL)

}

thank you very much

Although @kazurayam method of doing “decision” statements is probably the way you will start setting your decisions up, another method does exist for you to keep in mind. It is called tertiary and it looks like:

"if we have 5 or more tasks, use the fifth, otherwise use the first"
int myRow = (tasks.size() >= 5) ? 5 : 1;

workItem = makeTestObject("//table/tbody/tr[${myRow}]/td[8]")