In a test listener, return the last executed step of a test case

I’ve been playing around a bit with the AfterTestCase annotation, and I’ve been experimenting with building a single report file with it. I know it’s possible to print out the test case ID, status, and error message, but I am wondering if it’s also possible to somehow retrieve the last step that was executed in a test case so people who read the report may quickly identify where a failure occurred.

1 Like

As a follow-up: I could settle for just retrieving the last executed step number, e.g. “7: closeBrowser()” would return 7. Is there any way I could read directly from the Katalon console? I could do a little string manipulation to extract that 7 if I so needed.

I figured out a (sort of) working method. Rather than reading from the console itself, I redirect the test case’s output to a text file and perform my operations on that:

@BeforeTestCase
def BeforeTestCase(TestCaseContext testCaseContext) {			
	stdOut = System.out //Store the current standard output for later
	PrintStream fileOut = new PrintStream("output.txt")
	System.setOut(fileOut)
}

@AfterTestCase
def AfterTestCase(TestCaseContext testCaseContext) {
	System.setOut(stdOut) //Return the standard output to its original destination
	def content = new String(Files.readAllBytes(Paths.get("output.txt")))
	def startDelimiter = "-e[0;39m e[39m"
	def endDelimiter = "e[0;39m"
	def lastLineBegin = content.lastIndexOf(startDelimiter)
	def lastLineEnd
	def trigger = false	
	for(def index = content.indexOf(endDelimiter) ; index >= 0 ; index = content.indexOf(endDelimiter, index + 1)) {
		lastLineEnd = index
		if(trigger)
			break
		if(lastLineEnd > lastLineBegin)
			trigger = true
	}
	def lastStep = content.substring(lastLineBegin + startDelimiter.length(), lastLineEnd)
	println("Last step: " + lastStep) //Outputs something like "7: closeBrowser()"
}