Forum Discussion

tc_2018's avatar
tc_2018
Contributor
7 years ago

How to declare controls and objects globally?

Hi,

I am trying to find a way to declare controls and objects globally.

Currently, I have to repeat buttons, text boxes, and etc... in every script file as shown below:

 

def test1():

  login_window = Sys.Process("my_app").VBObject("login") #repeat in every script file

  user_name_box = login_window.VBObject("txtUserName") #repeat in every script file 

  user_name_box.SetText("test")

 

Can anyone show me a way to declare those lines below globally once so I can call user_name_box in any script files?

login_window = Sys.Process("my_app").VBObject("login") #repeat in every script file

user_name_box = login_window.VBObject("txtUserName") #repeat in every script file

 

Thanks.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    As with any coding environment, if you want a variable to be accessible to all code, you need to declare it at a higher scope than where you are calling it and then make sure that you reference that scope.

     

    Probably the EASIEST way to do this in TestComplete is to use Project Variables.  Define a project variable for the components you want and then reference Project.Variables.<variable name>

     

    For Python, you can declare all your variables globally in one unit of code and then reference that unit in all your other units.  See https://support.smartbear.com/testcomplete/docs/scripting/calling-routines/declared-in-another-unit/python.html

     

     A third alternative: Learn to use NameMapping and Aliases.  With Aliases, you could easily just build an alias for that login that would be referenced as Aliases.user_name_box and you can then use that EVERYWHERE.  See https://support.smartbear.com/testcomplete/docs/testing-with/object-identification/name-mapping/overview.html#about-the-mapped-objects-and-aliases-trees and related topics.

    • tc_2018's avatar
      tc_2018
      Contributor

      I try to declare variable in class and call it from a script file as shown below:

       

      In class_objects.py file:

      Class MyObjects:

        user_name_box = Sys.Process("myapp").VBObject("login").VBObject("txtUserName")

      In my script file:

      import class_objects

      def test():

        mo = class_objects.MyObjects()

        mo.user_name_box.SetText("test")

       

      And it works.

       

      Do you have any input for doing this way? Thanks.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        See the second option I mentioned above.