In the groovy script, how may we know the current test case id?

Greetings,

We have a simple requirement whereby we wish to record the time it took for a test case to run.

One way I saw of achieving this was:

// Additional imports needed for time duration calculations:
import groovy.time.TimeDuration
import groovy.time.TimeCategory


WebUI.openBrowser('')

time_started = new Date()
println "TEST STARTED AT (UTC): " + time_started.format("yyyyMMdd-HH:mm:ss.SSS", TimeZone.getTimeZone('UTC'))

'Load the target site page.'
WebUI.navigateToUrl(P_Site_page_URL)

//
// Perform our test steps here...
//

time_finished = new Date()
println "TEST FINSIHED AT (UTC): " + time_finished.format("yyyyMMdd-HH:mm:ss.SSS", TimeZone.getTimeZone('UTC'))

TimeDuration duration = TimeCategory.minus(time_finished, time_started)

println "TEST DURATION (milliseconds): " + duration.toMilliseconds().toString()

file1 = new File('some_file_name.txt')
file1.write "Duration=" + duration.toMilliseconds().toString() + '\n'

My query:

Is there some way to ascertain the current test case name (or ID) so that I may use it as part of our output file’s filename or inside the output file itself?

Regards,
Jinesh

If you run the test case in a test suite, the elapsed time will show in the test suite report, which locates in the Reports folder.

However, I would also like to know how to retrieve the test case name/id.

Being able to get the id of the Test Case on the script (using an API) is extremely useful. We can use that for all sorts of lookups for test resources, data, reports, etc.

This API almost gives us that.
com.kms.katalon.core.configuration.RunConfiguration.getExecutionSourceId()

Unfortunately it only returns the id of the test case if you’re running a single Test Case. When you’re running a Suite/Collection of Suites it provides the name of the suite/collection - I do think that is the correct behavior for this API, so not asking to change this. My request really is: can we get a similar API (for example getExecutionTestCaseId()) that will yield the ID of the currently executing Test Case in all situations?

Thanks!

1 Like

You can use Test Listeners in this case to get current test case as I can see it provides an API for you to get current test case ID already:

class NewTestListener {
	/**
	 * Executes before every test case starts.
	 * @param testCaseContext related information of the executed test case.
	 */
	@BeforeTestCase
	def sampleBeforeTestCase(TestCaseContext testCaseContext) {
		println testCaseContext.getTestCaseId()
	}

	/**
	 * Executes after every test case ends.
	 * @param testCaseContext related information of the executed test case.
	 */
	@AfterTestCase
	def sampleAfterTestCase(TestCaseContext testCaseContext) {
		println testCaseContext.getTestCaseId()
	}

Fantastic! This works for us.
Thank you.

hi…how do I get my cucumber scenario names…I am able to get non BDD test case names but when I us the same command for cucumber feature file it returns cucumber feature name but I am interested in cucumber scenario names.

please help!

regards,
Krishna

Hi, Can you please look into this.

Hi,

The solution whether you want to get the test case name in an individual test case run or a test suit run, follow the below steps;

  1. Create a Test Listener, (e.g. NewTestListener)

1.1) In the NewTestListener class define a static string variable

class NewTestListener {
static String myTestCaseName

1.2)Inside the sampleBeforeTestCase assign the following value to your variable

       _**static String myTestCaseName**_ =  getTestCaseRelativeId(testCaseContext.testCaseId) 

2)Now inside your test case define a variable and assign the static variable to it

     Def myTestCase = NewTestListener.myTestCaseName

I hope this was helpful.

Good Luck

Anyone has solution for this?

It is necessary to do an import:

import static com.kms.katalon.core.testcase.TestCaseFactory.getTestCaseRelativeId

1 Like