I’m working with an if loop that checks for the presence of an element. If the element is present, it enters the loop; if not, it should continue executing the script without throwing any console errors or exceptions for that element. I want the script to proceed seamlessly regardless of the element’s presence.
I’ve used FailureHandling.OPTIONAL, and it works well, but I don’t want to see any errors in the console.
Thank you very much for your topic! It may take a little while before Katalon team member or others forum members respond to you.
In the meantime, you can double-check your post to see if you can add any extra information i.e. error logs, HTML codes, screenshots, etc. Check out this posting guide to help us help you better!
You can put your code into a Keyword and then all you should get is a “response” that the code ran. Generally, you won’t get any error message unless your pathway to the object changes and it cannot be found anymore. If you have Katalon Studio after v9.0 then you need the Enterprise edition to use Keywords.
@Keyword
public void reviewPermission() {
if (WebUI.verifyElementPresent(findTestObject(permissionField), 5), FailureHandling.OPTIONAL) {
WebUI.click(findTestObject(permissionField))
}
}
Also, just a note that I would change from using “verifyElementPresent” and instead either use: “verifyElementVisible” or “verifyElementClickable”. These two statements indicate that the object is visible or is ready to click, whereas present just means it is on the page, but may not be visible or clickable.
if (WebUI.verifyElementVisible(findTestObject(permissionField))) {
if (WebUI.verifyElementClickable(findTestObject(permissionField))) {
You should rather use waitForElementPresent keyword rather than verifyElementPresent keyword.
What’s the difference of these 2 keywords? The waitForElementPresent will never throw Exception when the element is found absent. The verifyElementPresent will throw Exception.