Checking for the presence of an object without throwing an error

One of my goals has to been to make my tests less noisy and not generate errors in the log unless it’s really necessary. For example, if an object is missing from the page, that may or may not be an error. If a tab is missing, the user may not meet the criteria to make it display.

When checking for the existence of an object using the WebUI.verifyElementVisible() keyword, a lot of additional logging that can be misleading. For example, this code block will execute as expected, but generates 2 entries for the Failed log.

    @Keyword
    //Function to determine if an item exists on the page
    boolean verifyObjectVisible(String objectReference) {
        try {
            WebUI.verifyElementPresent(findTestObject(objectReference),5)
            return true;
        } catch (Exception e) {
            log.logWarning("The object with the name, " + objectReference + " was not found. Exiting Test.")
            return false;
        }
    }

There is a cleaner way to check for an object using the WebUiCommonHelper.findWebElement command, which is part of the,

import com.kms.katalon.core.webui.common.WebUiCommonHelper library and performs the same function. I like it better because it allows for better handling of errors and logging.

Below is a Custom Keyword that wraps the use of the findWebElement statement within a Try/Catch block to determine if the object exists. The benefit is suppressing an error if the object doesn’t exit.

    @Keyword
    //Function to determine if an item exists on the page using WebUiCommonHelper.findWebElement
    //This does not throw an exception error to the log so it looks cleaner
    boolean verifyObjectPresent(String objectReference) {
        try {
            WebUiCommonHelper.findWebElement(findTestObject(objectReference),5)
            return true;
        } catch (Exception e) {
            return false;
        }
    }

A call to the Custom Keyword would then look like:

    //Check if there are existing notes on the page and count how many
    boolean elementVisible=CustomKeywords.'tools.commonCode.verifyObjectPresent'(katalonObject)
    if (elementVisible==true){
        originalNumberOfTasks=CustomKeywords.'tools.commonCode.countRowsPerPage'(xpath)
    } else {
        log.logWarning('Customer has no Tasks available at this time')
    }

This gives control within the code whether or not to display an error. In looking at the code above, if no Tasks are displayed on the page, a notification message is display, not an error. The test hasn’t failed and there is no problem with the site, a user may not have created an tasks which is why they don’t exit.

 

The verifyElementPresent or verifyElementVisible will perform the same task, but will log multiple errors if an element isn’t found, even when wrapped in a Try/Catch block.

Then again, I could be wrong.

Author Signature for Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.