Search code examples
hp-uft

Using one variable for multiple items data in descriptive programming


I know that with Descriptive programming you can do something like this:

Browser("StackOverflow").Page("StackOverflow").Link("text:=Go To Next Page ", "html tag:=A").Click

But is it possible to create some kind of string so I can assign more than one data value and pass it as single variable? I've tried many combinations using escape characters and I always get error.

For example in the case above, let's say I have more properties in the Page object, so I'd normally have to do something like this:

Browser("StackOverflow").Page("name:=StackOverflow", "html id:=PageID")...etc...

But I'd like to pass "name:=StackOverflow", "html id:=PageID" as a single variable, so when writing many objects I'd only have to write:

Browser(BrowserString).Page(PageString).WebEdit("name:=asdfgh")

And the first part would remain static, so if the parents' data needs to be modified I'd only have to modify two variables and not all the objects created in all libraries.

Is it possible?

If I was not clear enough please let me know.

Thank you in advance!


Solution

  • I think what you're looking for is UFT's Description object

    This allows you finer grained control on the description since in descriptive programming all values are regular expressions but with Description you can turn the regular expression functionality off for a specific property.

    Set desc = Description.Create()
    desc("html tag").Value = "A"
    desc("innertext").Value = "More information..."
    desc("innertext").RegularExpression = False
    
    Browser("Example Domain").Navigate "www.example.com"
    Browser("Example Domain").Page("Example Domain").WebElement(desc).Click
    

    If you want to represent this with plain string then it's a bit more of a problem, you can write a helper function but I'm not sure I would recommend it.

    Function Desc(descString)
        Set ret = Description.Create()
        values = Split(descString, "::")
        For Each value In values
            keyVal = Split(value, ":=")
            ret(keyVal(0)).Value = keyVal(1)
        Next
        Set Desc = ret
    End Function
    
    ' Usage
    Browser("StackOverflow").Page("StackOverflow").WebElement(Desc("html tag:=H2::innertext:=some text")).Click
    

    Further reading about descriptive programming.