Adding a new method to a class

You can also override an existing TO method and we could actually extend a TO class with new methods. Such custom methods will be registered in the same fashion as those which override the existing ones. For example, if you wish to retrieve the comment property of a TO, use the following code:

Print "username: " & Browser("AdvancedQTP - Overriding").Page("AdvancedQTP - Overriding").WebEdit("username").GetTOProperty("miccommentproperty")

Instead of writing this every time, the best practice would be to create a new method, which passes in the properties of the TO and returns the retrieved value.

How to do it...

Proceed with the following steps:

  1. Open Object Repository in which we previously added the objects from our HTML sample page. Enter the following values (or whichever you choose) to their respective comment fields in the Properties pane:
    WebEdit("username"): Username
    WebEdit("password"): Password123
    WebEdit("email"): [email protected]
    WebEdit("tel"): (01) 23 456 789
  2. We will write the following custom method in our Web_RegFunc.vbs function library:
    Function getTOComment(obj)
        getTOComment = obj.GetTOProperty("miccommentproperty")    
    End Function
  3. In our test, we will write the following code:
    RegisterUserFunc "WebEdit", "Comment", "getTOComment"
    
    with Browser("AdvancedQTP - Overriding").Page("AdvancedQTP - Overriding")
        Print "username: " & .WebEdit("username").Comment
        Print "password: " & .WebEdit("password").Comment
        Print "email: " &.WebEdit("email").Comment
        Print "tel: " &.WebEdit("tel").Comment
    End with
    
    UnregisterUserFunc "WebEdit", "Comment"

The print log in the Output pane will show the values as we set them in Object Repository:

username: Username

password: Password123

email: [email protected]

tel: (01) 23 456 789

How it works...

To add a new method or property, we simply map (register) it to a non-existing method, in this case, Comment. UFT does not check whether the native TO method exists or not.

The custom function simply retrieves the value of miccommentproperty (an undocumented property of TOs), and returns it to the calling function or Action.

During runtime, we call the method or property by the name we decided, in this case, Comment, and UFT redirects the call to our getTOComment function.

See also

Refer to the following article at www.advancedqtp.com, which also discusses this topic in depth:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset