IF ELSE statements in Katalon

Hi All,
I find it weird when Katalon execute IF ELSE statements
I have simple IF statement, if the panel element is not present click an element to make that panel element present, if it is already present do not click
if(WebUI.verifyElementNotPresent(findTestObject(‘Page_SurveyQuestions/span_SurveyPanel’),30)){
WebUI.doubleClick(findTestObject(‘Page_SurveyQuestions/span_Screening’))
}
If the element is not Present it works perfectly, but if it does present, I just wanna it goto next statement without any action, but it fails with below exceptions:

Unable to verify object ‘Object Repository/Page_SurveyQuestions/span_SurveyPanel’ is not present
Web element with id: ‘Object Repository/Page_SurveyQuestions/span_SurveyPanel’ located by ‘By.xpath: //mat-expansion-panel[contains(@class,‘survey-panel’)]’ is present after ‘30’ second(s)

1 Like

Katalon executes if statements like any other program would. Its most likely down to the way you have written it. Please share your whole if statement including the else so i can take a look. It should look something like this. Although reading the error message tells me that you might not have an else part which is what will be causing your problem

if(WebUI.verifyElementNotPresent(findTestObject(‘Page_SurveyQuestions/span_SurveyPanel’),30))
{
WebUI.doubleClick(findTestObject(‘Page_SurveyQuestions/span_Screening’))
}
Else
{
//do what ever you want it to do if its present.
}

@ hpulsford](http://forum.katalon.com/u/hpulsford)
Thanks for your response
I didn’t know ELSE is mandatory for IF statement, if condition is true executes otherwise if would move to next statement is what is my understanding
Anyways I did add ELSE per your suggestion I get the same error still.
if(WebUI.verifyElementNotPresent(findTestObject(‘Page_SurveyQuestions/span_SurveyPanel’),30)){
WebUI.doubleClick(findTestObject(‘Page_SurveyQuestions/span_Screening’))
CustomKeywords.‘custom.affectlabHandlers.selectTestPanel’(‘Add Screening Questions’)
}else{
CustomKeywords.‘custom.affectlabHandlers.selectTestPanel’(‘Add Screening Questions’)
}

It is failing at verify statement, verify doesn’t return FALSE if the element is present.

Hi Anjanaiv,

I don’t think your if else statement is the one to blame here.

As Harry said:

katalon team will not change any logic here.

Based on my understanding, there’s a difference between:

verifyElementPresent
And
verifyElementVisible

..... or any methods related to those two, such as:
verifyElementNotPresent
verifyElementNotVisible
etc. . . . .

That’s why those two methods are created.

Someone correct me if I’m wrong, this is based on my experience using these two ----
VerifyElementPresent deals with the source (HTML) means it maybe present in the in the source but not visible on the website while VerifyElementVisible deals with the UI itself.

And, there’s nothing wrong about katalon if else statement. Possibly your condition is the one to look out for.

My suggestion is try to observe and use “Visible”. Maybe that will do the trick.

Hope that helps. . . :slight_smile:

Thanks for suggestion again
Yes I also think VerifyElementPresent is the culprit
Both of these function should return true or false based on the situation, but I get exceptions, as it is not assert functions why is exception thrown & I have to add unnecessary try catch to handle that.
VerifyElementVisible is also not solving the purpose
verifyElementNotPresent when Element is not present it should return true & when it present it should return false, instead of false it throws exception

The Issue here is that VerifyElementNotPresent is there to verify that an element is not present… and if it’s present it’s an exception and will stop the program, because that’sn what you want to test with it.
You just need to add following statement to it:

WebUI.verifyElementNotPresent(findTestObject(‘Page_SurveyQuestions/span_SurveyPanel’),30, FailureHandling.CONTINUE_ON_FAILURE)
OR
WebUI.verifyElementNotPresent(findTestObject(‘Page_SurveyQuestions/span_SurveyPanel’),30, FailureHandling.OPTIONAL)

Continue just continues and gives no error message and Optional will also continue, but will mark this step with a warning.
If you don’t add anything to it, STOP_ON_FAILURE is the standard selection.

Here a link for you for further informations :slight_smile:

Hope that helps.

thank you for this topic and this has been specifically helpful for me today… both of you, author anjanaiv.26 and solver, Dominik_Kreft