HP UFT: applying PageObject pattern

Let us consider how we can apply page object design pattern in terms of HP UFT and VBScript. This article will not describe pattern, why we need or do not need it. To answer this please read a Martin Fowler's article. This article is rather a short example of the Page Object approach.

VBScript has certain limitations when it goes about OOP. It doesn't support inheritance, you can not create a class constructor with parameters, no polymorphism available. Still we can use classes. In the example below we create just one class of Google search page and use a method of created class. Prerequisites: HP UFT objects Browser and Page are added to Object Repository.


Class SearchPage

    Public objSearchPage

    Public Function SearchFor(ByRef searchCriteria)
        Set searchField = objSearchPage.WebEdit("html id:=lst-ib")
        searchField.Set(searchCriteria)
        searchField.Submit()
    End Function

End Class


Set objBrowser = Browser("Google")
Set objPage = objBrowser.Page("Google")

SystemUtil.Run "iexplore.exe", "www.google.com"
objBrowser.Sync

Set clsSearchPage = New SearchPage
Set clsSearchPage.objSearchPage = objPage
clsSearchPage.SearchFor("cheese")

That's it! You create a class which describes methods available on you page. In the provided example we are interested only in one method to search.

As it was mentioned, constructors with parameters are not allowed in VBScript. That is why we are defining class field objSearchPage as 'public' and the later assign value of a class field:


Set clsSearchPage.objSearchPage = objPage

While this example is really short and naive, it can be easily applied to more complicated tests. Page Object design pattern allows you to apply DRy principle and it is recommended in most of UI automation cases.

Comments