If text present, show failed step

I have a simple thing I’m starting with, I want to see if the text “2018” is present on a page, if it is, I want to show it as a failed step. I have this now, but it keeps showing as passed.

WebUI.verifyTextPresent(‘2018’, true, FailureHandling.STOP_ON_FAILURE)

I’m new to this, what am I missing?

First thing is the “true” in the verifyTextPresent method means you want to use Regular Expression (RegEx). RegEx allows you to do a similar comparison using wildcards. You are not using any wildcard in your text, so I would change the statement to use false, like I do below.

The second thing is you have to use the NOT version of the method, because you want it to fail if the text is present, so if it is not present, then all’s good. However, the failure is that the text is present and we were asking for it to “not be present”. Since the below statement is not true, it fails and you get STOP_ON_FAILURE.

WebUI.verifyTextNotPresent('2018', false, FailureHandling.STOP_ON_FAILURE)

This is sort of like reverse logic. And you could always put your statement and my statement in your Test Case and see which one blows up. Yaaaah!

3 Likes

So, I tried that, but get a LONG error.

06-26-2023 08:07:35 PM Test Cases/Case2

Elapsed time: 9.521s

Test Cases/Case2 FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to verify text ‘2018’ is not present
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
at com.kms.katalon.core.webui.keyword.builtin.VerifyTextNotPresentKeyword.verifyTextNotPresent(VerifyTextNotPresentKeyword.groovy:82)
at com.kms.katalon.core.webui.keyword.builtin.VerifyTextNotPresentKeyword.execute(VerifyTextNotPresentKeyword.groovy:68)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyTextNotPresent(WebUiBuiltInKeywords.groovy:1740)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyTextNotPresent$1.call(Unknown Source)
at Case2.run(Case2:24)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1687824451810.run(TempTestCase1687824451810.groovy:25)
Caused by: com.kms.katalon.core.exception.StepFailedException: Text ‘2018’ is present on page
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.builtin.VerifyTextNotPresentKeyword$_verifyTextNotPresent_closure1.doCall(VerifyTextNotPresentKeyword.groovy:77)
at com.kms.katalon.core.webui.keyword.builtin.VerifyTextNotPresentKeyword$_verifyTextNotPresent_closure1.call(VerifyTextNotPresentKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
… 17 more

Did your test script come to inglorious end or did it keep on going? If it came to an inglorious end, then good (FailureHandling.STOP_ON_FAILURE). If it kept on going, huh!

1 Like

It came to an abrupt halt. So that’s good?

The text “Unable to verify text ‘2018’ is not present” is what concerned me.

So if I wanted it to continue running the script, but show a failure point at that line, is that possible?

Thank you

It’s the STOP_ON_FAILURE that causes the test script to “blow up”. To keep on going, you can use either, FailureHandling.CONTINUE_ON_FAILURE, which will show a red error and allow you to isolate the statement if you use the “Only show failed step” icon (on the side of the Log Viewer),

image

becomes:
image

or you can use FailureHandling.OPTIONAL, which will show as a yellow warning.

image

Edit: Lastly, if you don’t use the FailureHandling in the parameters, then the project preferences are used and the default setting is OPTIONAL.

Check the doc

Just change your code to:

WebUI.verifyTextPresent('2018', false, FailureHandling.STOP_ON_FAILURE)
// '2018' is interpreted as a plain string

If the 1st arugment is interpreted as a string, then the keyword tries to find the text to appear ANYWHERE in the page’s content text.

Or you can write:

WebUI.verifyTextPresent('.*2018.*', true, FailureHandling.STOP_ON_FAILURE)
// '.*2018.*' is interpreted as a Regular Expression

If the 1st argument is interpreted as an Regular Expression, then the verifyTextPresent keyword tries the exact match against the whole content text.

If a string “2018” is present somewhere in the page, then either of the 2 codes above will throw an StepFailedException. This must be what you wanted.