Managing Custom Controls

You can create custom classes for custom controls for which Silk4J does not offer any dedicated support. Creating custom classes offers the following advantages:
  • Better locators for scripts.
  • An easy way to write reusable code for the interaction with the custom control.

Example: Testing the UltraGrid Infragistics control

Suppose that a custom grid control is recognized by Silk4J as the generic class Control. Using the custom control support of Silk4J has the following advantages:
Better object recognition because the custom control class name can be used in a locator.
Many objects might be recognized as Control. The locator requires an index to identify the specific object. For example, the object might be identified by the locator //Control[13]. When you create a custom class for this control, for example the class UltraGrid, you can use the locator //UltraGrid. By creating the custom class, you do not require the high index, which would be a fragile object identifier if the application under test changed.
You can implement reusable playback actions for the control in scripts.

When you are using custom classes, you can encapsulate the behavior for getting the contents of a grid into a method by adding the following code to your custom class, which is the class that gets generated when you specify the custom control in the user interface.

Typically, you can implement the methods in a custom control class in one of the following ways:
  • You can use methods like click, typeKeys, textClick, and textCapture.
  • You can dynamically invoke methods on the object in the AUT.
  • You can dynamically invoke methods that you have added to the AUT. This is the approach that is described in this example.
You can use the following code to call the static method that is defined in the example in Adding Code to the Application Under Test to Test Custom Controls. The method GetContents is added into the generated class UltraGrid.
// Java code
import com.borland.silktest.jtf.Desktop;
import com.borland.silktest.jtf.common.JtfObjectHandle;

public class UltraGrid extends com.borland.silktest.jtf.Control {

  protected UltraGrid(JtfObjectHandle handle, Desktop desktop) {
    super(handle, desktop);
  }

  public List<List<String>> getContents() {
    return (List<List<String>>) invoke("AUTExtensions.UltraGridUtil.GetContents", this);
  }
}
When you define a class as a custom control, you can use the class in the same way in which you can use any built-in class, for example the Dialog class.
// Java code
UltraGrid ultraGrid = mainWindow.find("//UltraGrid[@automationId='my grid']");
List<List<String>> contents = ultraGrid.getContents();