How to pass value from @BeforeTestCase to @AfterTestCase (Test Listeners)

I have a value from @BeforeTestCase which I would like to pass into @AfterTestCase
Example:

@BeforeTestCase
String text = ‘a’

@AfterTestCase
println text

I tried to use GlobalVariable with the following but doesn’t do the trick:

@BeforeTestCase
String text = ‘a’
GlobalVariable.gText = text

@AfterTestCase
println GlobalVariable.gText

Please explain “doesn’t do the trick” (if you want help with a problem, you need to explain in detail what exactly is going wrong).

This works:

@BeforeTestCase
  def beforeTestCase(TestCaseContext testCaseContext) {
    GlobalVariable.RANDOM_NAME = "russ"
  }

  @AfterTestCase
  def afterTestCase(TestCaseContext testCaseContext) {
    WebUI.comment('RANDOM_NAME: ' + GlobalVariable.RANDOM_NAME)
  }

Also, please follow this advice:

1 Like

Hi mate, apologies for not providing details on the “doesn’t do the trick” part. Anyway, your code is similar to what I did in using GlobalVariables. The stup*d thing is that I didn’t do proper debug and checking that the actual value from the API (Response body) is passed into @BeforeTestCase.
The passing of value from @BeforeTestCase to @AfterTestCase works using GlobalVariables.

Thanks anyway for spending time responding to my query and happy holidays.