Search code examples
vbscriptqtphp-uft

QTP/UFT - Function That Creates WebLink Variables


I am thinking of doing something I am not sure if it is possible - perhaps not the best practice even, but nonetheless want to give it a try.

I have a function library and I have the action in which I do the main scripting, obviously calling functions from the library.

Originally in my action, the code looked something like this:

Code

Browser("Browser").Page("Page").WebElement("Element").Click

I have, however, changed it to this:

Code

Set WebLink = Browser("Browser").Page("Page")

WebLink.WebElement("Element").Click

I did this because i feel that it has a cleaner look with "less" code in each line.

I know that I can code a function to do this:

Public Function myLink(WebLink)

Set WebLink = Browser("Browser").Page("Page")

End Function

Then in my action, I do a call just once at the top:

Code

Call myLink(WebLink)
WebLink.WebElement("Name").HighLight 

However, I was thinking that in some instances, I would have different page names, for example:

Code

Browser("Browser").Page("Page1")
Browser("Browser").Page("Page2")
Browser("Browser").Page("Page3")

So perhaps, creating a function that would store my variables and then I am able to call the variables could be an alternative.

My function could look like this:

Code

Public Function myLinks(WebLink)

    Dim Pages(): Pages = ("Login","CreateUser","SelectOption","DeleteUser",)

    Browser("Browser").Page(Pages())

    For Each Elem In Pages

        Set WebLink = Browser("Browser").Page(Pages())

    End For

End Function

Then in my action, I use it like this:

Code

Call myLinks(WebLink)

Login.WebElement("Element").Click
CreateUser.WebElement("Element").Click
SelectOption.WebElement("Element").Click
DeleteUser.WebElement("Element").Click

I know that what I have is probably illogical but if something like this could perhaps work, I would like to give it a try.


Solution

  • well, if you do not have the Option Explicit enabled at the beginning of your vbs files (which would force you to declare each variable before usage) then you can simply say:

    For Each strPage in Pages()
         ExecuteGlobal "Set " & strPage & " = Browser(""Browser"").Page(""" & strPage & """)"
    Next
    

    This one would create for you Global Variables having as names the Strings in your Pages Array. You do not need any Input Parameter

    P.S: A better name would be InitPageObjects - as what you are trying to achieve has nothing to do with Links