How to define a global variable on the fly and access tit through Different suites in the Collection?


#1

I am creating a Global variable while execution is in progress in 1 Suite, and I am trying it to access it in next Suite that is getting executed through Testsuite-collection. But this is not happening.

The code I am trying to create Global variable is as below :
@Keyword
void addGlobalVariable(String name, def value) {
GroovyShell shell1 = new GroovyShell()
MetaClass mc = shell1.evaluate(“internal.GlobalVariable”).metaClass
String getterName = “get” + name.capitalize()
mc.‘static’."$getterName" = { -> return value }
mc.‘static’."$name" = value
}


#2

The instance of internal.GlobalVariable is scoped per each execution of single TestSuite.This instance is newly created and destroyed per each TestSuite run.

Therefore, even if your TestSuite1 created a property in the instance of internal.GlobalVariable class, it will be destroyed before TestSuite2 runs. The instance of internal.GlobalVariable in TestSuite2 will be newly created and will not have the property that you want to see.


If I were you, I would create a JSON file somewhere in the project as a temporary storage for values to pass around from TestSuite1 to TestSuite2. TestSuite1 writes values into the file, TestSuite2 reads values from the file. That’s all. No Groovy Metaprogramming required.


#3

Thanks for the reply.
I did the same work around as per you suggestion.
Thanks for the help.