# Test listeners: What does testCaseContext.getTestCaseVariables() gets, is there a set method as wel?

**URL:** <https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [April 23, 2018, 1:11pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455 "2018-04-23T13:11:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![udi](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@udi](https://forum.katalon.com/u/udi)\
**Post date:** [April 23, 2018, 1:11pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/1 "2018-04-23T13:11:33Z")

</div>

Hi,

i want to a add a test listener to my code before the test case starts that will check the test case variables, and set them according to different scenarios.

an example will be sign in:  
one sign in test case with 2 variables: userName and password.  
run the test \> test listener opens see the values of userName and password and changes the values according to the values the the original userName and password has.

for getting the test case vars i wanted to use:  
testCaseContext.getTestCaseVariables()

tried this and i always got an empty map from this method.

and to change them i wanted to use:  
testCaseContext.setTestCaseVariables()

saw this method in this jar com.kms.katalon.core\_1.0.0.201804160633.jar

// Method descriptor #50 (Ljava/util/Map;)V

// Signature: (Ljava/util/Map\<Ljava/lang/String;Ljava/lang/Object;\>;)V

// Stack: 2, Locals: 2

public void setTestCaseVariables(java.util.Map testCaseVariables);

but katalon doesn’t recognize it…

so 2 basic questions:  
1. what are the variables that getTestCaseVariables method gets?  
2. is it possible to change their values and send them back to the test case?

Thanks,  
udi

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [April 23, 2018, 1:18pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/2 "2018-04-23T13:18:26Z")

</div>

Hi Udi

> 1. what are the variables that getTestCaseVariables method gets?

These?

![](https://s3.amazonaws.com/katalon-forum/editor/kg/5tq8oy6niw8i.png "Image: https://s3.amazonaws.com/katalon-forum/editor/kg/5tq8oy6niw8i.png")

> 2. is it possible to change their values and send them back to the test case?

Once you have them, you can modify them. I don;t use these guys right now but I do recall trying them when I first played around with KS. They work like GlobalVariables: the values you supply in the UI become their starting values; any modifications you make in your test cases are applied just for that test run “in memory” (i.e. the values in the KS UI are “constant” and will remain).

Russ

[image.png](https://s3.amazonaws.com/katalon-forum/editor/kg/5tq8oy6niw8i.png)

---

<div class="post-metadata">

**Author:** ![udi](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@udi](https://forum.katalon.com/u/udi)\
**Post date:** [April 23, 2018, 1:44pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/3 "2018-04-23T13:44:42Z")

</div>

Thanks for the info Russ!  
didn’t know about the local variables! now i get what does getTestCaseVariables() does.

i’m still not sure how to change them within the test listener…  
any ideas?

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [April 23, 2018, 3:33pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/4 "2018-04-23T15:33:56Z")

</div>

If I remember correctly…

Let’s say you have a variable “my\_variable” set to “5” defined in the variables view. When the Test Case is running you can…

```auto
WebUI.comment("My Variable is: " + my_variable)

my_variable = "something else"

WebUI.comment("My Variable is: " + my_variable)

```

Hope that helps.

More here: [https://docs.katalon.com/display/KD/Variable+Types](https://docs.katalon.com/display/KD/Variable+Types) (although, I think that page could do with a little editing – there is much confused talk about “public” “private” “global” “local”… it’s messy and needs a rewrite).

---

<div class="post-metadata">

**Author:** ![udi](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@udi](https://forum.katalon.com/u/udi)\
**Post date:** [April 24, 2018, 9:26am UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/5 "2018-04-24T09:26:23Z")

</div>

Thanks for the reply Russ!

to change local variables your suggestion will work fine.

my questions is how to change them from a test listener.  
i can get the map with my variables, but i want to change their values in the the test listener before the test case runs, and send them back to the test case.

is it possible?

here is what i want to achieve (this code will not work):

Test listener:

@BeforeTestCase

def sampleBeforeTestCase(TestCaseContext testCaseContext)

{

Map\<String, Object\> map = testCaseContext.getTestCaseVariables()

String userName = map.get(“user”)

String pasword = map.get(“pass”)

switch(userName)

{

case “all”:

userName = "alluser@email.com"

pasword = “allPassowrd”

}

Map\<String, Object\> mapResult = new HashMap(String, Object)

mapResult.put(“user”, userName)

mapResult.put(“pass”, pasword)

testCaseContext.setTestCaseVariables(map)

println testCaseContext.getTestCaseId()

println testCaseContext.getTestCaseVariables()

}

Test Case:

\*imports…

WebUI.openBrowser(’’) // open browser and navigte

WebUI.maximizeWindow()

WebUI.navigateToUrl(‘url’)

//get user name and password

String user = user

String pass = pass

WebUI.setText(findTestObject(‘Sign in page/input\_identity’),user )

WebUI.setText(findTestObject(‘Sign in page/input\_password’), pass )

‘Sign in to ios only account’

WebUI.click(findTestObject(‘Sign in page/button\_Sign in’))

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [April 24, 2018, 12:02pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/6 "2018-04-24T12:02:50Z")

</div>

I don’t think so. I don’t think the TLs run in the context of the test, I guess the clue is in the name “before…” and “after…” 🙂

But you could use GlobalVariables…

---

<div class="post-metadata">

**Author:** ![udi](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@udi](https://forum.katalon.com/u/udi)\
**Post date:** [April 24, 2018, 2:07pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/7 "2018-04-24T14:07:08Z")

</div>

GlobalVariables doesn’t have logic so that won’t server my purpose.  
But thank you Russ.

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [April 24, 2018, 3:14pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/8 "2018-04-24T15:14:49Z")

</div>

> GlobalVariables doesn’t have logic …

You lost me there. They’re variables - does any variable have logic?

---

<div class="post-metadata">

**Author:** ![udi](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@udi](https://forum.katalon.com/u/udi)\
**Post date:** [April 29, 2018, 10:05am UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/9 "2018-04-29T10:05:10Z")

</div>

by logic I meant that they don’t have a way to change them externally which is what i wanted to use the test listeners for.

---

<div class="post-metadata">

**Author:** ![Alexander\_Shchetinin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_Shchetinin](https://forum.katalon.com/u/Alexander_Shchetinin)\
**Post date:** [August 17, 2018, 9:14pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/10 "2018-08-17T21:14:14Z")

</div>

Its not really clear why would you want to fetch the TC variables with listener. If you can’t initialize them, then what for?

---

<div class="post-metadata">

**Author:** ![technical4vihari](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@technical4vihari](https://forum.katalon.com/u/technical4vihari)\
**Post date:** [September 17, 2019, 8:19am UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/11 "2019-09-17T08:19:06Z")

</div>

Hi udi,  
I am trying to do the same. Did you get an answer.  
From the latest version of Katalon, it is adding a prefix and a suffix of character ’ if the value passed from the excel has any quote.  
Ex: if i put “Scondary Girl’s School” in excel. Katalon is adding quotes for this text when i try to retrieve it in the test case like “‘Scondary Girl’s School’”.  
So either i am looking for a fix where katalon does not add these quotes. If not, i want to iterate through all the variables in test case and write a code to remove the quotes in listener and send the modified values to the actual test case.

---

<div class="post-metadata">

**Author:** ![smlpreetham08](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/smlpreetham08/32/16103_2.png) [@smlpreetham08](https://forum.katalon.com/u/smlpreetham08)\
**Post date:** [February 21, 2022, 1:01pm UTC](https://forum.katalon.com/t/test-listeners-what-does-testcasecontext-gettestcasevariables-gets-is-there-a-set-method-as-wel/10455/12 "2022-02-21T13:01:56Z")

</div>

I have used the below code to set the value of file name, which is unique for each test case and it is given as variable in each test case. But still when I am trying to get its value in the BeforeTestCase listener. The value is returned as null. How should I call the value of the variable in before test case listener.  
@BeforeTestCase  
def sampleBeforeTestCase(TestCaseContext testCaseContext) {  
println testCaseContext.getTestCaseId()  
println testCaseContext.getTestCaseVariables()  
Map\<String, Object\> map = testCaseContext.getTestCaseVariables()  
String fileName = map.get(“fileName”)  
helper.webDeploy(‘samautomationtest’,fileName)

```
}

```

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/5/a/5ad5eafdbd2fb9075c22f297e37c28685e1db20e.png)

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/6/0/60952fd330e3523503bde0e7ab69693303fb0de5.png)

Thanks  
Sam.
