TestComplete Tip: Parsing HTML Tables

  November 19, 2006

TestComplete provides scripting access to web page elements as well as to their attributes, methods and events. You can use this functionality to obtain data from tables displayed on the web page under test.

A table is a TABLE element with special sub-elements for the table’s header, footer and body. These sub-elements contain rows and cells.

To retrieve data from a table, you can use the rows and cells properties of the table object (this is an object defined by the Document Object Model). The cells property belongs both to the table and to the row objects. The table’s property provides access to all cells of the table. The row’s property provides access to values displayed in the row’s cells.

The following code goes through rows and cells of a table and posts the cell contents to the log. The sample routine works with the web page displaying the orders list. To view this page, open the
<TestComplete>SamplesOpen AppsOrdersDemoWebStart.htm
page in your Internet browser and click View all orders. The test script works with Internet Explorer 6. If you use another browser, you will need to modify script lines responsible for obtaining the Page object and the table object.

Note that the rows property of the table object provides access to all rows of the table, including the rows displayed in the table’s header and footer. To only process cells of the table body, you obtain the objects corresponding to the TBODY elements (note that a table can contain several TBODY elements) and then go through the rows and cells of each TBODY.

The following code demonstrates how you can do this. Like the previous example, it works with the orders list displayed in Internet Explorer 6. If you use another browser, you will need to modify the script lines responsible for obtaining the Page object and the table object.