HP UFT VBScript: two ways to supply arguments to your method

We will use existing function SearchFor() from previous post HP UFT page object pattern. But now let's take a look at how another yet way to supply arguments to a function.

Previusly it looked like this:

Public Function SearchFor(ByRef searchCriteria)
     Set searchField = objSearchPage.WebEdit("html id:=lst-ib")
     searchField.Set(searchCriteria)
     searchField.Submit()
End Function
What if you need a function accepting more arguments or different number of arguments? It is possible to create several more functions with different signatures. At the same time we can utilize Dictionary object. Let's prepare a dictionary firstly:

Set d = CreateObject("Scripting.Dictionary")
d.Add "first", "https://play.google.com/store/apps/details?id=com.olyv.wortschatz.ui"   
d.Add "second", "http://wortschatz-olyv.rhcloud.com"
And now a new function accepting a dictionary as an argument:

Public Function SearchFor(ByRef searchCriteriaDict)
     Set searchField = objSearchPage.WebEdit("html id:=lst-ib")
     searchField.Set(searchCriteriaDict("first"))
     searchField.Set(searchCriteriaDict("second"))
     searchField.Submit()
End Function
As you can see, it is now encapsulating the same logic and it is more flexible since you can supply different number of arguments. On the other hand you need to use this approach responsibly because it is really easy to mess up with dictionary keys. 

Comments