问题
Is it possible to add objects to an object repository automatically using VBScript?
Let's say I have list of links in my webpage/application. It would be time consuming, when I add objects manually so I tried to add objects to repository automatically. But couldn't able to proceed as I don't know how to activate object repository (during run time) in QTP.
Below is my code which I tried:
Function Objects_Count(myObject)
Dim Objects
Set Objects=Description.Create
Objects("micclass").value=myObject
set obj=Browser("title:=.*").Page("title:=.*").ChildObjects(Objects)
For i=0 to obj.count-1
'msgbox i
c=obj(i).getroproperty("name")
If obj(i).Exist(1) Then
obj(i).highlight
End If
Print c
Next
End Function
Call Objects_Count("link")
The above code will get the links name displayed in a log file and also it will highlight in application/webpage. Please guide me how to add the properties of that links(objects) to object repository.
Thanks!
回答1:
In order to manipulate the object repository, you'll need the Mercury.ObjectRepositoryUtil
object, load
an existing object repository and add an object to it using AddObject
.
Set myRepository = CreateObject("Mercury.ObjectRepositoryUtil")
myRepository.Load "C:\QuickTest\Tests\Flights.tsr"
myRepository.AddObject myLink, Browser("B").Page("P"), "myLinkName"
You'll have to play with it a bit to get it working in your situation. Use the QTP help to find out all accessible methods/properties of the ObjectRepositoryUtil, just enter "ObjectRepositoryUtil" in the search box underneath the Index
tab.
回答2:
Edit: You need to create the test object by its ProgId not by its micclass
.
Then use SetTOProperty
in order to create the relevant description from the values returned by ChildObjects
.
See this question for more details.
I don't believe there is a way to add a test object dynamically. ObjectRepositoryUtil.AddObject
has this in its documentation:
You can use this method to add a copy of an object that you retrieved from a different object repository.
You can dynamically create a repository via XML with the properties fetched in your test but I think this is more bother than it's worth.
Probably best to use descriptive programming in this case.
For more details see this LinkedIn thread.
来源:https://stackoverflow.com/questions/14813791/how-to-add-objects-to-object-repository-automatically-using-vb-scripts