Ask a Question

Changing the value of a project variable through scripting

ianlop
Occasional Contributor

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.

 

6 REPLIES 6
rraghvani
Champion Level 2

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.

ianlop
Occasional Contributor

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

rraghvani
Champion Level 2

Sorry, I still don't quite understand.

 

Are you able to upload your complete code? 

ianlop
Occasional Contributor

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?

 

ianlop_0-1681748962020.png

 

rraghvani
Champion Level 2

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

rraghvani_0-1681750476719.png

 

See https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference for more information.

 

AlexKaras
Champion Level 2

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

 

Regards,
  /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
================================
cancel
Showing results for 
Search instead for 
Did you mean: