I want to force an error

Hi all

I want to force an error when an object appears in a web test.

For example, if an error message appears on my web page, I want the test fails

Thy for all.

Hi,

You can use KeywordUtil methods
import com.kms.katalon.core.util.KeywordUtil

KeywordUtil.markFailed("Failed message")
KeywordUtil.markError("Error message")

https://api-docs.katalon.com/com/kms/katalon/core/util/KeywordUtil.html

Hi,

Thanks for this. But I have some questions about it. ¿I will be like…?

if (findtestobject(Error_message)) {
KeywordUtil.markFailed(“Failed message”)
}

For example

import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.util.KeywordUtil
if(WebUI.verifyTextPresent('The message error on your website', false, FailureHandling.OPTIONAL)) {
   KeywordUtil.markFailed('I want you failed')
}

With FailureHandling.OPTIONAL you can and an else statement.
Otherwise :

if(WebUI.verifyTextPresent('The message error on your website', false)) {
   KeywordUtil.markFailed('I want you failed')
}
1 Like

Perfect, it works. Thank u so much.

You’re welcome :smile:

1 Like

Hey there, sorry to revive a dead thread!

Is there a way to have those two lines in every test case I create by default? I’ve been using that failure handling import, but it gets to be a pain copying and pasting it every single time.

Cheers

Yes, there is - you can use a listener.

@BeforeTestCase
def sampleBeforeTestCase(TestCaseContext testCaseContext) {
  if(WebUI.verifyTextPresent('The message error on your website', false)) {
     KeywordUtil.markFailed('I want you failed')
  }
}

But I don’t think it’s a good idea to do test steps in a listener. You will need more code – atleast enough to make sure the page is loaded and ready.

Here’s the doc:

Hey Russ,

Thanks for your reply!

Sorry- not quite what I meant. The two lines I would want on each test case are

import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.util.KeywordUtil

I was hoping there was a way to add them to the “import static” or “import” lines that are in the script tab each time I create a new TC without having to paste them underneath each time and save.

Pardon my terminology- I’m a newbie.

Ah, okay.

When you make reference to the relevant methods (KeywordUtil, for example), you should be able to hit Ctrl+Shift+O to reset your imports (unused are removed, missing are added).

1 Like

Oh wow- serious time saver. Thank you!

1 Like