Forum Discussion

sriram_sig's avatar
sriram_sig
Contributor
6 years ago

Working with project variables in Testcomplete

I have a requirement to execute a set-up method before executing all the tests. And this set-up method is supposed to have 10 global variables and intializing those within that method. So adding this...
  • tristaanogre's avatar
    6 years ago

    Actually, I think I might know what's going on with your delete script.

    Every time you delete a variable, the indexes change.  So, let's say you have the following three variables.

     

    Project.Variables.Var1 -> Index 0

    Project.Variables.Var2 -> Index 1

    Project.Variables.Var3 -> Index 2

     

    Now your script runs and you delete the first one (index 0).  The collection now looks like this.

     

    Project.Variables.Var2 -> Index 0

    Project.Variables.Var3 -> Index 1

     

    The next time through the loop, you delete Index 1.  So, now the collection looks like this.

     

    Project.Variables.Var3 -> Index 0

     

    Now your automation is going to try to delete Index 2.... and you'll most likely get an error like "Variable not found" or "Index out of range" or something like that because now there is no variable with index value of 2.

     

    If you're going to do a loop like that, you should do it as a for loop that counts DOWN instead of counting up.  Here's what it would look like in JavaScript... I'm assuming you're using Python so you'll have to adapt.

     

    varcount = Project.Variables.VariableCount
    for (i = varcount; i >= 0; i--){
      if Project.Variables.VariableExists(Project.Variables.GetVariableName(i)){
        Project.Variables.RemoveVariable(Project.Variables.GetVariableName(i))
    }

    See if that helps.