Home > Articles > Programming

This chapter is from the book

Retrieving All Properties of a Test Object

The two sets of properties discussed in the “Viewing an Object’s Properties” section—value and nonvalue properties—can be retrieved from a test object in a script by invoking getProperties() and getNonValueProperties(), respectively. Each of these methods returns a Hashtable of property names and values. For nonvalue properties, the values are references to the actual objects in the target application (remote JVM). Listing 3.7 shows three methods: one that retrieves value properties, one that retrieves nonvalue properties, and one that calls both methods.

Listing 3.7. Methods for retrieving all properties of a test object

Java
public void printValueProperties(TestObject testObject) {

      Hashtable valueProperties = testObject.getProperties();
      Enumeration valuePropNames = valueProperties.keys();
      Enumeration valuePropValues = valueProperties.elements();
      while (valuePropNames.hasMoreElements()) {
            System.out.println(valuePropNames.nextElement() + ":"
            + valuePropValues.nextElement());
      }
}

public void printNonValueProperties(TestObject testObject) {

      Hashtable nonValueProperties = testObject.getNonValueProperties();
      Enumeration nonValuePropNames = nonValueProperties.keys();
      Enumeration nonValuePropValues = nonValueProperties.elements();
      while (nonValuePropNames.hasMoreElements()) {
            System.out.println(nonValuePropNames.nextElement() + ":"
            + nonValuePropValues.nextElement());
      }
}

public void printAllProperties(TestObject testObject) {

      printValueProperties(testObject);
      printNonValueProperties(testObject);
}
VB.NET
Public Sub printAllProperties(ByVal testObject As TestObject)
   printValueProperties(testObject)
   printNonValueProperties(testObject)
End Sub

Public Sub printValueProperties(ByVal testobject As TestObject)
   Dim valueProperties As Hashtable = testobject.GetProperties
   Dim valuePropNames As ICollection = valueProperties.Keys
   Dim valuePropValues As ICollection = valueProperties.Values

   Dim enumPropNames As IEnumerator = valuePropNames.GetEnumerator()
   Dim enumPropVals As IEnumerator = valuePropValues.GetEnumerator()

   Do While enumPropNames.MoveNext And enumPropVals.MoveNext
      Dim currentProp As String = enumPropNames.Current
      Dim currentPropVal As Object = enumPropVals.Current
         If TypeOf currentPropVal Is Object Then
            If Not (TypeOf currentPropVal Is String) Then
               currentPropVal = currentPropVal.ToString
            End If
         End If
         Console.WriteLine(currentProp + ":" + currentPropVal)
   Loop
End Sub


Public Sub printNonValueProperties(ByVal testobject As TestObject)
   Dim valueProperties As Hashtable = testobject.GetNonValueProperties
   Dim valuePropNames As ICollection = valueProperties.Keys
   Dim valuePropValues As ICollection = valueProperties.Values

   Dim enumPropNames As IEnumerator = valuePropNames.GetEnumerator()
   Dim enumPropVals As IEnumerator = valuePropValues.GetEnumerator()

   Do While enumPropNames.MoveNext And enumPropVals.MoveNext
      Console.WriteLine(enumPropNames.Current + ": " +
            enumPropVals.Current)
   Loop
End Sub
  • + Share This
  • 🔖 Save To Your Account