How can I add action when test fails?

I need to execute a script when a test case fails.

The script is this:

import org.openqa.selenium.WebDriver
import org.openqa.selenium.logging.LogEntries
import org.openqa.selenium.logging.LogEntry

LogEntries logs = driver.manage().logs().get(“browser”)

for (LogEntry entry : logs) {
println entry.getMessage()
}

According to this topic: Can I log browser console errors? - #3 by Edita_Markauskaite

Perhaps the below might help you, but as mentioned in the answers, there can be multiple reasons why a step might fail? I just read the Console log myself.

Might be helpful to check this link about test listeners:

Look for @teardownIfFailed annotaion.

com.kms.katalon.core.context.TestCaseContext has a getMessage() function that contains the error message if a test case is failed.
https://api-docs.katalon.com/com/kms/katalon/core/context/TestCaseContext.html

Here is a sample code:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import org.openqa.selenium.WebDriver
import org.openqa.selenium.logging.LogEntries
import org.openqa.selenium.logging.LogEntry

import com.kms.katalon.core.exception.StepFailedException
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.driver.WebUIDriverType
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

try {
	
	WebUI.openBrowser('')

	WebUI.navigateToUrl('http://demoaut.katalon.com/')

	WebUI.verifyElementPresent(findTestObject('Page_CURA Healthcare Service/a_Make Disappointment'), 10)

	WebUI.closeBrowser()
	
} catch (StepFailedException ex) {
	
	if (DriverFactory.getExecutedBrowser() == WebUIDriverType.CHROME_DRIVER) {
		WebDriver driver = DriverFactory.getWebDriver()
		LogEntries logs = driver.manage().logs().get("browser")
		for (LogEntry entry : logs){
			println(">>> ${entry}")
		}
	}
}

Let us assume that the Test Object Page_CURA Healthcare Service/a_Make Disappointment contains a mistake, therefore the call to WebUI.verifyElementPresent() will fail after 10 seconds.

When the call failed after 10 seconds, a StepExecutionException will be thrown. So the try { ... } catch (StepFailedException ex) { .... } will intercept the Exception and perform a post processing which will retrieve JavaScript console log from Chrome browser.

Hi @lucas.wolfgramm, :wave:

Just checking in to see if you were able to find the answer you need within this thread or not? If yes, it would be great if you could mark said reply as a solution :white_check_mark: to show your appreciation to the person who post said reply. Thanks :smile:

It worked here, but not with @teardownIfFailed.
Getting the log on @AfterTestCase(sampleAfterTestCase) is enough for me.

Thanks!!