Execute if/else when Element exists

I’m trying to test a page that randomly generates one of two objects each time the page is loaded. I’d like to use an if statement to execute a click action depending on which object is found. I tried this:

if (WebUI.verifyElementPresent(findTestObject(‘Objects/object1’)) == true) {
WebUI.click(findTestObject('Objects/object1_action))
} else {
WebUI.click(findTestObject(‘Objects/object2_action’))
}

where Objects/object1 is the id of one of the dynamically generated objects.

The problem with this is that if object1 is not found then the test fails. I need it to continue to the else block.

I’m probably doing something horribly wrong… hoping you can assist.

 

5 Likes

Yay it works!

Thanks :slight_smile:

Thanks for your response. I amended the script as per your suggestion but am getting the following error when executing the test;

Test Cases/Test1 FAILED because (of) groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, com.kms.katalon.core.model.FailureHandling) values: [TestObject - Object Repository/Objects/object1, …]

1 Like

I am also facing the same problem. Same with try catch blocks. The actions which are scripted in try block are not executed again after exception is handled via the catch block and I had to write the scripts for the actions in catch block as well. Hope, we get some suggestions.

1 Like

The same issue. Can anyone tell what to do?

1 Like

Sorry about my mistake. Follow this link, that keyword needs a timeout parameter.

Katalon provides FailureHandling feature as an parameter of every keywords to deal with failure. In your case, the script should be:

if (WebUI.verifyElementPresent(findTestObject('Objects/object1'), FailureHandling.OPTIONAL)) {
WebUI.click(findTestObject('Objects/object1_action))
} else {
WebUI.click(findTestObject('Objects/object2_action'))
}

8 Likes

Thanks “duyluong” for the sample code.

I have used the same structure as given here, but it doesn’t work for me, the execution is not going into the else part of the code. It is frustrating me a little because searching on the web keeps bringing me here. I even tried to add ‘== true’ in the condition, but no luck.

How we can add assert condition in Katalon?

I’m having the same problem as @“Tim van der Weyden”. I tried all of the above solutions, but none worked.
Any progress?

1 Like

Expanding on the answer from duyluong and putting it all together:

// Set how long to wait to interact with/verify an element on the screen.// Setting this to 0 seems to fall back to the project default, which could be 30 sec.// If you don't want to wait that long, set to an integer > 0.int timeout = 1// We can define the test objects first for re-use.// This does not actually try to find the object on the page, but only looks// for the definition of the object in the object repository.TestObject object1 = findTestObject('Objects/object1_action')TestObject object2 = findTestObject('Objects/object2_action')// FailureHandling.STOP_ON_FAILURE = Default behavior. Exits/fails the test when the element is not found// FailureHandling.CONTINUE_ON_FAILURE = reports that an error occurred during the run but doesn't exit; continues running the "else" clause// FailureHandling.OPTIONAL = no error reported; continues running the "else" clause
if (WebUI.verifyElementPresent(object1, timeout, FailureHandling.OPTIONAL)) {
    WebUI.click(object1)
} else {
    WebUI.click(object2)
}
17 Likes

thanks for the answers.

Note however that Katalon Studio freezes up if I use a high TIMEOUT value (300 secs).

I’m following the same line of code, but it won’t let me use Else with my If statement. It gives me the error ‘expecting ‘}’, found ‘else’ @ line 144, column 15.’

But it wants me to put enough brackets there to end my entire block, and then I can’t have my else statement. Any thoughts? I can try to provide some code

Hi Uriel,

A code sample would be helpful to figure out what’s going on.

At its simplest, an if/else block looks like:

if (logic) {
   do the true thing
} else {
    do the false thing
}

It’s possible that if you have complicated “logic” statements, that you forgot to close a “)” at the end of it. Also note the opening "{"at the end of the “if” line.

Some people find if/else statements easier to write out with more line breaks, just to be more clear:

if (logic) 
{
   do the true thing
} 
else 
{
    do the false thing
}
2 Likes

It was as simple as missing a closing parenthesis, Chris. Thank you for pointing that out. I thought I had checked it.

Hi,

I am facing same problem in this scenario. I used above code in my script but not succeed.

In my case; I have to search object 1 in the page and if object 1 is found than click on it and pop-up window will open to do next step. if object 1 is not found and object 2 is available in the page on that time have to click on object 2 and than click on object 3 and than after pop-up window will open to do next step.

Here, What happen in first case;

Object 1 found and click on it and pop-up window open and then script go to search object 2 and it is not available on pop-up window and throw error message.

Second Case:

When object 1 is not found in the page on that time script doesn’t switch to else statement but trying to search object 1 and script doesn’t stop at all.

FYI: Project Settings >> Execution >>Default

Default wait for element timeout (in seconds) = 3
Delay between actions (in secs) = 1
Default page load time out = wait until the page is loaded

For second case, I try with 30 secs time limit for wait for element

Code:

int timeout = 1

TestObject object1 = findTestObject(‘Object Repository/Object 1’)

TestObject object2 = findTestObject(‘Object Repository/Object 2’)

if (WebUI.verifyElementPresent(object1, timeout, FailureHandling.OPTIONAL)){

WebUI.click(object1)

} else {

WebUI.click(object2)

WebUI.click(findTestObject('Object Repository/Object 3'))

}

‘Click on add new user’

WebUI.click(findTestObject(‘Object Repository/Object 4’))

Error Message:

10-10-2018 10:30:26 AM - [FAILED] - Unable to click on object ‘Object Repository/ClientPreManagement-Object 2)’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/ClientPreManagement- Object 2’ located by ‘By.xpath: //*[@id = ‘xpath’]’ not found)

10-10-2018 10:30:26 AM - [FAILED] - Test Cases/Client Pre-Management Module - 400/UAT-LL-470-Add or edit a client Finance Manager/Reject_Sub_ClientFinmgr FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Unable to click on object ‘Object Repository/object 2’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/object 2’ located by ‘By.xpath: //*[@id = xpath]’ not found)

1 Like

Hi Vasudha,

The error about not being able to find Object 2 with an xpath of “//*[@id = xpath]” tells me that the object might not be set up correctly in the Object Repository. Could you please share what Object 1 and Object 2 look like in the repository?

Thanks,

Chris

Hi Chris,

Thanks for prompt response.

For First Case- Error Message: Once Object 1 is found than also script searching for Object 2 and throw error message

10-11-2018 01:31:03 PM - [FAILED] - Unable to click on object ‘Object Repository/ClientPreManagement-400/ClientPage/NewManagerRoles/Page_ClientDetails/input_AddnewUser’ (Root cause: org.openqa.selenium.ElementNotVisibleException: element not visible

10-11-2018 01:31:04 PM - [FAILED] - Test Cases/Client Pre-Management Module - 400/UAT-LL-471-Add or edit a Client Manager/Add_Edit_ClientManager FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/ClientPreManagement-400/ClientPage/NewManagerRoles/Page_ClientDetails/3dots_ClientManager’ located by ‘By.xpath: //*[@id = ‘LoopedIn_Theme_wt5_block_wtMainContent_Clients_WEB_wt111_block_wt10_VVITFramework_WEB_wt1_block_wtlblToggle’]’ not found

For Second Case - Error Message: Script searching for Object 1 if its not found than it should search Object 2 but here it stuck to find object 1 and failed

10-11-2018 09:46:31 AM - [FAILED] - Test Cases/Client Pre-Management Module - 400/UAT-LL-471-Add or edit a Client Manager/Add_Edit_ClientManager FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/ClientPreManagement-400/ClientPage/NewManagerRoles/Page_ClientDetails/input_ClientManager’ located by ‘By.xpath: id(“LoopedIn_Theme_wt5_block_wtMainContent_Clients_WEB_wt111_block_wt10_wt4”)[count(. | //input[@type = ‘submit’ and @name = ‘LoopedIn_Theme_wt5$block$wtMainContent$Clients_WEB_wt111$block$wt10$wt4’ and @id = ‘LoopedIn_Theme_wt5_block_wtMainContent_Clients_WEB_wt111_block_wt10_wt4’]) = count(//input[@type = ‘submit’ and @name = ‘LoopedIn_Theme_wt5$block$wtMainContent$Clients_WEB_wt111$block$wt10$wt4’ and @id = ‘LoopedIn_Theme_wt5_block_wtMainContent_Clients_WEB_wt111_block_wt10_wt4’])]’ not found

Hi Guys, how do we make sure that Failure is not reported when it goes to the Else loop? It somehow frightens the users that something failed when it should not be the case.

Also cannot enter the else loop without putting a CONTINUE_ON_ERROR failure handling

if (WebUI.verifyElementPresent(findTestObject(‘Object Repository/Page_PM - Management/span_Error_Message’), 6))
{
String ErrorMessage = WebUI.getText(findTestObject(‘Object Repository/Page_PM - Management/span_Error_Message’))
KeywordLogger log = new KeywordLogger()
log.logInfo(‘Creation of Contact failed for the new Contact with Error :’ + ErrorMessage)
}
else
{
KeywordLogger log = new KeywordLogger()
log.logInfo(‘Creation of Contact Passed’)
}