Using the Object Repository API in QuickTest ProMarch 10, 2006
Note: We don't know what they're going to do with this in version 9 yet, so everything here applies to QTP 8.2.
I don't know why Mercury chooses not to say much about this. I had
to do a lot of hunting and pecking to get to it, and the trainers at my
Adv QTP course all but told me there's no way to do it. But it's
probably sitting there already, right there on your hard drive.
The Object Repository API allows you to do just about anything to
any Object Repository you have access to - the OR files are stored in
binary, and XML export of the OR won't be available until version 9 (or
so we hear).
Here are the prerequisites:
- The first thing you have to do is make sure you've installed the
QuickTest Plus Toolkit. If you do a custom install, you'll see that
Object Repository API is one of the checkboxes.
- After installation, you have to register the dll manually. It's
located (by default) in "C:\Program Files\Mercury Interactive\QuickTest
Plus\Sdk\ObjectRepositoryAPI"
regsvr32 RepositoryUtil.dll
- Once that's done, you should be able to see all the objects and
methods from within Visual Studio, or if you're a geek like me, you'll
see them in the ActiveState COM OLE browser. That's right, I use Perl
to automate QuickTest Pro.
In the same directory with the DLL is a short rtf document detailing
some things about the API, including sample code. It is by no means
comprehensive, but the examples are good enough to get you going.
There is an AddObject method, though it's best used when you're
opening one OR, reading an object from it, and putting this object into
another OR. You can create objects from scratch, but there is a lot of
initialization involved, and I've never had a compelling enough case to
go through all that. I'm sure you could do it with some perserverance.
If you do, let us know and post some code.
Here's a snippet of VB code that, once the dll is registered, you
should be able to use to get an OR object, and to start manipulating
and changing it:
DimImageObj, PageObj, RepositoryFrom, RepositoryTo
SetRepositoryFrom =CreateObject("Mercury.ObjectRepositoryUtil")
SetRepositoryTo =CreateObject("Mercury.ObjectRepositoryUtil")
RepositoryFrom.LoadActionRepository"E:\Temp\Tests\Test1","Action1"
RepositoryTo.Load"E:\Temp\Tests\Default.tsr"
That gets you access to the object so you can start making method calls.
I haven't written much code in VBS for this stuff, so it's time to
move to Perl. The syntax is almost the same in this kind of scrīpt, so
you shouldn't have any trouble following along. This code doesn't do
much, but it should give you a basic idea. In the next few days I'll be
able to produce my XML-generation code. I posted it to the Mercury
forum once, but I think it got buried (as usual) beneath all the crap.
useWin32::
OLE;
my($ImageObj,
$PageObj);
my$Rep= Win32::
OLE->new
("Mercury.ObjectRepositoryUtil");
$Repository->Load
('HC.tsr');
#########################################################
# The function enumerates all the Test Objects under a
# given root, printing out the keys and values.
# This could be used to generate a hash, which can
# then be used to generate test scrīpt code or an
# XML dump.
#########################################################
subenumProps
{ my$root=
shift;
my($TOCollection,
$TestObject,
$PropertiesCollection,
$Property,
$Msg);
$TOCollection=
$Repository->GetAllObjects
();
formy$i(0..
$TOCollection->Count -
1) { $TestObject=
$TOCollection->Item
($i);
$name=
$Repository->GetLogicalName
($TestObject).
"\n";
$PropertiesCollection=
$TestObject->GetTOProperties;
for(0..
$PropertiesCollection->Count -
1) { print$PropertiesCollection->Item
(n
);
print$Property->Name .
"-".
$Property->Value .
"\n";
} } return$or;
}my$or= enumProps
('Browser');