RegisterUserFunc in QTP automation testing : Enables you to add new methods to test objects or change the behavior of an existing test object method during a run session.
When you use this statement, QuickTest uses your user-defined function as a method of a specified test object class for the remainder of a run session, or until you unregister the method.
If the specified method name does not already exist for the test object, it becomes a new method for the object. If the method name is a defined QuickTest method for the object, your definition (temporarily) overrides the existing functionality of the specified method.
Let’s suppose we have a function called GetProperty, which we would like to register to all objects available to us. Now, if we do it manually, it can be quite time-consuming since any sort of AUT has many objects, and if you’re working in an environment where multiple technologies are involved, this can even get a little boring and make our library hard to maintain. Now, though, there is a simple way of register all our objects to the function by simply calling the RegisterUserFuncX method.
Before we see how the method RegisterUserFuncX works, let’s create a function GetProperty function, which we will first manually register to the objects in our multi-technology (Standard Windows, Web, Siebel) AUT:
Function GetProperty(Object) GetProperty = Object.GetTOProperty("micclass") End Function |
'Web RegisterUserFunc "Link", "GetProperty", "GetProperty" RegisterUserFunc "WebEdit", "GetProperty", "GetProperty" 'Standard Windows RegisterUserFunc "WinEditor", "GetProperty", "GetProperty" 'Siebel RegisterUserFunc "SiebList", "GetProperty", "GetProperty" |
Now, this is just an example. There are obviously many more objects I would have to register the function with. However, instead of doing this manually, I can simply use the below method and register any given function to all objects automatically:
Function RegisterUserFuncX(TOMethodName, FunctionName) Dim oRegistry, arrSubKeys, sSubKey, sKeyPath Const HKEY_LOCAL_MACHINE = &H80000002 sKeyPath = "SOFTWARE\MERCURY INTERACTIVE\QuickTest Professional\MicTest\Test Objects" Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\." &_ "\root\default:StdRegProv") oRegistry.EnumKey HKEY_LOCAL_MACHINE, sKeyPath, arrSubKeys For Each sSubKey In arrSubKeys RegisterUserFunc sSubKey, TOMethodName, FunctionName Next Set oRegistry = Nothing End Function |
If we have to associate all objects in our AUT to the custom function, we would just call the above method once to do our job:
Call RegisterUserFuncX("GetProperty", "GetProperty") |
This is the equivalent of registering the GetProperty method to all objects manually – which requires writing many individual RegisterUserFunc statements. The concept behind this method is that in the following folder of our Registry (regedit), we have all the objects stored:
HKEY_LOCAL_MACHINE\SOFTWARE\MERCURY INTERACTIVE\QuickTest Professional\MicTest\Test Objects
Therefore, instead of registering single object classes to our function, we can use the method RegisterUserFuncX and register any given function to all objects at once. The above function can also be tweaked to only register Web Objects, or Windows, or objects of an environment of your choice. This would involve manipulating the For..Each loop a bit:
'Register all the Web Objects For Each sSubKey In arrSubKeys If InStr(1, sSubKey, "Web") > 0 Then RegisterUserFunc sSubKey, TOMethodName, FunctionName End If Next |
Unregistering a Registered function
RegisterUserFunc sSubKey, TOMethodName

No comments:
Post a Comment