Extracting Data from a ComboBox/List Control (JComboBox)

This topic describes how to use Functional Tester's getTestData method to access the values in the list of a ComboBox/List control. The following example tests against the Classics Java application:

import resources.GetListDataExampleHelper;

import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;

/**
* Description : Functional Tester Script
* @author Administrator
*/

public class GetListDataExample extends GetListDataExampleHelper
{
/**
* Script Name : <b>GetListDataExample</b>
* Generated : <b>Dec 31, 2002 1:15:35 PM</b>
* Modified : <b>Dec 31, 2002 1:15:35 PM</b>
* Description : Functional Tester Script
* Original Host : WinNT Version 5.0 Build 2195 (Service Pack 2)
*
* @since 2002/12/31
* @author Administrator
*/
public void testMain (Object[] args)
{
startApp("ClassicsJavaA");

// Frame: ClassicsCD
tree2Tree().click(atPath("Composers->Schubert->Location(PLUS_MINUS)"));
tree2Tree().click(atPath("Composers->Schubert->Die schone Mullerin, Op. 25"));
placeOrderButton2Button().click();

//Declare variables for list
ITestDataList nameList;
ITestDataElementList nameListElements;
ITestDataElement nameListElement;

// Frame: Member Logon
nameComboComboBox().waitForExistence();

// Available test data types: {selected=Selected List Element, list=List Elements}
java.util.Hashtable ht = nameComboComboBox().getTestDataTypes();
System.out.println(ht);

// Get all elements
nameList = (ITestDataList)nameComboComboBox().getTestData("list");

nameListElements = nameList.getElements();

int listElemCount = nameList.getElementCount();

for (int i = 0; i < listElemCount; i++)
{
nameListElement = nameListElements.getElement(i);
System.out.println(nameListElement.getElement());

// Click on each element
nameComboComboBox().click();
nameComboComboBox().click(atText(nameListElement.getElement().toString()));
};

cancelorderlogonButton().click();

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

This example first opens up the Classics Java application. It selects a composer in the tree and an album (composer = Schubert, album = "Die Schone Muellerin") and clicks the "Place Order" button. In the next screen (Member Login - dialog) the sample code extracts the list of values from the ComboBox and displays them in the console window before clicking on each list element.

The first step is to extract the data from the control by using the getTestData method:

ITestDataList nameList;
nameList =(ITestDataList)nameComboComboBox().getTestData("list");

To find out which data types are available for a control, use the following code: .

java.util.Hashtable ht = nameComboComboBox().getTestDataTypes();

Given this data set, you can create an array that contains all of the elements of the list. This is done as follows:

ITestDataElementList nameListElements;
nameListElements = nameList.getElements();

With the list elements in hand, you can create a loop that accesses each list element. To determine the number of list elements, use the getElementCount method. To extract the value of the list element, the getElement method is used. In the example, this is done with the following code:

int listElemCount = nameList.getElementCount();

for (int i = 0; i < listElemCount; i++)
{
nameListElement = nameListElements.getElement(i);
System.out.println(nameListElement.getElement());
};

Terms of use | Feedback
(C) Copyright IBM Corporation 2002, 2004. All Rights Reserved.