I wanted a way of referencing an object within the UFT Object Repository starting from a spreadsheet.
Most solutions I’ve seen do something like this…
Dim objectFromOR = DataTable(“Object”, dtLocalSheet)
Dim objectValue= DataTable(“Value”, dtLocalSheet)
Browser(“Main Browser”).Page(“SB Admin 2 - Login”).WebEdit(objectFromOR).set objectValue
If I know the specific object name (logical name), why do I have to specify its Browser, Page, and Object class to interact with it???
IMO, this takes away from what can be accomplished with descriptive programming.
Here is a function that allows me to simply specify an object’s Logical Name and have it return an interactive object (one that I can “set” or “click”)
Function buildDescFromRepo(inObjectString)
Dim RepositoryFrom
Dim TOCollection
Dim TestObject
Dim localProperties
Dim i
‘Creating Object Repository utility Object
Set RepositoryFrom = CreateObject(“Mercury.ObjectRepositoryUtil”)
‘Load Object Repository
‘The ObjectRepositoryPath variable was set globaly
RepositoryFrom.Load ObjectRepositoryPath
Set TOCollection = RepositoryFrom.GetAllObjects()
For i = 0 To TOCollection.Count - 1
Set TestObject = TOCollection.Item(i)
print TestObject.GetTOProperty(“TestObjName”)
If RepositoryFrom.GetLogicalName(TestObject) = inObjectString Then
Set localProperties = Description.Create()
‘A sub function that helps parse the descriptive properties
parseProperties TestObject, localProperties
Set buildDescFromRepo = localProperties
End If
Next
End Function
Sub parseProperties(testObject, in_localProperties)
Dim i
Dim PropertiesCol
Dim ObjectProperty
Set PropertiesCol=testObject.GetTOProperties
‘This gets us all the descriptive properties listed in the OR for the object
For i=0 to PropertiesCol.count-1
Set ObjectProperty=PropertiesCol.Item(i)
in_localProperties(ObjectProperty.name).Value = ObjectProperty.value
Next
End Sub
Now the code that interacts with the object looks like this…
set genericScreen = Browser(“CreationTime:=0”).Page(“index:=0”) ‘a generic browser and page
set getObjDesc = buildDescFromRepo(dict.Item(“Object”)) ‘Call to the above function that returns the object description. The logical name was passed in.
set objectFromOR = genericScreen.ChildObjects(getObjDesc) ‘Query the generic browser/page with the returned object description. Now we have the interactive object!
objectFromOR.set “Hello World!”
Notice I have combined descriptive programming for the Browser and Page and I’ve removed the need to specify the interactive object’s class (WebEdit, etc.) I only have to specify its logical name and the routine figures out the rest!
As a next step, I can leverage this ability within a Keyword Framework to keep my coding minimal and manage all objects in the repository without updating code. The generic nature also makes this capability reusable on any web app.
Let me know what you think!!