Can we get testcase name from code?

No. Your understanding is wrong.

Please read the following code again more carefully; please find how the value of GlobalVariable.currentTestCaseId is resolved; it is found dynamically by calling testCaseContext.getTestCaseId().

class NewTestListener {
  /**
   * Executes before every test case starts.
   * @param testCaseContext related information of the executed test case.
   */
  @BeforeTestCase
  def sampleBeforeTestCase(TestCaseContext testCaseContext) {
    println "testCaseContext.getTextCaseId()=${testCaseContext.getTestCaseId()}"
    GlobalVariable.currentTestCaseId = testCaseContext.getTestCaseId()
  }
1 Like