Extracting data from a combobox/list control (JComboBox)

You can use HCL DevOps Test UI (Test UI)'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.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 GetListDataExample extends GetListDataExampleHelper
{
/**
* Script Name   : GetListDataExample
* Generated     : May 16, 2006 9:06:46 AM
* Description   : Functional Test Script
* Original Host : WinNT Version 5.1  Build 2600 (S)
* 
* @since  2006/05/16
* @author Administrator
*/
public void testMain(Object[] args) 
{
startApp("ClassicsJavaA");

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

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

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

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

//Get all elements
nameList = (ITestDataList)nameCombo().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
	nameCombo().click();
	nameCombo().click(atText(nameListElement.getElement().toString()));
	};
	
cancel().click();

// Frame: ClassicsCD
classicsJava(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)nameCombo().getTestData("list")

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


java.util.Hashtable ht = nameCombo().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());
  
  // Click on each element
	nameCombo().click();
	nameCombo().click(atText(nameListElement.getElement().toString()));
};