Can i turn off log of Mobile.verifyElementVisible(element, timeout, FailureHandling.OPTIONAL)

Question is rather simple.
Is it possible to turn off log spam on demand?

As you can see on screen I need to check element visibility as followed:
if (Mobile.verifyElementVisible(element, timeout, Failure.Handling.OPTIONAL)) {
//Do something
} else {
//Do something else
}

But problem is, when element is not visible, it spit like 30 lines of error message into log, that element was not found (which is expected)


(example snap, it goes at line… at line.. etc for 30+ lines)

Is it possible to moderate, silent it
(without need of writing my own method driver + findElements())
when FailureHandling.OPTIONAL / is expected that element will not be visible.

Thanks,
Best regards,
Jan.

3 Likes

Hi @Bishopek As an Enterprise user, I have created a support ticket for you on our Support Portal. Our Support Agent is currently reviewing it and will contact you through the ticket shortly.

Can i turn off log of Mobile.verifyElementVisible(element, timeout, FailureHandling.OPTIONAL)

Mobile.verifyElementVisible(..., FailureHandling.OPTIONAL) intentionally logs verbose “timeout” details even on expected non-visibility to aid debugging, but you can suppress via custom keyword wrapping or listener tweaks.

Custom Silent Check

Create this keyword in Keywords > New > TestObject Keyword:

import com.kms.katalon.core.logging.KeywordLogger as KeywordLogger
import com.kms.katalon.core.util.KeywordUtil

@Keyword
boolean silentlyVerifyElementVisible(TestObject to, int timeout) {
    KeywordLogger log = new KeywordLogger()
    log.logInfo("Checking visibility of ${to.getObjectId()}")
    try {
        boolean visible = Mobile.waitForElementVisible(to, timeout)
        if (visible) {
            KeywordUtil.markPassed("Element visible")
            return true
        } else {
            KeywordUtil.markPassed("Element not visible (expected optional)")
            return false
        }
    } catch (Exception e) {
        KeywordUtil.markPassed("Element absent after ${timeout}s (no error)")
        return false
    }
}

Use: if (silentlyVerifyElementVisible(element, 10)) { /* visible */ } else { /* absent */ }. No spam, custom logs only.

Global Log Filter

Project > Settings > Test Suite > Execution Log Level > ERROR (hides INFO/WARN). Or implement TestListener override onKeywordInfo to skip verifyElementVisible traces.

Instead of mobile.verifyElement Visible keyword, you should use mobile.waitForElementPresent keyword which will NOT throw any Exception.


Let me explain my idea by code.

The following is the implemenation of mobile.verifyElementPresent keyword:

    @CompileStatic
    public boolean verifyElementVisible(TestObject to, int timeout, FailureHandling flowControl) throws StepFailedException {
        return MobileKeywordMain.runKeyword({
            KeywordHelper.checkTestObjectParameter(to)
            timeout = MobileCommonHelper.checkTimeout(timeout)
            WebElement element = findElement(to, timeout)
            if (element != null) {
                if (element.isDisplayed()) {
                    logger.logPassed(MessageFormat.format(StringConstants.KW_LOG_PASSED_ELEMENT_X_VISIBLE, to.getObjectId()))
                    return true
                } else {
                    MobileKeywordMain.stepFailed(MessageFormat.format(StringConstants.KW_LOG_FAILED_ELEMENT_X_VISIBLE, to.getObjectId()), flowControl, null, true)
                    return false
                }
            } else {
                MobileKeywordMain.stepFailed(MessageFormat.format(StringConstants.KW_LOG_FAILED_ELEMENT_X_EXISTED, to.getObjectId()), flowControl, null, true)
                return false
            }
        }, flowControl, RunConfiguration.getTakeScreenshotOption(), to != null ?  MessageFormat.format(StringConstants.KW_MSG_FAILED_TO_CHECK_FOR_ELEMENT_X_VISIBLE, to.getObjectId()) : StringConstants.KW_MSG_FAILED_TO_CHECK_FOR_ELEMENT_VISIBLE)
    }

When the target element is found missing after the timeout expires, then the verifyElementVisible keyword internally calls the MobileKeywordMain.stepFailed(...) which will throw an StepFailedExcetion. The Exception will result a long lines of stack trace printed into the console.

On the other hand, the following is the implementation of mobile.waitForElementPresent keyword:

    @CompileStatic
    public boolean waitForElementPresent(TestObject to, int timeout, FailureHandling flowControl) throws StepFailedException {
        return MobileKeywordMain.runKeyword({
            KeywordHelper.checkTestObjectParameter(to)
            timeout = MobileCommonHelper.checkTimeout(timeout)
            WebElement element = findElement(to, timeout)
            if (element != null) {
                logger.logPassed(MessageFormat.format(StringConstants.KW_LOG_PASSED_ELEMENT_PRESENTED, to.getObjectId()))
                return true
            } else {
                logger.logWarning(MessageFormat.format(StringConstants.KW_MSG_OBJ_NOT_FOUND, to.getObjectId()))
                return false
            }
        }, flowControl, RunConfiguration.getTakeScreenshotOption(), to != null ? MessageFormat.format(StringConstants.KW_MSG_FAILED_TO_WAIT_FOR_ELEMENT_PRESENT, to.getObjectId()) : StringConstants.KW_MSG_FAILED_TO_WAIT_FOR_ELEMENT_X_PRESENT)
    }

When the target element is missing after the timeout, the keyword will call logWarning which will print a single line of message into the console; the keyword won’t call MobileKeywordMain.stepFailed(...). Therefore no Exception will be raised.

2 Likes

totally agree, waitForElementPresent with OPTIONAL is much cleaner and avoids the log spam
way better than using verifyElementVisible for this case

Hm.. this is interesting, thanks for advice, will try what u suggest and get back to you.

Thanks very mutch :slight_smile:

2 Likes