How to check element status in conditional statement?


This is a companion discussion topic for the original entry at https://docs.katalon.com/katalon-studio/docs/how_to_check_element_status_in_conditional_statement.html

Katalon should consider the following use case: 1) an element is not present and 2) that’s okay. Don’t fail the test case. .Optional still fails the test case. At a glance the progress-bar is red, there are 2 giant red X’s on the step and test cases, all because the webpage is working normally. This is not acceptable. You need a FailureHandling.DO_NOT_THROW_ERROR option I suppose.

Or I could just drop down to Selenium’s objects… but then why am I using Katalon?

hello,
ok great to be able to check if an element is visible or not without failing the test.

But how to code it in order to avoid any unwanted stacktrace in my console ?

Here is an example with my code :

int cpt = 0

while ((cpt < 10) && WebUI.verifyElementPresent(findTestObject(‘Object Repository/Authentification/Page_Reflex - QCSGERFX023/img_R’),
1, FailureHandling.OPTIONAL)) {
WebUI.doubleClick(findTestObject(‘Object Repository/Authentification/Page_Reflex - QCSGERFX023/img_R’))
cpt++
}

it produces this huge message in console which is too verbose for me :

[2020-07-27T10:36:01.521] 2020-07-27 10:36:01.520 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
[2020-07-27T10:36:01.521] 2020-07-27 10:36:01.520 INFO c.k.katalon.core.main.TestCaseExecutor - CALL Test Cases/Common Test Cases/Fermeture des fenetres ouvertes
[2020-07-27T10:36:01.523] 2020-07-27 10:36:01.523 DEBUG t.Fermeture des fenetres ouvertes - 1: cpt = 0
[2020-07-27T10:36:01.524] 2020-07-27 10:36:01.524 DEBUG t.Fermeture des fenetres ouvertes - 2: while (cpt < 10 && verifyElementPresent(findTestObject(“Object Repository/Authentification/Page_Reflex - QCSGERFX023/img_R”), 1, OPTIONAL))
[2020-07-27T10:36:03.057] 2020-07-27 10:36:03.056 INFO c.k.k.c.webui.common.WebUiCommonHelper - Unable to find the element located by ‘By.xpath: //div[contains(@class,‘dijitDialog’)][not(contains(@class,“a-windowNotVisible”))][not(@class=“dijitDialogDisabledMask”)][not(contains(@style,“hidden”))][last()]//span[contains(@id,’_SYSMNU_ITEM_text’)][@id!=“RFXDEM-ID_FEN_PRINC-1-_SYSMNU_ITEM_text”]/img’. Please recheck the objects properties to make sure the desired element is located.
[2020-07-27T10:36:03.447] 2020-07-27 10:36:03.446 WARN c.k.k.core.keyword.internal.KeywordMain - com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Authentification/Page_Reflex - QCSGERFX023/img_R’ located by ‘By.xpath: //div[contains(@class,‘dijitDialog’)][not(contains(@class,“a-windowNotVisible”))][not(@class=“dijitDialogDisabledMask”)][not(contains(@style,“hidden”))][last()]//span[contains(@id,’_SYSMNU_ITEM_text’)][@id!=“RFXDEM-ID_FEN_PRINC-1-_SYSMNU_ITEM_text”]/img’ not found (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Authentification/Page_Reflex - QCSGERFX023/img_R’ located by ‘By.xpath: //div[contains(@class,‘dijitDialog’)][not(contains(@class,“a-windowNotVisible”))][not(@class=“dijitDialogDisabledMask”)][not(contains(@style,“hidden”))][last()]//span[contains(@id,’_SYSMNU_ITEM_text’)][@id!=“RFXDEM-ID_FEN_PRINC-1-_SYSMNU_ITEM_text”]/img’ not found
[2020-07-27T10:36:03.447] at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword$_verifyElementPresent_closure1.doCall(VerifyElementPresentKeyword.groovy:85)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword$_verifyElementPresent_closure1.call(VerifyElementPresentKeyword.groovy)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword.verifyElementPresent(VerifyElementPresentKeyword.groovy:92)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword.execute(VerifyElementPresentKeyword.groovy:68)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(WebUiBuiltInKeywords.groovy:1444)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementPresent$7.call(Unknown Source)
[2020-07-27T10:36:03.447] at Fermeture des fenetres ouvertes.run(Fermeture des fenetres ouvertes:25)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.doCall(CallTestCaseKeyword.groovy:59)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.call(CallTestCaseKeyword.groovy)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.callTestCase(CallTestCaseKeyword.groovy:81)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.execute(CallTestCaseKeyword.groovy:44)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.BuiltinKeywords.callTestCase(BuiltinKeywords.groovy:334)
[2020-07-27T10:36:03.447] at Authentification si besoin.run(Authentification si besoin:46)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.doCall(CallTestCaseKeyword.groovy:59)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.call(CallTestCaseKeyword.groovy)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.callTestCase(CallTestCaseKeyword.groovy:81)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.execute(CallTestCaseKeyword.groovy:44)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
[2020-07-27T10:36:03.447] at com.kms.katalon.core.keyword.BuiltinKeywords.callTestCase(BuiltinKeywords.groovy:334)
[2020-07-27T10:36:03.448] at Preparation Despatch from stock.run(Preparation Despatch from stock:20)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.doCall(CallTestCaseKeyword.groovy:59)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.call(CallTestCaseKeyword.groovy)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.callTestCase(CallTestCaseKeyword.groovy:81)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.execute(CallTestCaseKeyword.groovy:44)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.keyword.BuiltinKeywords.callTestCase(BuiltinKeywords.groovy:334)
[2020-07-27T10:36:03.448] at DGB - palhomo avec confirmation et sortie de stock.run(DGB - palhomo avec confirmation et sortie de stock:80)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:169)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:142)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:91)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:157)
[2020-07-27T10:36:03.448] at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
[2020-07-27T10:36:03.448] at TempTestSuite1595838665006.run(TempTestSuite1595838665006.groovy:37)
[2020-07-27T10:36:03.448] )
[2020-07-27T10:36:03.448] 2020-07-27 10:36:03.448 INFO c.k.katalon.core.main.TestCaseExecutor - END CALL Test Cases/Common Test Cases/Fermeture des fenetres ouvertes
[2020-07-27T10:36:03.448] 2020-07-27 10:36:03.448 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------

I agree. I too want to check for elements present or not, just to make a decision.
Here’s the “use case”

  • My page can come in two flavors, depending on the build, I am presented with either or another when a new build is available (every 6 hours for us).
  1. I want to check if an element of page 1 is present, and simply get true or false
  2. I want to check if an element of page 2 is present, and simply get true or false
    depending on which 1) or 2) is true, I do the processing and move on
    What I don’t want is any logging, no messages, no stack traces - just simple
    true/false to take proper action.
    Yep, using FailureHandling.OPTIONAL is progress but still leaves junk in the logs.
    How about a FailureHandling.NONE , meaning literally, no message, no logs ?

Can you please update us on this fix?

I also would like to know if there’s an update to address this specific case (just check if an element is present/visible without throwing an exception if not).

Katalon Studio provides 2 built-in keywords WebUI.verifyElementPresent and WebUI.waitForELementPresent. What’s the difference?

WebUI.verifyElementPresent LINE# 85

} catch (WebElementNotFoundException ex) {
                WebUIKeywordMain.stepFailed(ExceptionsUtil.getMessageForThrowable(ex), flowControl, null, true)

When the target element is not found in the web page, verifyElementPresent() throws an StepFiledException. Consequently, a big StackTrace will be printed in the Console.

On the other hand, waitForElementPresent() behaves differently.

WebUI.waitForElementPresent LINE#87

               } catch (WebElementNotFoundException e) {
                    logger.logWarning(MessageFormat.format(StringConstants.KW_MSG_OBJ_IS_NOT_PRESENT_AFTER_X_SEC, [to.getObjectId(), timeOut] as Object[]), null, e)
                    return false

As code tells, when a target element is not found,
Web.waitForElementPresent() does not throw Exception, it will write 1 or 2 lines of logs.

You can choose either of 2 keywods. If you do not mind Stack Trace, you may use verify keyword. If you do not like any Stack Trace, you should prefer waitFor keyword.


If you use WebUII.waitForElement() keyword, you wouldn’t see a big Stack Trace, but would see 2 lines of log messages emitted through SLF4J, like this.

2021-08-14 10:22:01.859 INFO  c.k.k.c.webui.common.WebUiCommonHelper   - Unable to find the element located by 'By.xpath: //*[@id='fake']'. Please recheck the objects properties to make sure the desired element is located. 
2021-08-14 10:22:01.861 WARN  k.k.c.w.k.b.WaitForElementPresentKeyword - Object '//*[@id='fake']' is not present after 3 second(s)

You may want to erase these 2 log messages.

It is possible. You want to write Include/config/log.properites as this:

logging.level.com.kms.katalon.core.webui.common.WebUiCommonHelper=WARN
logging.level.com.kms.katalon.core.webui.keyword.builtin.WaitForElementPresentKeyword=ERROR

then the 2 warning messages will stop. For detail, see


Even after you erased the log messages through SLF4J in the Console tab, still you would find StepExecution Logs in the Log Viewer tab, like this

You can suppress these StepExecution logs in the Log Viewer if you have the Enterprise license. See


Let me show you another variant of experiment:

import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

/**
 * check how [WebUI.waitForElementPresentKeyword](https://github.com/katalon-studio/katalon-studio-testing-framework/blob/master/Include/scripts/groovy/com/kms/katalon/core/webui/keyword/builtin/WaitForElementPresentKeyword.groovy) works.
 */
String x = "//*[@id='fake']"
TestObject tObj = new TestObject(x)
tObj.addProperty("xpath", ConditionType.EQUALS, x)
WebUI.openBrowser("https://www.google.com")

boolean found

WebUI.comment("trying waitForElementPresent...")
found = WebUI.waitForElementPresent(tObj, 3, FailureHandling.STOP_ON_FAILURE)
WebUI.comment("found=" + found + " after STOP_ON_FAILURE")


WebUI.comment("trying waitForElementPresent...")
found = WebUI.waitForElementPresent(tObj, 3, FailureHandling.CONTINUE_ON_FAILURE)
WebUI.comment("found=" + found + " after CONTINUE_ON_FAILURE")

WebUI.comment("trying waitForElementPresent...")
found = WebUI.waitForElementPresent(tObj, 3, FailureHandling.OPTIONAL)
WebUI.comment("found=" + found + " after OPTIONAL")

WebUI.closeBrowser()

When I executed this, I saw in the Console:

found=false after STOP_ON_FAILURE
...
found=false after CONTINUE_ON_FAILURE
...
found=false after OPTIONAL
...

This result is interesting to me. I expected to see the test case to stop at the line of

found = WebUI.waitForElementPresent(tObj, 3, FailureHandling.STOP_ON_FAILURE)

But actually it does not stop. This proves that FailureHandling as 3rd argument to waitForElementPresent keyword has no effect.

You can find the reason. Read the source of waitForElementtPresent keyword in the source code. The implementation just ignores the 3rd parameter. In other word, the method signature of the waitForElementPreset keyword should have been

WebUI.waitForElementPresent(TestObject testObject, int timeout)

rather than

WebUI.waitForElementPresent(TestObject testObject, int timeout, FailureHandling flowControl)

But it’s too late to change the method signature of the published API. For the backward compatibility, this signature should stay as is.

1 Like