I am looking for a way to verify element present for either of two elements.
The scenario is on a particular page:
- Both elements can be present
- One of the two elements can be present
But the test should fail if both elements are not present.
I have the following:
assert WebUI.verifyElementPresent(findTestObject('Object1'), 1, FailureHandling.OPTIONAL) ||
WebUI.verifyElementPresent(findTestObject('Object2'), 1, FailureHandling.OPTIONAL)
This code works when it doesn’t find one of the two test objects. For example, if it does not find the first one, it checks the second statement and moves on. Similarly if it finds the first one, it tries to find the second but fails and still continue, which is fine.
But when it neither finds the first one, nor the second one, it still continues due to OPTIONAL failure handling. And if I change the failure handling to STOP_ON_FAILURE, the test fails if it does not find one of the two elements.
Can someone let me know how I can manage this?
I doubt it.
I tried to reproduce your code and verified.
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser('')
WebUI.navigateToUrl("http://example.com")
boolean b1 = WebUI.verifyElementPresent(findTestObject("Object1"), 1, FailureHandling.OPTIONAL)
WebUI.comment("b1=${b1}")
boolean b2 = WebUI.verifyElementPresent(findTestObject("Object2"), 1, FailureHandling.OPTIONAL)
WebUI.comment("b2=${b2}")
boolean b3 = b1 || b2
WebUI.comment("b3=${b3}")
WebUI.closeBrowser()
assert b1 || b2
The output was like this:
...
2022-01-07 17:27:44.775 INFO c.k.k.c.keyword.builtin.CommentKeyword - b1=false
...
2022-01-07 17:27:47.075 INFO c.k.k.c.keyword.builtin.CommentKeyword - b2=false
...
2022-01-07 17:27:47.078 INFO c.k.k.c.keyword.builtin.CommentKeyword - b3=false
...
2022-01-07 17:27:47.213 ERROR c.k.katalon.core.main.TestCaseExecutor - ❌ Test Cases/farooqi FAILED.
Reason:
Assertion failed:
assert b1 || b2
| | |
| | false
| false
false
...
The last statement
assert b1 || b2
failed. So the test case failed.