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

TheBlastOne picture TheBlastOne · Aug 28, 2013 · Viewed 9.3k times · Source

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.

Answer

Xiaofu picture Xiaofu · Aug 29, 2013

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")"