6

假设我将 a 传递Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox")给一个函数:

MyFunction (Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox"))

稍后,该函数想要记录接收到的测试对象的逻辑名称(在这种情况下,当然是“MyBox”)。

它怎么可能做到这一点?

如果您重新添加测试对象,“名称”测试对象属性会返回构建的名称。 逻辑名称没有(记录的)测试对象属性。 运行时对象属性不可能包含名称,因为它不是来自 AUT GUI 的名称。

所以我认为测试对象不知道它的名字。只有存储库“知道”测试对象存储在哪个名称下。

所以我将不得不检查存储库本身,而不是测试对象。

ObjectRepositoryUtilAPI 允许我(通过或GetChildren其他方法)在存储库的测试对象集合中找到测试对象,并使用该GetLogicalName方法获取其名称。美好的。

但是让它工作的唯一方法是通过加载它来获取对存储库的引用。我的印象是这个 API 旨在从 QTP 外部而不是在测试运行中操作(或分析)存储库。我不想重新加载存储库。我想在已加载的存储库之一中查找测试对象。

API 可以告诉我哪些已加载(通过它们的RepositoriesCollection名称和路径),但它不提供获取对代表其中一个存储库的对象实例的引用的方法。

那么如何获得对已加载存储库的引用,以便我可以使用GetLogicalName

或者一般问:给定对包含在当前操作的共享存储库中的“正常”测试对象的引用,我如何以编程方式找出它的逻辑名称?

如果有一些超级明智的 QTP 向导 a la Motti 知道这是不可能的,我真的很感激他的回答,即使它显示“它不能完成”如果这是真的。

4

4 回答 4

9

您想要“TestObjName”属性:

function GetRepoName(obj)
    GetRepoName = obj.GetTOProperty("TestObjName")
end function

用法:

logicalName = GetRepoName(Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox"))
'logicalName now equals "MyBox"

如果您觉得需要将整个对象链重构为字符串,您可以使用以下方法“GetFullQtpName”(还需要 GetRepoName 以及下面的 2 个额外方法):

function GetFullQtpName(obj)
    dim fullQtpName : fullQtpName = MakeQtpName(obj)
    dim objCurrent : set objCurrent = obj

    do while not IsEmpty(objCurrent.GetTOProperty("parent"))
        set objCurrent = objCurrent.GetTOProperty("parent")
        fullQtpName = MakeQtpName(objCurrent) & "." & fullQtpName
    loop

    GetFullQtpName = fullQtpName
end function

function MakeQtpName(obj)
    MakeQtpName = GetClassName(obj) & "(""" & GetRepoName(obj) & """)"
end function

function GetClassName(obj)
    GetClassName = obj.GetTOProperty("class Name")
end function

用法:

fullQtpName = GetFullQtpName(Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox"))
'fullQtpName now equals "Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox")"
于 2013-08-29T02:25:28.113 回答
1

为方便起见,我将所有这些单独的函数合并为一个函数 (GetFullORName),它工作得很好!我用它在我的自定义函数中提供更好的 Reporter.Event 信息......

Function GetFullORName (obj)
    Dim fullUFTName : fullUFTName = obj.GetTOProperty("class name") & "(""" & obj.GetTOProperty("TestObjName") & """)"
    Dim objCurrent : Set objCurrent = obj
    Do While Not IsEmpty(objCurrent.GetTOProperty("parent"))
        Set objCurrent = objCurrent.GetTOProperty("parent")
        fullUFTName = objCurrent.GetTOProperty("class name") & "(""" & objCurrent.GetTOProperty("TestObjName") & """)" & "." & fullUFTName
    Loop
    GetFullORName = fullUFTName
End Function


Public Function CheckObjExist (obj)
    If obj.Exist Then
        Reporter.ReportEvent micPass, "CheckObjExist [" & obj.GetTOProperty("TestObjName") & "]", "Object = [ " & GetFullORName(obj) & " ]" & Chr(13) & "Object exists"
        CheckObjExist = True
    Else
        Reporter.ReportEvent micFail, "CheckObjExist [" & obj.GetTOProperty("TestObjName") & "]", "Object = [ " & GetFullORName(obj) & " ]" & Chr(13) & "Object does NOT exist"
        CheckObjExist = False
    End If
End Function
于 2014-09-25T15:11:37.160 回答
0

我刚刚想出的唯一解决方法有很多明显的缺点,包括它的不完整性,看起来像这样:

Function GetLogicalName (ByVal TestObject)
    Dim NameWithType: NameWithType=TestObject.ToString
    Dim TypeProp: TypeProp=TestObject.GetTOProperty ("micclass")
    Dim Suffix
    Select Case TypeProp
        Case "Page" 
            Suffix=" web page"
        Case "Browser" 
            Suffix=" browser"
        Case "JavaApplet" 
            Suffix=" applet"
        Case "JavaButton" 
            Suffix=" button"
        Case "WebCheckBox" 
            Suffix=" check box"
        Case "WebEdit" 
            Suffix=" edit box"
        Case "WebElement" 
            Suffix=" object"
        Case "WebFile" 
            Suffix=" edit box"
        Case "WebTable" 
            Suffix=" table"
        Case "JavaObject" 
            Suffix=" object"
        Case else
            MsgBox "Unknown micclass '" & TypeProp & "'"
            ExitTest
    End Select
    GetLogicalName=Left (NameWithType,Len (NameWithType)-Len (Suffix))
End Function
于 2013-08-28T14:37:47.337 回答
0

逻辑名称可以通过简单的一行代码而不是这么多行来检索:

在你的情况下:

该函数应从争论中返回对象的逻辑名称

MyFunction (Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox"))  

Function MyFunction(obj)  

MyFunction= obj.ToString()  'This is an inbuilt method of object in QTP

End Function  

让我知道它是否有帮助。

于 2016-07-29T03:30:29.410 回答