Iterating through table cells using the getTestData method

This topic provides an example of  using Functional Tester's getTestData method to access the values in the cells of a grid control.

The example tests against the Classics Java application:

import resources.GetGridDataExampleHelper;
import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.SAP.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;

/**
* Description   : Functional Test Script
* @author Administrator
*/
public class GetGridDataExample extends GetGridDataExampleHelper
{
/**
* Script Name   : GetGridDataExample
* Generated     : Jul 14, 2006 3:05:22 PM
* Description   : Functional Test Script
* Original Host : WinNT Version 5.1  Build 2600 (S)
* 
* @since  2006/07/14
* @author Administrator
*/
public void testMain(Object[] args) 
{
//Start Classics Java Application
startApp("ClassicsJavaA");

//Navigate to Existing Order Grid
jmb().click(atPath("Order"));
jmb().click(atPath("Order->View Existing Order Status..."));

// Frame: View Order Status
nameComboB().click();
nameComboB().click(atText("Claire Stratus"));
ok().click();

// Frame: View Existing Orders
existingTable().click(atPoint(172,92));

//Get the data for the table
ITestDataTable orderTable = (ITestDataTable)existingTable().getTestData("contents");

//Display the available data types for the grid, total rows and columns.
System.out.println ("Available Data Types: " + existingTable().getTestDataTypes());
System.out.println ("Total Rows in table : " + orderTable.getRowCount());
System.out.println ("Total Cols in table : " + orderTable.getColumnCount());

      // Cycle through all rows
      for (int row=0; row < orderTable.getRowCount();++row)
      {
          // Cycle through all columns
          for (int col=0; col < orderTable.getColumnCount();++col)
          {
              // Print out values of cells at (row,col) coordinates
              System.out.println ("Row " + row + ", " + orderTable.getColumnHeader(col) + ": " +orderTable.getCell(row,col) );
          }
      }
// Close the frame
close().click();

// Frame: ClassicsCD
classicsJava(ANY,MAY_EXIT).close();
}
}

This example navigates to the "View Existing Orders" screen of the application. The code in this sample extracts the values from all cells in the grid and displays them in the console window.

The first step to extracting the data is to use the getTestData method to extract the data from the control. This is done with the following syntax:

ITestDataTable orderTable;
orderTable = (ITestDataTable)existingTable().
  getTestData("contents");

Given this data set, you can determine the total number of rows and columns by using the getRowCount and getColumnCount methods. You can also ask the control what data types are available from the table using the getTestDataTypes. The following code sends the results of these queries to the console window.

System.out.println ("Available Data Types: " +
     existingTable().getTestDataTypes());
System.out.println ("Total Rows in table : " +
     orderTable.getRowCount());
System.out.println ("Total Cols in table : " +
     orderTable.getColumnCount());

The next step is to print out the values of the individual cells, which is done by using a for loop to cycle through the rows and columns of the grid:

for (int row=0; row < orderTable.getRowCount();++row)
{
    // Cycle through all columns
    for (int col=0; col < orderTable.getColumnCount();++col)
    {
        // Print out values of cells at (row,col) coords
        System.out.println ("Row " + row + ", " +
        orderTable.getColumnHeader(col) + ": " +
        orderTable.getCell(row,col) );
    }
}

The example script uses the getCell method to print out the value of the current cell. Note also that the getColumnHeader method prints out the current column header. When working with a grid, the numbering for both rows and columns starts at 0. This does not apply to the getRowCount and getColumnCount methods where numbering starts at 1.