使用 getTestData 方法在表单元格中进行迭代

本主题提供一个示例,即使用 Functional Tester 的 getTestData 方法来访问网格控件的单元格中的值。

该示例针对 Classics Java™ 应用程序进行测试:

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();
}
}

此示例浏览至应用程序的“查看现有订单”屏幕。 本样本中的代码从网格的所有单元格中抽取值,并在控制台窗口中进行显示。

抽取数据的第一步是使用 getTestData 方法来从控件中抽取数据。此操作使用以下语法来完成:

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

获取此数据集后,您可以通过使用 getRowCountgetColumnCount 方法来确定总行数和总列数。 您也可以使用 getTestDataTypes 来询问控件,表中有哪些可用的数据类型。以下代码会将这些查询的结果发送到控制台窗口。

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());

下一步是打印出个别单元格的值,可通过使用 for 循环在网格的行和列中进行循环来完成此操作:

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) );
    }
}

示例脚本使用 getCell 方法打印出当前单元格的值。另请注意,getColumnHeader 方法会打印出当前的列标题。使用网格时,行和列的编号都从 0 开始。这不适用于 getRowCountgetColumnCount 方法(这些方法中编号从 1 开始)。


反馈