我们在编写QTP的自定义函数时通常用RegisterUserFunc来注册到指定的测试对象,但是如果碰到需要注册到很多测试对象的情况,则需要一条条注册,比较麻烦。Anshoo Arora给我们介绍了一个批量注册的方法:
http://relevantcodes.com/registeruserfuncx-register-methods-to-all-qtp-objects-automatically/
这种方法的关键之处在于利用了QTP在注册表中的信息:
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
取出所有注册的测试对象后调用RegisterUserFunc函数逐一进行自定义函数的注册。也可以针对某一类测试对象(例如Web对象)进行注册:
'Register all the Web Objects
For Each sSubKey In arrSubKeys
If InStr(1, sSubKey, "Web") > 0 Then
RegisterUserFunc sSubKey, TOMethodName, FunctionName
End If
Next