cancel
Showing results for 
Search instead for 
Did you mean: 

Working with project variables in Testcomplete

SOLVED
Contributor

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Working with project variables in Testcomplete

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.

 

 

 


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
8 REPLIES 8
Community Hero

Re: Working with project variables in Testcomplete

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");

...

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Community Hero

Re: Working with project variables in Testcomplete

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.


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Contributor

Re: Working with project variables in Testcomplete

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

Community Hero

Re: Working with project variables in Testcomplete

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.

 

 

 


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Contributor

Re: Working with project variables in Testcomplete

hi tristaanogre ,

yes exactly, this is what is going on

 

After adding variables

 

Variables_list.PNG

Log with index of variables added

 

Variables_index.PNG

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))

After_removal.PNGError_log.PNG

Community Hero

Re: Working with project variables in Testcomplete

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))

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Contributor

Re: Working with project variables in Testcomplete

Thank you, i had to use the 3rd argument to make it work in python

for i in range (varcount-1,-1,-1):

Highlighted
Contributor

Re: Working with project variables in Testcomplete

Thanks you, your solution to count down worked perfectly fine

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors