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

**URL:** <https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [November 9, 2017, 12:38pm UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262 "2017-11-09T12:38:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jinesh\_Choksi](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jinesh\_Choksi](https://forum.katalon.com/u/Jinesh_Choksi)\
**Post date:** [November 9, 2017, 12:38pm UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/1 "2017-11-09T12:38:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zheng\_Li](https://avatars.discourse-cdn.com/v4/letter/z/977dab/32.png) [@Zheng\_Li](https://forum.katalon.com/u/Zheng_Li)\
**Post date:** [December 28, 2017, 4:26pm UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/2 "2017-12-28T16:26:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andres\_Barbaro](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Andres\_Barbaro](https://forum.katalon.com/u/Andres_Barbaro)\
**Post date:** [December 29, 2017, 3:11pm UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/3 "2017-12-29T15:11:01Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Zarashima](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/zarashima/32/928_2.png) [@Zarashima](https://forum.katalon.com/u/Zarashima)\
**Post date:** [January 1, 2018, 9:50am UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/4 "2018-01-01T09:50:08Z")

</div>

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()
	}
```

---

<div class="post-metadata">

**Author:** ![Andres\_Barbaro](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Andres\_Barbaro](https://forum.katalon.com/u/Andres_Barbaro)\
**Post date:** [January 2, 2018, 11:21pm UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/5 "2018-01-02T23:21:05Z")

</div>

Fantastic! This works for us.  
Thank you.

---

<div class="post-metadata">

**Author:** ![krishna.x.kant](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@krishna.x.kant](https://forum.katalon.com/u/krishna.x.kant)\
**Post date:** [March 15, 2019, 10:49am UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/6 "2019-03-15T10:49:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krishna.x.kant](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@krishna.x.kant](https://forum.katalon.com/u/krishna.x.kant)\
**Post date:** [March 17, 2019, 12:03pm UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/7 "2019-03-17T12:03:33Z")

</div>

Hi, Can you please look into this.

---

<div class="post-metadata">

**Author:** ![Mustafa\_Abdel](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@Mustafa\_Abdel](https://forum.katalon.com/u/Mustafa_Abdel)\
**Post date:** [March 26, 2019, 11:52pm UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/8 "2019-03-26T23:52:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laxminarayana](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/laxminarayana/32/1296_2.png) [@laxminarayana](https://forum.katalon.com/u/laxminarayana)\
**Post date:** [April 2, 2019, 10:11am UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/9 "2019-04-02T10:11:48Z")

</div>

Anyone has solution for this?

---

<div class="post-metadata">

**Author:** ![diogo.soares](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@diogo.soares](https://forum.katalon.com/u/diogo.soares)\
**Post date:** [May 5, 2020, 12:10pm UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/10 "2020-05-05T12:10:58Z")

</div>

It is necessary to do an import:

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

---

<div class="post-metadata">

**Author:** ![albert.vu](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/albert.vu/32/20595_2.png) [@albert.vu](https://forum.katalon.com/u/albert.vu)\
**Post date:** [August 8, 2022, 7:32am UTC](https://forum.katalon.com/t/in-the-groovy-script-how-may-we-know-the-current-test-case-id/8262/11 "2022-08-08T07:32:05Z")

</div>


