首页 文章

如何获取测试对象的逻辑名称(存在于关联的共享OR中)?

提问于
浏览
6

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

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

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

How could it do that?

"name"测试对象属性返回重新添加测试对象时生成的名称 . There is no (documented) test object property for the logical name. 运行时对象属性不可能包含名称,因为它不是AUT GUI中的名称 .

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

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

ObjectRepositoryUtil API允许我(通过 GetChildren 或其他方法)在存储库的测试对象集合中查找测试对象,并使用 GetLogicalName 方法获取其名称 . 精细 .

但是,使其工作的唯一方法是通过加载来获取对存储库的引用 . 我得到的印象是,这个API旨在操纵(或分析)来自QTP外部的回购,而不是来自测试运行 . 我不想重新加载存储库 . 我想在一个已经加载的存储库中查找测试对象 .

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

So how can I obtain a reference to an already-loaded repository, so I can use GetLogicalName?

Or generally asking: Given a reference to a "normal" test object contained in the current action´s shared repository, how can I find out its logical name programmatically?

如果有一些非常明智的QTP向导,那么知道这一点的Motti就无法完成,我真的很感激他的回答,即使它是“如果这是真的那就无法完成” .

4 回答

  • 8

    你想要“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")"
    
  • 0

    为方便起见,我将所有这些单独的函数加入到一个函数(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
    
  • 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
    
  • 1

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

    在你的情况下:

    该函数应从arguement返回对象的逻辑名称

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

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

    如果有帮助,请告诉我 .

相关问题