How to get TestCase Variable BEFORE callTestCase

Running a test case via callTestCase I don’t have a testCaseContext (unless I’m missing something?). So in a Keyword class method, I don’t see how to get a Test Case variable.

I can see that a TestCase object has a getVariables method, but how to retrieve my_var? Do I need to write an iterator to find it? Or is there a way to form a testCaseContext from a TestCase object?

I’m sure I’m missing something…

textCaseContext, if i remember it right, is scoped to suite, (not 100%sure about, long time since i was looking into docs) so I don’t think will suit to your needs.
if you need to extract some vars from a testcase and pass to other, whitout using globals, a ‘return’ at the end of the called testcase can work.

No, it’s scoped to test case:

beforeTestSuite(TestSuiteContext testSuiteContext) { ...

beforeTestCase(TestCaseContext testCaseContext) { ...

No, and I now realize I could have stated the problem better - I need the context before the testcase is actually called. The code is something like this:

  static void runTC(String tcId, String pageClass = "") {
    runEmbeddedTC(tcId, "before-tc")

    // beginTC needs the testCaseContext 
    beginTC(tcId)

    TestCase tc = findTestCase(tcId)

    try {
      WebUI.callTestCase(tc, null)
    } catch(Exception e) {
      // report the error
    }

    runEmbeddedTC(tcId, "after-tc")
  }

tc has a getVariables method but it doesn’t return them as a map like testContext.getTestCaseVariables… it returns a List<Variable> type, which I think can be iterated with .each - which I think is what I am going to need to write.

The TestCase API should have this help built in but I can’t see it anywhere.

https://api-docs.katalon.com/index.html

Yes, the iterator is required (.each can’t be stopped so I’m using a for loop)

  String varValue
  def tc = findTestCase(tcId)
  def vars = tc.getVariables() 
  for(int i = 0; i < vars.size(); i++) {
    if(vars[i].name == "append_url") {
      varValue = vars[i].defaultValue
      break
    }
  }
      // Now we have varValue

@devalex88 Any wisdom you can add here?

@Russ_Thomas got it,now i understand what do you want to achieve.
I think instead of for,since you need just a particular variable, find() will be a more elegant approach, altough i haven’t played with it too much. not sure if this method is available also for lists or only for maps.
sorry,still in holiday,far away from pc so i cannot post a more detailed solution.
see https://www.tutorialspoint.com/groovy/groovy_find.htm

1 Like