Forum Discussion

sriram_sig's avatar
sriram_sig
Contributor
6 years ago
Solved

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 piece code each time i want to a add a global variable.

Project.Variables.AddVariable("Plugin_table","String")
 Project.Variables.Plugin_table = "Testing"

And each time before i begin to run a test i need to make sure i wipe out all the variables before running the test with the below code, but it does not work well. The reason why i try to delete is because it does not let me add the same variable since it already exists due to the previous run as a persistent variable.

varcount = Project.Variables.VariableCount
for i in range (0,varcount):
  if Project.Variables.VariableExists(Project.Variables.GetVariableName(i)):
    Project.Variables.RemoveVariable(Project.Variables.GetVariableName(i))

Has anyone worked with such a scenario before?

  • 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.

     

     

     

8 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    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.

     

     

     

    • sriram_sig's avatar
      sriram_sig
      Contributor

      hi tristaanogre ,

      yes exactly, this is what is going on

       

      After adding variables

       

      Log with index of variables added

       

      Error while trying to remove variables with below code

       

      Log.Message("Variable count -- " + aqConvert.IntToStr(Project.Variables.VariableCount))
        if Project.Variables.VariableCount > 0:
          varcount = Project.Variables.VariableCount
          for i in range (0,varcount):
            if Project.Variables.VariableExists(Project.Variables.GetVariableName(i)):
              Log.Message("index -- " + aqConvert.IntToStr(i) + "---" + Project.Variables.GetVariableName(i))
              Project.Variables.RemoveVariable(Project.Variables.GetVariableName(i))

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        for (i = varcount - 1; i >= 0; i--)

          ...

         

        Does this help?

         

        Alternatively:

        for i in range (0, varcount - 1):
          Project.Variables.RemoveVariable(Project.Variables.GetVariableName(0))

         

    • sriram_sig's avatar
      sriram_sig
      Contributor

      Thanks you, your solution to count down worked perfectly fine

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Why not to check if variable already exists and add it if it does not?

    if (! Project.Variables.VariableExists("Plugin_table"))

      Project.Variables.AddVariable("Plugin_table","String");

    ...

     

    • sriram_sig's avatar
      sriram_sig
      Contributor

      Thank you. Yes i did try this and it worked. But i just thought it would be better if i'm able to remove all those variables at once, instead of repeating the code

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    You say your remove code doesn't work well.  What happens? Do you get an error?  Does something not delete?  Please describe because what you have written looks like it should work.