Using Global Variables on Test Cases


#1

Hi,

I’m wondering if someone has the same problem with me. Currently I’m able to run Katalon WebService Tests to some extent and now I’m trying to use GlobalVariable.

 

My code is more or less like this

 

def loginResponse = WS.sendRequest(findTestObject(‘Login’))

def loginObject = slurper.parseText(loginResponse.responseBodyContent)

println loginResponse.responseBodyContent

def token = loginObject.data.token

GlobalVariable.token = token

 

The thing is, after setting Global Variable in Login Test Case and I want to run Logout test case using this code

RequestObject logout = findTestObject(‘Logout’)

TestObjectProperty authHeader = new TestObjectProperty()

authHeader.setName(‘Authorization’)

authHeader.setValue('Bearer ’ + GlobalVariable.token)

println GlobalVariable.token

 

GlobalVariable.token is empty

But I can run it if I include both in a Test Suites. Is there any way that I can use global variable accross single Test Cases without using Test Suites?


#2

so currently there’s no way to set global variable and use it on another test case? because I need to use several variables at once and it seems that I cant create Java class to contain those variables. Perhaps I’ll use Map then?

what do you think?


#3

You can create a Map and put your variables in there, then use it in your ‘return’ statement.


#4

You can also return the variable in one test case and then used in other test case, e.g

TC1:

String yourValue = "ABCDEF"

return yourValue

TC2:

yourValue = WebUI.callTestCase(findTestCase('TC1'))


#5

How to assign the project location directory path to the GlobalVariable? Please suggest any idea


#6

Harinath said:

How to assign the project location directory path to the GlobalVariable? Please suggest any idea

I’ve tried with various things as

  1. System.getProperty(‘user.dir’)+’/path/file.txt’
  2. /path/file.txt
  3. %cd%/path/file.txt
    But i didn’t get the expected result.

#7

You can get your project directory by executing:

RunConfiguration.getProjectDir()

Unfortunately, there is no option like “Method” for GlobalVariable, system converts the method above to string.

You can create your own class with variables in Custom Keywords.

public class MyVariables {
    public static String projectDir = RunConfiguration.getProjectDir()
}

// And then, in your test case simple write:

...
MyVariables.projectDir
...

// OR, use RunConfiguration.getProjectDir() directly in your test to get current project directory

#8

Using V5.4.0, I am not able anymore to access my old global variables. How to access them using V5.4.0?


#9

Stephane Blais said:

Using V5.4.0, I am not able anymore to access my old global variables. How to access them using V5.4.0?

Me too


#10

Ok, no more problem, found them:

Global Variables.JPG