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.