在TestPartner中创建Class Module扩展测试对象支持的方法和例子
TestPartner implements VBA (an object-oriented script language) as an automation client, and models the elements of an application under test as objects exposed to VBA. This is the purpose of using TPOSI objects and the TestPartner type library. The TestPartner type library defines the properties, methods, and events available for each TPOSI object, allowing design time interrogation of these objects, a concept known as early binding.
However, a powerful feature of an automation client such as VBA is the support of late binding. Late binding allows an object to be generically typed (as Object) at design time, then interrogated at run time for the properties, methods, and events that are not exposed at design time. TPOSI objects do not inherently support late binding, which does not present an issue for traditional applications where the role of each object is clearly delimited and can be interrogated at design time, but does present an issue in respect to modern object-oriented systems such as ActiveX controls, Java, and .NET. In such systems, TestPartner's TPOSI objects have restricted access to application objects at design time. Only the properties, methods, and events listed in the TestPartner type library are available, and without late binding support, much of the functionality in the objects of the application would remain closed for interrogation.
In order to support late binding, TestPartner offers Object Extension through the IDispatch interface. Using the methods of IDispatch combined with the Object( ) method from TPOSI, TestPartner has the ability, at run time, to interrogate late bound objects in an application under test.
Tip: To view an object's run-time properties exposed by the Object( ) method, open the Watch window and set a breakpoint in your test script. In debug mode, you can expand the nodes to display the properties.
The following steps provide an example of employing the Internet Explorer Object Model to extend TestPartner's standard support. The example makes use of the Object( ) method to generically define the THTMLTable object inside the Internet Explorer browser, and interrogates the late bound THTMLTable object at run time.
To extend TestPartner's object support for base objects - example:
Start TestPartner.
Click File>New. A New Asset dialog box appears:
Create a new Class Module, and click OK. A blank Class Module appears.
Click Insert>File..., and use the browser that appears to open and insert C:\Program Files\Micro Focus\TestPartner\Samples\THTMLTableEx_ClassModule.txt.
This file provides a wrapper around the THTMLTable object to interface with the Microsoft Internet Explorer HTML Object Model. Notice that the code contains an Object( ) method in the Click() function. It's through the use of the Object( ) method that the code can access the actual object from the Internet Explorer's Object Model, and make calls to the object at run time.
In order to access many of the interfaces used in this Class Module, there must be a reference made to the Microsoft HTML Object Library. Follow steps 5 and 6 to create this reference.
Click View>Object Browser. The Object Browser appears.
Right-click anywhere in the Object Browser and choose References from the menu, a References dialog box appears. Scroll down the References list and put a checkmark next to Microsoft HTML Object Library. Click OK.
Close the Object Browser.
Save the Class Module, naming it THTMLTableEx, and close the Class Module.
From the Windows Explorer, double-click the HTML file: C:\Program Files\Micro Focus\TestPartner\Samples\thtmltableex_demo.htm to open it in an Internet Explorer window. This file contains the HTML table that is interrogated at run time.
Return to TestPartner. Click File>New. A New Asset dialog box appears. Create a new test script, and click OK. The View code window appears with some text. Clear all text.
Click Insert>File..., and use the browser that appears to open and insert C:\Program Files\Micro Focus\TestPartner\Samples\THTMLTableEx_Example_Script.txt. This file is a test script which attaches to the running HTML table object, retrieves text from cells in the object, and automates objects inside the tables cells.
Save the test script, naming it TableExample, then play it back. Click OK when prompted, and notice that the test script is able to interrogate and manipulate data and control in the HTML Table object.