运行时查询组件参数?

Query component parameters at runtime?

如何查询当前正在执行的组件的参数的参数名称?

第一个想到的想法是使用 UFT 自动化对象并查询 .BusinessComponent 属性,如下所示:

Dim UFTApp: Set UFTApp=CreateObject ("QuickTest.Application")
Dim Index
Print "BusinessComponent.ParameterDefinitions.Count=" & CStr (UFTApp.BusinessComponent.ParameterDefinitions.Count)
For Index = 1 To UFTApp.BusinessComponent.ParameterDefinitions.Count
    Print CStr (Index) & ": " & UFTApp.BusinessComponent.ParameterDefinitions.Item (Index).Name
Next

获得名称后,我可以调用 Parameter (<Name>) 来获取值。或者,为了也获得值,可以使用

Print "BusinessComponent.ParameterDefinitions.GetParameters.Count=" & CStr (UFTApp.BusinessComponent.ParameterDefinitions.GetParameters.Count)
For Index = 1 To UFTApp.BusinessComponent.ParameterDefinitions.GetParameters.Count
Print CStr (Index) & ": " & UFTApp.BusinessComponent.ParameterDefinitions.GetParameters.Item (Index).Value
Next

如果组件是独立执行的,这工作正常。

但是! 如果将组件作为 BPT 的一部分执行,.Count 方法 return 零。

在这种情况下似乎没有设置 ParameterDefinitions 集合。 (注意 .BusinessComponent.Name return 的值正确,因此容器实例本身确实设置正确。)

如果我事先不知道参数名称,如何迭代当前组件的参数?

一旦您创建了不允许预先 "know" 名称的通用库代码,就会出现这个问题。

我 99% 确定同样的问题适用于 QTP,所以我也包含了那个标签。

原来主要的问题是 BusinessComponent.ParameterDefinitions 设置不正确,至少在所有情况下都没有正确设置。

要获取参数定义,我们需要查看 ALM 的 API。

那里,主要问题是我们不知道要查询哪个组件,即我们不知道哪个是当前 运行ning 组件。

我发现获取当前组件名称(和 ALM 路径)的唯一可靠方法是检查 UFTApp.BusinessComponent.Location。 (UFTApp.BusinessComponent.Name 不可靠,因为某些版本的 UFT 似乎使用组件 ID 来查找测试计划中的组件名称,这会产生一些(不可预测的)测试名称,而不是正确的组件名称。)

因此,为了枚举当前组件的参数,我现在在 ALM 的组件文件夹工厂中查找组件的路径(从 .Location 获得),过滤生成的 ALM 文件夹,以便过滤列表仅包含具有搜索名称的组件(也从 .Location 获得),并使用 ALM 的 API 查询参数。

只要您不使用版本控制,这似乎就可以正常工作,或者您确保在修改参数后先签入更改,然后再 运行 下面的代码。

由于 ALM API 不理解 UFT 想要的 “[ALM] “ 前缀(并且包含在 BusinessComponent.Location 属性 值中),一些快速而肮脏的需要字符串解析来构建要搜索的路径(或名称)。

结果代码:

' Global shorthand for CreateObject ("QuickTest.Application") (UFT OTA-interface)
Dim UFTApp: Set UFTApp=GetObject ("","QuickTest.Application")


'@Description Name including path of current UFT component?
Public Function GetCurrentModuleName ()
    Dim Result: Result=GetUFTCurDoc.Location
    Result=Right (Result, Len (Result)-Len ("[ALM] "))
    GetCurrentModuleName=Result
End Function

'@Description Component name (without path) of the specified component?
'   ModuleName: Component name including ALM path
Public Function GetModuleName (ByVal ModuleName)
    ' Use split with backslash, then return only the last element
    Dim Parts: Parts=Split (ModuleName, "\", -1, vbBinaryCompare)
    GetModuleName=Parts(UBound (Parts))
End Function

'@Description ALM path of the specified componentname (including path) (returned value does not end with a backslash)?
'   ModuleName: Component name (including path) in ALM
Public Function GetModulePath (ByVal ModuleName)
    GetModulePath=Left (ModuleName, Len(ModuleName)-Len (GetModuleName (ModuleName))-1)
End Function


Dim ComponentFilter: Set ComponentFilter=QCUtil.QCConnection.ComponentFolderFactory.FolderByPath (GetModulePath (GetCurrentModuleName ())).ComponentFactory.Filter
ComponentFilter ("CO_NAME")="""" & GetModuleName (GetCurrentModuleName ()) & """"
Dim Param
For Each Param in ComponentFilter.NewList().Item(1).ComponentParamFactory.NewList("")
    Print Param.Name
Next

因为这完美地回答了我原来的问题,我会接受我自己的答案,尽管这看起来很糟糕,所以遇到同样问题的每个人都可以使用这种方法。

使用ALM + BPT,我用下面的代码多次执行一个组件,每次我都得到正确的参数计数。我还在同一个 BPT 中两次使用同一个组件。

UFTApp.BusinessComponent.ParameterDefinitions.Count

您的计数一直为 0 还是偶尔一次?