How can we retrieve a variable value or object for the current test case?

Hello,

We have a use case where we must save objects and variables to a test case during execution. We need to retrieve the value during execution within the custom keywords we have developed. The variable will be used in each of our modules and steps so we do not want to continuously pass it in to the custom keyword.

For example setting debug output to a level 1-4 will output different information to the console and logs. The test case variable would be named something like tcDebugLevel = 2. We want retrieve the value within our methods.

Could someone point in the direction of where I can find this information?

Thank you,
Paul

Hi Paul

If I’m understanding your question correctly…

I would create a new class (again using the same method you used to create your keywords) and import that class statically everywhere you need it.

Use the same package you used to create your other keywords but call the class something like my_globals or my_vars

package my_package

import whatever

public class my_globals {

  static int debugLevel = 999  // set a preferred default here

  // Other stuff you want available here
}

Then, in your existing keyword class file:

import static my_package.my_globals.*

void myKeyword() {
  WebUI.comment("Debug Level is: " + debugLevel)
  // ...
}  

And the same in your TestCase scripts:

import static my_package.my_globals.*
...

// Test Case code

WebUI.comment("Debug Level is: " + debugLevel)

// Change the debug level

debugLevel = 42

WebUI.comment("Debug Level is: " + debugLevel)

Hi Russ,

Thank you for the information on using a solution that is similar to global variables. I was hoping to find a solution that allowed each test case and test case instance to pull values assigned to the individual instance. I know we can pass in the values from the test suite execution to each test case and I had hoped to retrieve the individual test case value.

For example:
TestSuite1 TestCase Login debug = 999
TestSuite1 TestCase Add Record debug = 1
TestSuite2 TestCase Login debug = 1
TestSuite2 TestCase Login and Search debug = 2

As a test case variable the value is localized to the test case instance. Using a global or static solution would not allow the single instance of Katalon to update or have a unique value that we need.

Any other suggestions on a call to the testcase variable to return the value?

Thank you,
Paul

Hey Paul

I’m really not sure I’m following this…

Let me ask you… if the “instance” vars were saved externally to the whole system, would that cover the case? I’m thinking…

  1. Some code runs (perhaps in a keyword or some preparatory TC) that writes values to a file (JSON, perhaps).

  2. Some other code runs that reads those values for use elsewhere.

Make sense?

I do this. Works.

But, caveat, if you expect to be running parallel tests on the same system, you’re going to be treading on your own toes “one day”. The moment locking code is introduced, your technical debt just magnified enormously.

Let me know what you think…

Hi Russ,

Your point of running parallel test and stepping on toes is well taken. That was the reason why I was hoping like UFT we could read the test case instance variable value. I could then contain each test cases instance value within the scope of the test case. Otherwise the management becomes difficult at best.

I am surprised this has not been requested before. I found methods to call and pull info from the test case but I just have not found a way to return a variable value. That would be the optimal solution unless I create multiple instances of the test suites and data drive at that level.

Thank you,
Paul

Do you mean from a TC? You can do that. callTestCase can return a result (which could be an array/list, map… whatever). Does that help?

https://docs.katalon.com/katalon-studio/docs/common-call-test-case.html

Hi Russ,

I thought I might have found a solution but I have not found the documentation for the TestCaseFactory. I found the following call which looks promising: TestCaseFactory.VARIABLE_DEFAULTVALUE_PROPERTY. I assume I pass the variable name and return the value that is currently present.

The idea in doing this is to reduce the number of parameters we have to pass into each of our keywords.

Is the TestCaseFactory a solution to read values?

Thank you,
Paul