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)