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.
Proceed with the following steps:
WebEdit("username"): Username WebEdit("password"): Password123 WebEdit("email"): [email protected] WebEdit("tel"): (01) 23 456 789
Web_RegFunc.vbs
function library:Function getTOComment(obj) getTOComment = obj.GetTOProperty("miccommentproperty") End Function
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
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.
Refer to the following article at www.advancedqtp.com, which also discusses this topic in depth: