Forum Discussion

MrDysprosium's avatar
MrDysprosium
Contributor
7 years ago

How to access the value of a Project Variable programmatically?

Trying to get an idea for how I can interact with Project Variables. Here is a test script I setup:


def TestVars():
  ProjectSuite.Variables.AddVariable("testVar", "Integer")
  ProjectSuite.Variables.VariableByName["testVar"] = 1234
  Log.Error(str(ProjectSuite.Variables.VariableByName("testVar")))

I have a couple of questions.

The above code creates a "Persistent" variable, how do I programmatically create a "Temporary" one?
Secondly, this script fails on the fourth line with the following error.

 

Type Error
`IDispatchIndexedPropertWrapper` object is not callable

 

Any ideas? I want to be able to create and edit and call the value of project variables all within a script.

2 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    When you talk "Persistent" versus "Temporary", are you using the TestComplete monikers where a persistent variable preserves the content of the variable between test runs or is it "Persistent" in that TestComplete creates a variable that is always associated with the project?

     

    As for the error... one of the limitations of Python is calling object properties that take a aprameter... you can't do so without using the _getprop_ method (see http://support.smartbear.com/testcomplete/docs/reference/language/python/getprop-method.html).

     

    However, couple of things about your code:

     

    1) If you want to use a ProjectSuite Variable in your project suite, you don't (and probably shouldn't) add it as part of your automation code.  Consider it the same sort of declaration that you would do if it was a variable in code.  You declare before you use.  So, add your variable to your project suite manually.

    2) If you do #1, the you shouldn't have to worry about "VariableByName" at all.  You should be able to get the variable easily just by going 

     

    ProjectSuite.Variables.testVar