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?
Solved! Go to Solution.
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.
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");
...
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.
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
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.
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))
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))
Thank you, i had to use the 3rd argument to make it work in python
for i in range (varcount-1,-1,-1):
Thanks you, your solution to count down worked perfectly fine
Subject | Author | Latest Post |
---|---|---|