Home > Articles > Programming

This chapter is from the book

Using getTestData() to Read Data in a Table

As opposed to lists, which require several method calls (and three interfaces) to get list values, tabular table is straightforward; you use only one interface. The initial learning challenge is the test data type arguments. There are quite a few; some are test object domain-specific. (See Tables 3.3, 3.4, and 3.5.)

Table 3.3 HTML Tables

Test Data Type

Return Type

Comments

Provides Access to Column and Row Header Data?

Contents

ITestData Table

Returns all table data, including hidden rows/columns.

Yes

Grid

ITestData Table

Returns all table data, including hidden rows/columns.

No

visiblecontents

ITestData Table

Returns all data displayed in the table.

Yes

visiblegrid

ITestData Table

Returns all data displayed in the table.

No

Text

ItestData Text

Returns the entire contents of the tables as a single string.

No

Table 3.4 Swing Tables

Test Data Type

Return Type

Comments

Provides Access to Colum and Row Header Data?

contents

ITestData Table

Returns all data, including hidden rows/columns.

Yes

visible contents

ITestData Table

Returns all data displayed in the table.

Yes

selected

ITestData Table

Returns all selected data, including hidden rows/columns.

Yes

visible selected

ITestData Table

Returns all displayed selected data.

Yes

Table 3.5 Microsoft .NET Grids

Test Data

Return Type

Comments

Provides Access to Colum and Row Header Data?

viewcontents

ITestData Table

Returns all data displayed in the table.

Yes

Sourcecontents

ITestData Table

Returns all data in the table source, i.e., includes rows/columns not displayed in the table.

Yes

Viewselectedrow

ITestData Table

Returns all data displayed in the selected row(s).

Yes

Sourceselectedrow

ITestData Table

Returns all data in table source, i.e., includes rows/columns not displayed in the table.

Yes

Viewcurrentrow

ITestData Table

Returns all data displayed in the currently active row. i.e., the row with focus.

Yes

Sourcecurrentrow

ITestData Table

Returns all data in the source of the currently active row.

Yes

Table 3.6 lists the most commonly used methods in ITestDataTable.

Table 3.6 Most Commonly Used ITestDataTable Methods

Method

Comments

int getRowCount()

Returns the number of rows in the data returned.

int getColumnCount()

Returns the number of columns in the data returned.

Object getColumnHeader(int)

Returns the value of the column title at the specific index. Because the data is returned as Object, you typically cast to a String.

Object getRowHeader(int)

Returns the value of the row header at the specific index. Because the data is returned as Object, you typically cast to a String.

Object getCell (int, int)

Returns the data in the row, column pair passed. Because the data is returned as Object, you typically cast to a String.

Listing 3.10 provides two examples that print to the console (Output window in Visual Studio) all the test data types available in the (table) test object passed. This method (function in VB.NET) can be used at design time to compare what is returned in each case and to help you determine the appropriate test data type argument for a table you are coding against.

Listing 3.10. Printing all of a table’s test data types

Java
public void printTableData(TestObject table) {

   Enumeration<String> testDataTypes = table.getTestDataTypes()
                                                            .keys();
   while (testDataTypes.hasMoreElements()) {
      String testDataType = testDataTypes.nextElement();
      System.out.println(testDataType);
      ITestData iData = table.getTestData(testDataType);
      if (iData instanceof ITestDataTable) {
         ITestDataTable iTableData = (ITestDataTable) table
                                    .getTestData(testDataType);
         int rows = iTableData.getRowCount();
         int cols = iTableData.getColumnCount();
         for (int col = 0; col < cols; col++) {
            System.out.print(iTableData.getColumnHeader(col));
            System.out.print("\t\t");
         }
         System.out.print("\n");
         for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
               System.out.print(iTableData.getCell(row, col));
               System.out.print("\t\t");
            }
            System.out.print("\n\n");
         }
         System.out.print("\n");
       } else if ( iData instanceof ITestDataText ) {
          ITestDataText iText = (ITestDataText) iData;
          String text = iText.getText();
    System.out.println(text + "\n\n" );
 }
    }
  }
VB.NET
Public Sub printTableData(ByVal table As TestObject)
   Dim testDataTypes As System.Collections.ICollection = _
                                          table.GetTestDataTypes.Keys
   For Each testDataType As String In testDataTypes
      Dim iData As ITestData = table.GetTestData(testDataType)
      Console.WriteLine(testDataType)
      If TypeOf iData Is ITestDataTable Then
         Dim iTableData As ITestDataTable = CType(iData, ITestDataTable)
         Dim rows As Integer = iTableData.GetRowCount()
         Dim cols As Integer = iTableData.GetColumnCount()

         ' First print column headers if available
         Try
            For col As Integer = 0 To cols
               Console.Write(iTableData.GetColumnHeader(col))
               Console.Write(Chr(9) + Chr(9))
            Next
            Console.Write(Chr(13))
         Catch ex As Exception
            Console.WriteLine("COLUMN HEADERS NOT AVAILABLE")
         End Try
         For row As Integer = 0 To rows
            For col As Integer = 0 To cols
               Console.Write(iTableData.GetCell(row, col))
               Console.Write(Chr(9) + Chr(9))
            Next
            Console.Write(Chr(13))
         Next
         Console.Write(Chr(13))
      ElseIf (TypeOf iData Is ITestDataText) Then
         Dim iText As ITestDataText = CType(iData, ITestDataText)
         Dim text As String = iText.GetText()
         Console.WriteLine( text + chr(13) + chr(13) )
      End If
   Next
End Sub

Referring to the previous code samples, note the following. First, you want to note that the target (control) of the code is a Swing table. To get all test data types applicable to the table, you call getTestDataTypes. The ITestDataTable object (returned by the call to getTestData()) can be used to determine the number of rows and columns in the table by calling getRowCount() and getColumnCount(). If column headers are not available, an exception can be thrown; you therefore use a try block to prevent the script from failing. To retrieve the value in any particular cell, call getCell(row, col), passing in the row and column index of the cell you’re interested in (start counting at 0). getCell() returns the value as an object, which you almost always downcast to a string. In Swing tables, the column and row headers are separate Swing objects from the table object. The data in these headers, however, can be retrieved via methods in the ITestDataTable object returned by calling getTestData() on the table. In other words, your scripts do not need to worry about having (or getting) separate column or row header TestObjects. After you have the ITestDataTable in the table, call getColumnHeader() and getRowHeader() methods, respectively.

  • + Share This
  • 🔖 Save To Your Account