Home > Articles > Programming

This chapter is from the book

Using getTestData to Extract Data from a List

Lists are relatively easy objects to start with, though there are two twists that follow. The first issue is the test data types:

First twist: If you retrieve the selected element from a Swing JList, the data is returned as ITestDataText (in this case, you then call getText() and you’re done); for any other type of list, the data is returned as ITestDataList.

Second twist (relates only to ITestDataList): Getting the individual data values out of the ITestDataList requires a little more work than you might like. You work with three interface types: ITestDataList, ITestDataElementList, and ITestDataElement.

Tables 3.1 and 3.2 show the most commonly used methods in each of these interfaces.

Table 3.1 List Object Data Types

Data Type

Return Type

Comments

List

ITestDataList

Retrieves all list elements.

Selected

ITestDataList

ITestDataText

Retrieves the selected items. The selected elements in HTML and .Net lists are returned as ITestDataList; selected data in Swing lists (javx.swing.JList) is returned as ITest DataText.

Table 3.2 List Object Interface Methods

Interface

Methods

ITestDataList

int getElementCount() ITestDataElementList getElements()

ITestDataElementList

ITestDataElement getElement( int )

ItestDataElement

Object getElement()

At a high level, this is the algorithm to get list data:

  1. Call getTestData() on your list test object. The data will be returned as ITestDataList.
  2. On the returned ITestDataList, call getElementCount() on the returned ITestDataList to find out how many elements are in the ITestDataList.
  3. Call getElements() on the returned ITestDataList. This returns another interface: ITestDataElementList.
  4. From the returned ITestDataElementList, you can now get any individual element by calling getElement(int). The integer argument passed to getElement() is the index of the element you want (you start counting at zero). getElement() returns yet another interface type: ITestDataElement.
  5. After you have an individual list item as an ITestDataElement, you can call getElement() to get actual data value. getElement() returns the data as an Object, so if you’re working in Java, you need an explicit cast to a String.

Listing 3.9 offers two examples: one that retrieves all elements and one that retrieves selected elements. In both examples, the data is returned as an array of Strings.

Listing 3.9. Getting all elements from a list

Java
public String[] getAllListElements(TestObject testObject) {
      String[] all = null;
      ITestDataList iList = (ITestDataList)
                                    testObject.getTestData("list");
      int count = iList.getElementCount();
      all = new String[count];
      ITestDataElementList iElementList = iList.getElements();
      for (int i = 0; i < count; i++) {
         ITestDataElement iElement = iElementList.getElement(i);
         String value = iElement.getElement().toString();
         all[i] = value;
      }
      return all;
}
VB.NET
Public Function getAllListElements(ByVal testObject As TestObject) _
  As String()
    Dim iData As ITestDataList = testObject.GetTestData("list")
    Dim count As Integer = iData.GetElementCount()
    Dim all(count - 1) As String
    Dim iElementList As ITestDataElementList = iData.GetElements()

    For i As Integer = 0 To count - 1
       Dim iElement As ITestDataElement = iElementList.GetElement(i)
       Dim value As String = iElement.GetElement().ToString()
       all(i) = value
    Next

    Return all
End Function

Getting selected elements from a list

Java
public String[] getSelectedElements(TestObject testObject) {
      String[] selected = null;
      ITestData iData = testObject.getTestData("selected");
      if (iData instanceof ITestDataList) {
        ITestDataList iList = (ITestDataList) iData;
        int count = iList.getElementCount();
        selected = new String[count];
        ITestDataElementList iElementList = iList.getElements();
        for (int i = 0; i < count; i++) {
          ITestDataElement iElement = iElementList.getElement(i);
          String value = iElement.getElement().toString();
          selected[i] = value;
        }
       } else if (iData instanceof ITestDataText) {
            ITestDataText iText = (ITestDataText) iData;
            selected = new String[0];
            selected[0] = iText.getText();
      }
return selected;
}
VB.NET
Public Function getSelectedElements(ByVal TestObject As TestObject) As String()
   Dim selected() As String = Nothing
   Dim iData As ITestData = TestObject.GetTestData("selected")

   If (TypeOf iData Is ITestDataList) Then
     Dim iList As ITestDataList = iData
     Dim count As Integer = iList.GetElementCount()
     ReDim selected(count - 1)
     Dim iElementList As ITestDataElementList = iList.GetElements()
     For i As Integer = 0 To count - 1
       Dim iElement As ITestDataElement = iElementList.GetElement(i)
       Dim value As String = iElement.GetElement()
       selected(i) = value
     Next

   ElseIf (TypeOf iData Is ITestDataText) Then
     Dim iText As ITestDataText = iData
     ReDim selected(1)
     Dim value As String = iText.GetText
     selected(0) = value
   End If

   getSelectedElements = selected
End Function

The last two script examples in Listing 3.9 illustrate a technique for handling the special case of getTestData() returning ITestDataText and not ITestDataList.

  • + Share This
  • 🔖 Save To Your Account