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

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

Hi Udi

1. what are the variables that getTestCaseVariables method gets?

These?

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

1 Like

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?

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…

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 (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).

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’))

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…” :slight_smile:

But you could use GlobalVariables…

1 Like

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

GlobalVariables doesn’t have logic …

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

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.

1 Like

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

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.

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)

}

Thanks
Sam.