How can I get the logical name of a test object (that exists in the associated shared OR)?

StackOverflow https://stackoverflow.com/questions/18490083

  •  26-06-2022
  •  | 
  •  

Question

Let´s say I pass a Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox") to a function:

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

Later, the function wants to log the logical name of the received test object (which in this case, of course, is "MyBox").

How could it do that?

The "name" test object property returns the name that is built if you re-add the test object. There is no (documented) test object property for the logical name. The runtime object properties can´t possibly contain the name since it is not a name from the AUT GUI.

So I think the test object does not know its name. Only the repository "knows" under which name the test object is stored there.

So I will have to inspect the repository itself, not the test object.

The ObjectRepositoryUtil API allows me (via GetChildren, or other methods) to find the test object in the repository´s test object collection, and use the GetLogicalName method to get its name. Fine.

But the only way to get that to work is to obtain a reference to a repository by loading it. I get the impression this API is designed to manipulate (or analyze) repos from outside of QTP, not from within a test run. I do not want to re-load the repository. I want to look up the test object in one of the already-loaded repositories.

The RepositoriesCollection API can tell me which are loaded (by their name and path), but it does not provide a means of obtaining a reference to the object instance that represents one of those repositories.

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?

If there is some ultra-wise QTP wizard a la Motti who knows this can not be done, I´d really appreciate an answer from him even if it reads "it cannot be done" if this is true.

Was it helpful?

Solution

You want the "TestObjName" property:

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

Usage:

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

Should you feel the need to reconstruct the entire object chain as a string, you can use the following method "GetFullQtpName" (which also requires GetRepoName plus the 2 extra methods below):

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

Usage:

fullQtpName = GetFullQtpName(Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox"))
'fullQtpName now equals "Browser("MyBrowser").Page("MyPage").WebCheckBox("MyBox")"

OTHER TIPS

For convenience, I joined all of these separate functions into a single function (GetFullORName), and it works GREAT! I use it to give better Reporter.Event info in my custom functions...

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

The only workaround I just came up with has a lot of obvious downsides, including its incompleteness, and looks like this:

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

Logical name can be retrieved by a simple line of code instead of so many lines:

In your case:

The function should return logical name of the object from 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  

Let me know if it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top