Forum Discussion

falcron's avatar
falcron
Occasional Contributor
15 years ago

Referencing an object via a project variable

How is this done?



Currently, the following doesn't appear to be working for me:





Project.Variables.AddVariable "KittBar", "String"

If Project.Variables.envBrowser = "IEXPLORE" then

  Project.Variables.KittBar = "Aliases.IEXPLORE.pageHome.panelContainer.panelContent.panelBgComputers.panelProfileRow.panelSettingsRowSmall.imageKittBarGif"

End If

If Project.Variables.envBrowser = "firefox" then

  Project.Variables.KittBar =
"Aliases.firefox.pageHome.panelContainer.panelContent.panelBgComputers.panelProfileRow.panelSettingsRowSmall.imageKittBarGif"

End If

Set objKitt = Project.Variables.KittBar

While objKitt.Exists = TRUE

  Call Delay(5000)

Wend





I also tried:





  Set KittBar = Project.Variables.envKittBar   



  While aqObject.CallMethod(KittBar, "Exists") = True

    Call Delay(5000)

  WEnd





I feel like I'm making a dumb coding mistake, but is this possible to do in TestComplete?  Where is my error here?  I'm assuming it's because I'm seting the object as a string, but given the options given to me of the variables, I'm not sure how to set this as an Object. 



In a nutshell I want to be able to store an object name in a variable and reference it later.

5 Replies

  • Hi,



    Use Eval to obtain the target object by its name stored in a variable.

    Set objKitt = Eval(Project.Variables.KittBar)

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Probably an easier way to do it rather than using Eval or complicated "if/then" logic, is for you to edit the NameMapping node for your browser process and set the browser name from a Constant/String to a Variable (see screenshot)



    I would then rename your browser name mapping node and the corresponding Alias node from the specific "firefox" and "iexplore" to simply "WebBrowser".



    Any calls then to your browser would simply be



    Project.Variables.envBrowser = "iexplore"

    while Aliases.WebBrowser.Exists

        Deley(500)

    WEnd




    You can do the same for other name mapping nodes, components, and their properties.  See http://smartbear.com/support/viewarticle/12445/ for more info.
  • vex's avatar
    vex
    Contributor
    Ok, so..  first off, THIS IS WONDERFUL.  :)



    It works like a charm and I had no idea you could do that!  I would suggest to SmartBear if there already isn't, a document/support doc guiding how you can set up your testing to be portable across different OSes and/or browsers within one test would be great.  Some of those tips would be awesome.



    That made portability between IE and FF trivial.  Thanks so much for the Eval tip and the NameMapping trick guys!



    I do have one final snag I'm trying to figure out.



    My process of my app test is:



    1. Launch testedapp (launches in browser)

    2. Create a user account

    3. Start Download of Program

    4. Close browser

    5. Run install of program

    6. At the end of the install of program, the install program itself will automatically launch the default browser.



    #6 is obviously the snag -- is there an easy way to set the default web browser through a script?  As it is now if I want to test IE, I have to manually set the system default browser to IE.



    Cheers.
  • Hi,



    You can use Robert's suggestion together with WaitAliasChild to get the launched browser:

    ...

    ' Step #5 ends here

    Project.Variables.envBrowser = "iexplore"

    Set browser = Aliases.WaitAliasChild("WebBrowser", 10000)

    If Not browser.Exists Then

    Project.Variables.envBrowser = "firefox"

    Set browser = Aliases.WaitAliasChild("WebBrowser", 10000)

    If Not browser.Exists Then ' Neither IE nor FF found. Exit your function with an error

    End If



    ' Code which works with browser

    ...




    Also, you can get the path to the default browser from the HKEY_CLASSES_ROOT\http\shell\open\command registry key (see and example of getting a registry value here).




  • vex's avatar
    vex
    Contributor
    Thats what I need - the reg key.  I can't use TC to launch the browser because the install of the program I'm testing launches the default browser automatically.



    Thanks for heads up though, I'll try that reg key.