Changing the value of a project variable through scripting
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Changing the value of a project variable through scripting
Hello
I'd like to change the value of a project variable through a script that has already been set as some value at some time at the beginning of execution.
So at the beginning of my python script i do something like this:
Project.Variable.Obj1 = Some Object
...
But then later on down the execution of my keyword test I want to reassign that project variable object to a new one through a method like so, where "projVarObj" would be the "reference" to the project variable "Obj1":
def foo(projVarObj, propNames=[], propValues=[])):
...
projVarObj = MainWindow.Find(propNames, propValues, 1000, True)
I have tried this but when I debug I see that the projVarObj is None.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You have assigned the following project variable,
Project.Variable.Obj1 = Some Object
Then somewhere below, you want to assign Project.Variable.Obj1 to something else, using
def foo(projVarObj, propNames=[], propValues=[])):
projVarObj = MainWindow.Find(propNames, propValues, 1000, True)
projVarObj is different to Project.Variable.Obj1. If projVarObj is None, then it means the Find method was not able to find the object based on the criteria given for propNames and propValues.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I meant that projVarObj as a parameter is None. Even though I have already assigned it some value earlier in the execution. The .Find method was just something ill be doing with the project variable.
So what I have:
Project.Variable.Obj1 = Some object
but later on down the road when I don't want to type out Project.Variable.SomeObject in its entirety i want to make a method that generalizes the changing of one of my project variables through the method foo I wrote up there, I could have Proj.Variable.Obj1 through Obj5 and i want to change them this way for simplification purposes.
foo(Project.Variable.Obj1, PropNames[1,2,3], PropValues[1,2,3])
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, I still don't quite understand.
Are you able to upload your complete code?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Another way of asking my question is if it is possible to change a project variable's contents that does not require me to explicitly write out
"Project.Variable.Obj1 = some object"
over and over again.
Instead can I send Project.Variable.Obj1 as an argument to a method I made to change it that way?
If so, when I tried to do this, the project variable i passed did not get changed in its calling method. Why's that?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ah ok.
Since you're using a project variable, you can just do this
Project.Variables.Var1 = "Hello World"
def update():
Log.Message(Project.Variables.Var1)
Project.Variables.Var1 = "Goodbye World"
def main():
update()
Log.Message(Project.Variables.Var1)
If you run main() the following result is shown
See https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference for more information.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@ianlop :
Hi,
Your code looks correct to me, so I guess there is something Python-specific here...
Check if __setprop__ method solves the problem (https://support.smartbear.com/testcomplete/docs/reference/language/python/index.html)
/Alex [Community Champion]
____
[Community Champions] 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. Posts made by [Community Champions]
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.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
