Modified global variable across test cases?

Hello,

I have a global variable defined with no value. In my test suite collection I have two test suites:

Dynamics suite
-> Generate random string and save it in global variable
-> Change a Dynamics record using the global variable
Portal suite
-> Check the random string appears in the portal

This all works except that when the portal suite runs, the global variable has been reset and the test fails. It’s not acting global in the way I’d expect (as a programmer of 20 years)! How can I have a dynamic value that exists throughout the test execution? I’ve read about passing a value from one test case to another, but I though I could shortcut all that with a global.

Yep, GlobalVariable is a definite misnomer. The taxonomy is wrong and the life-cycle is (if not wrong) questionable: they’re defined as being “project wide” yet they have a runtime life somewhere between a suite and test case.

So, grab that 20 years of experience and define a class which you can import everywhere. Use either a listener or test case to initialize the value and reuse/read/update it anywhere in the suite(s).

If that doesn’t work, write the value out to a file (using java.io.File) and read it back wherever you need it.

Thanks for that. I’m not familiar with java/Groovy. In C# I’d just have a static class, job done. I’m having trouble here!

I’ve got a class with a static string field. If I copy the class into the test case I can instantiate it multiple times and always get the same value back, and it is different each time I run the test case. If I put the class into a package and import it I get “Unresolved compilation problem”. Katalon shows no script errors.

This is my package

package globals
...
...
class globalz
{
  public static String newTelNo = randomString(chars, 20)
  private static String chars = "abcdefghijklmnopqrstuvwxyz0123456789"
  public static String randomString(String chars, int length) 
  {
    ...
    ...
  }
}

My test case is simply:

import globals
println("fred")
g = new globals.globalz()
println("fred2")
println(g.newTelNo)
g = new globals.globalz()
println("fred3")
println(g.newTelNo)

The only clue I have is this error text:

Resource '/C%%Users%<user>%Katalon Studio%<path>%<project>.prj/Keywords/globals/globalz.groovy' does not exist.

The path is invalid as there is no folder <project.prj>, I found the file in:

C:\Users\<user>\Katalon Studio\<project>\Keywords\globals

What am I doing wrong?

You have static vars but you are not importing the class statically:

import static globals.globalz.*

Note: I didn’t dig too deep into your code - I’m under time pressure here. Might be better later…

Oh… you don’t need to say public (if not mentioned, it’s public by default).

Ah yes the static import, I always forget that. Too use to Resharper fixing everything for me. All working now, thanks.

np. Glad you got it working.