I need to make the test fail when it runs into an error message

image_2023-04-10_122941600
When this message pops up (The error message under name) I want the test to fail.

The element for this message is k-tooltip-icon k-icon k-i-warning. Could someone show me how to do it? Thanks!

2 Likes

Hi there @alex.bantjes,

Welcome to Katalon Community! :tada:

Regarding your question, I believe it would help if you could show us what you are dealing with by pasting the test code in your topic for example. Please refer to the topic below on how to best detail your questions. Thanks! :+1:

Thanks for the feedback, so basically every time i run the test and an error message like that(Shown in the image) pops up i want the test to automatically fail.


image_2023-04-10_122941600

image_2023-04-10_182630835

1 Like

Perhaps, you can try:

import com.kms.katalon.core.exception.StepErrorException as StepErrorException


if (WebUI.verifyTextPresent("'Name' must not be empty.", false)) {
    WebUI.takeScreenshot(gReportPathway + "NameNotEmpty.png")
    throw new StepErrorException("'Name' must not be empty.")
}
5 Likes

Hi @alex.bantjes,

Thanks for providing us with more information. As your topic get more replies from other members, please remember to engage with them so that you would get the answer you need.

And, if you find any replies that solve/answer your question, don’t forget to mark helpful replies as a solution :white_check_mark: so that others who may be asking similar questions / running into similar problems can find the solution as well!

1 Like

Thank you this helps, The only problem is that i would have to make multiple scripts because each warning box has a different message, is there maybe a script that would detect if the warning box (k-tooltip-icon k-icon k-i-warning) pops up and then fails the test ?

What I would do when trying to check if an element is present without relying on the text it contains is to see if the class is visible using javascript

I am guessing “k-tooltip-icon k-icon k-i-warning” is classes used for the popup, then I would do the following

ie.

if (WebUI.executeJavaScript('return document.querySelectorAll(".k-tooltip-icon.k-icon.k-i-warning")[0].checkVisibility()') {
    WebUI.takeScreenshot(gReportPathway + "NameNotEmpty.png")
    throw new StepErrorException("'Name' must not be empty.")
}

This is assuming that there is only one element on the page with this class. If there are multiple element with this class you might want to change the number between the block brackets

“[0]”

There might be a better solution out there, but this is what I do

1 Like

The issue is there seems to be a possible multitude of locations, so we would have to “check them all”.
As @jmeintjesn7 says, likely we would have to check if the element that has the class is visible. For the below, I would create a Keyword and call the Keyword to see if the error message was visible. At least it gives you something to check out.

WebDriver driver = DriverFactory.getWebDriver();

List<WebElement> list = driver.findElements(By.class('k-tooltip-icon k-icon k-i-warning'))
max = list.size()
if (max > 0) {
    for (int cnt = 0; cnt < max; cnt++) {
	    if (list.get(cnt).isDisplayed()) {
		    throw new StepErrorException("Encountered tooltip error.")
	    }
    }
}
1 Like


tried to use this but i think it does not find the element because either it fails when the element is not there and when its there it still fails.

What I can recommend using your snippet is the following between lines 53 and 64:

NB: remember to include the imports at the very top of your script if you have not already included them

import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement

//Delay after clicking save
WebUI.delay(1)

//Create an array of elements that has the class .k-icon.k-i-warning
WebDriver driver = DriverFactory.getWebDriver()
ArrayList<WebElement> popup = driver.findElements(By.cssSelector(".k-icon.k-i-warning"))

//Fail if array size is greater than 0
if(popup.size() > 0) {
	WebUI.comment('Pop-up block is present, test failed.')
	
	assert false
}

Another thing you could do instead of making a comment and assert to fail the test is to use KeywordUtil

So you can also do the following:

import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import com.kms.katalon.core.util.KeywordUtil

//Delay after clicking save
WebUI.delay(1)

//Create an array of elements that has the class .k-icon.k-i-warning
WebDriver driver = DriverFactory.getWebDriver()
ArrayList<WebElement> popup = driver.findElements(By.cssSelector(".k-icon.k-i-warning"))

//Fail if array size is greater than 0
if(popup.size() > 0) {
	KeywordUtil.markFailedAndStop('Pop-up block is present, test failed.')
}
2 Likes

I have made some changes to the import list in my previous comment. That should be my last edit.

2 Likes

Instead of the delay, how about:

//Delay after clicking save
WebUI.waitForPageLoad(10)
2 Likes

The reason why I used delay rather than waitForPageLoad is because form validation usually does not reload the page if the form is validated before attempting to submit. This might cause the test to pass when it should actually fail. For example the class “.k-icon.k-i-warning” is not yet on the page, but it is being checked for at the exact time validation is still busy.

If the page does load after save has been clicked, then waitForPageLoad will be much better to use, but if the validation is done client side and the page does not load, you will have to wait manually.

Also another thing to note is the project settings for Smart Wait. If the project already has smart wait enabled, then waitForPageLoad is not even needed. The setting is indicated below and as far as I know this setting is enabled by default:

Edit: It also looks like in the first screenshot posted that the save button is part of a modal, the fact that the modal is still visible with the validation popup tells me that this page does not load when save is clicked, thus a manual wait will be needed to ensure the validation popup is properly visible before doing a check for it.

2 Likes

in both these scrips the test just failed immediately
main reason( I Think) Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to open browser with url: ‘’
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed

@alex.bantjes can you please provide a screenshot of the entire error message and of the sections where you entered the scripts I provided?

And also make sure you entered the code between lines 53 and 64 and the import lines should be included at the top of your test case

Use the 3 key combo, CTRL + SHIFT + O (oh) to have KS create all the necessary import statement for you. Note it will also remove those import statements that are not used.

2 Likes