How to use a global variable in a different test case?

Hi,
My setup is as following:

  • A global variable defined as “name” with type string. Initial value is ‘’ (empty)
  • Testcase Registration, where name will be assigned a random value.
  • Testcase Login, where the loginname is the global variable name.
  • A test suite collection with these test cases and a few others

What Im trying to achieve is to create a new test account every time the test suite collection runs.

The problem is when I run the test suite collection, the Login testcase will fail, because the global variable name that is used for the login is empty. But in the test case Registration, name is a random value.

I assumed that it would be possible to assign a new value to a global variable, when a test case would run.
Is this assumption wrong and if so, what would be the best way of creating a new value for a global variable?

Or is the problem somewhere else?

Ignore this, lishenfan, I was talking crap. Refer to Mate’s answer below…

Unfortunately, GlobalVariables are “per test case”, not “per test suite”.

There are ways to create test-suite-globals (my term, not official) floating around but none of them are simple one-liners. For example, I place mine in an external file and read it from a listener. You, of course, would have the added task of needing to write the value in one TC, and read it in another. Sounds a little messy but certainly possible.

If you go this route, you probably want to use the JsonSlurper APIs.

http://docs.groovy-lang.org/latest/html/gapi/index.html?groovy/json/JsonSlurper

Actually, GlobalVariables are “per test suite”. They are reset after the TS execution ends. For example, here is what I use for keeping track of the TS statuses:

@AfterTestCase
def reporterAfterTestCase(TestCaseContext testCaseContext) {
WebUI.comment('>>>>>>>>>>>>>>>> Start: reporterAfterTestCase() <<<<<<<<<<<<<<<<<')
def status = testCaseContext.getTestCaseStatus()GlobalVariable.testsTotal++
if (status == 'FAILED') {
GlobalVariable.testSuiteStatus = 'FAILED'
GlobalVariable.testsFailed++
} 
if (status == 'PASSED'){
GlobalVariable.testsPassed++
}
if (status == 'ERROR'){
GlobalVariable.testSuiteStatus = 'ERROR'
GlobalVariable.testsError++
}
WebUI.comment('>>>>>>>>>>>>>>>> End: reporterAfterTestCase() <<<<<<<<<<<<<<<<<')
}
1 Like

They’ve never worked for me like that. Must have done something else wrong. Happens :wink:

Yep, I just tested this and you’re right, Mate. I wonder what (in my early days) led me to believe that was the issue I had? I recall I searched in the forum and read that this was the case. I tried again, but can’t find it (Search is due to improve soon).

Genuinely dumbfounded. I’ll edit my previous and upvote you.

Maybe they changed it up in the mean time? Anyway, thanks for the upvote!

Mate Mrse said:

Actually, GlobalVariables are “per test suite”. They are reset after the TS execution ends. For example, here is what I use for keeping track of the TS statuses:

@AfterTestCase

def reporterAfterTestCase(TestCaseContext testCaseContext) {
WebUI.comment(’>>>>>>>>>>>>>>>> Start: reporterAfterTestCase() <<<<<<<<<<<<<<<<<’)
def status = testCaseContext.getTestCaseStatus()GlobalVariable.testsTotal++
if (status == ‘FAILED’) {
GlobalVariable.testSuiteStatus = ‘FAILED’
GlobalVariable.testsFailed++
}
if (status == ‘PASSED’){
GlobalVariable.testsPassed++
}
if (status == ‘ERROR’){
GlobalVariable.testSuiteStatus = ‘ERROR’
GlobalVariable.testsError++
}
WebUI.comment(’>>>>>>>>>>>>>>>> End: reporterAfterTestCase() <<<<<<<<<<<<<<<<<’)
}


  

How can I deal with this problem ? I apreciatte you help!!

@lishenfan, as I told you here https://stackoverflow.com/questions/52180776/how-to-use-a-global-variable-in-a-different-test-case-katalon, assign a non-empty (random) value to your global variable. Then, when you run the test case, assign a new random value to the variable

GlobalVariable.name = 'newValue'

Mate Mrse said:

@lishenfan, as I told you here https://stackoverflow.com/questions/52180776/how-to-use-a-global-variable-in-a-different-test-case-katalon, assign a non-empty (random) value to your global variable. Then, when you run the test case, assign a new random value to the variable

GlobalVariable.name = 'newValue'

How to get this new value?

I don’t know…
If you can use any random string, try creating a random string in groovy:

import org.apache.commons.lang.RandomStringUtilsString charset = (('A'..'Z') + ('0'..'9')).join()
Integer length = 9
String randomString = RandomStringUtils.random(length, charset.toCharArray())(Copied from stackoverflow :D)
1 Like

Mate Mrse said:

I don’t know…
If you can use any random string, try creating a random string in groovy:

import org.apache.commons.lang.RandomStringUtilsString charset = (('A'..'Z') + ('0'..'9')).join()

Integer length = 9
String randomString = RandomStringUtils.random(length, charset.toCharArray())(Copied from stackoverflow :D)


  

Thank you very much…works great. Let me try!