Aqid
Katalon Apprentice
06/03/2017

Using Global Variables on Test Cases

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?

Upvote
Quote

Comments

  • Vinh Nguyen
    Katalon Moderator
    06/04/2017
    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'))

     
    Upvote
    Quote
  • Aqid
    Katalon Apprentice
    06/04/2017
    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?
    Upvote
    Quote
  • Vinh Nguyen
    Katalon Moderator
    06/04/2017
    You can create a Map and put your variables in there, then use it in your 'return' statement.
    Upvote
    Quote
  • Harinath
    Katalon Apprentice
    03/29/2018
    How to assign the project location directory path to the GlobalVariable? Please suggest any idea

    Upvote
    Quote
  • Harinath
    Katalon Apprentice
    03/29/2018
    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.
    Upvote
    Quote
  • Marek Melocik
    Katalon Expert
    03/29/2018
    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




    Upvote
    Quote
  • Stephane Blais
    Katalon Apprentice
    04/16/2018
    Using V5.4.0, I am not able anymore to access my old global variables.  How to access them using V5.4.0?
    Upvote
    Quote
  • Hadyn
    Katalon Apprentice
    04/17/2018
    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
    Upvote
    Quote
  • Stephane Blais
    Katalon Apprentice
    04/17/2018
    Ok, no more problem, found them: 
    Upvote
    Quote
Sign In or Register to comment.