Dynamically Invoking WPF Methods

Dynamic invoke enables you to directly call methods, retrieve properties, or set properties, on an actual instance of a control in the application under test. You can also call methods and properties that are not available in the Silk Test Classic API for this control. Dynamic invoke is especially useful when you are working with custom controls, where the required functionality for interacting with the control is not exposed through the Silk Test Classic API.

Call dynamic methods on objects with the DynamicInvoke method. To retrieve a list of supported dynamic methods for a control, use the GetDynamicMethodList method.

Call multiple dynamic methods on objects with the DynamicInvokeMethods method. To retrieve a list of supported dynamic methods for a control, use the GetDynamicMethodList method.

Retrieve dynamic properties with the GetProperty method and set dynamic properties with the SetProperty method. To retrieve a list of supported dynamic properties for a control, use the GetPropertyList method.

For example, to call a method named SetTitle, which requires the title to be set as an input parameter of type string, on an actual instance of a control in the application under test, type the following:
control.DynamicInvoke("SetTitle", {"my new title"})
Note: Typically, most properties are read-only and cannot be set.
Note: Reflection is used in most technology domains to call methods and retrieve properties.

The DynamicInvoke Method

For a Windows Forms or a WPF control, you can use the DynamicInvoke method to call the following methods:
  • Public methods that the MSDN defines for the control.
  • Public static methods that the MSDN defines.
  • User-defined public static methods of any type.

First Example for the DynamicInvoke Method

For an object of the Silk Test Classic type DataGrid, you can call all methods that MSDN defines for the type System.Windows.Forms.DataGrid.

To call the method IsExpanded of the System.Windows.Forms.DataGrid class, use the following code:
//4Test code
BOOLEAN isExpanded = (BOOLEAN) dataGrid.DynamicInvoke("IsExpanded", {3})

Second Example for the DynamicInvoke Method

To invoke the static method String.Compare(String s1, String s2) inside the AUT, use the following code:
//4Test code
INTEGER result = mainWindow.DynamicInvoke("System.String.Compare", {"a", "b"});

The DynamicInvokeMethods Method

For a Windows Forms or a WPF control, you can use the DynamicInvokeMethods method to invoke a sequence of nested methods. You can call the following methods:
  • Public methods that the MSDN defines for the control.
  • Public static methods that the MSDN defines.
  • User-defined public static methods of any type.

Example: Getting the Text Contents of a Cell in a Custom Data Grid

To get the text contents of a cell of a custom data grid from the Infragistics library, you can use the following C# code in the AUT:
string cellText = dataGrid.Rows[rowIndex].Cells[columnIndex].Text;
The following C# code sample gets the text contents of the third cell in the first row:
string cellText = dataGrid.Rows[0].Cells[2];
Scripting the same example by using the DynamicInvokeMethods method generates a relatively complex script, because you have to pass five methods with their corresponding parameters to the DynamicInvokeMethods method:
INTEGER rowIndex = 0
INTEGER columnIndex = 2

LIST OF STRING names = { ... }
  "Rows"         // Get the list of rows from the grid.
  "get_Item"     // Get a specific row from the list of rows by using the indexer method.
  "Cells"        // Get the list of cells from the the row.
  "get_Item"     // Get a specific cell from the list of cells by using the indexer method.
  "Text"         // Get the text of the cell.
 
LIST OF LIST parameters = { ... }
  {}             // Parameters for the Rows property.
  {rowIndex}     // Parameters for the get_Item method.
  {}             // Parameters for the Cells property.
  {columnIndex}  // Parameters for the get_Item method.
  {}             // Parameters for the Text property.
  
dataGrid.DynamicInvokeMethods(names, parameters)

Supported Methods and Properties

The following methods and properties can be called:
  • Methods and properties that Silk Test Classic supports for the control.
  • All public methods and properties that the MSDN defines for the control.
  • If the control is a custom control that is derived from a standard control, all methods and properties from the standard control can be called.

Supported Parameter Types

The following parameter types are supported:
  • All built-in Silk Test Classic types

    Silk Test Classic types includes primitive types (such as boolean, int, string), lists, and other types (such as Point and Rect).

  • Enum types

    Enum parameters must be passed as string. The string must match the name of an enum value. For example, if the method expects a parameter of the .NET enum type System.Windows.Visiblity you can use the string values of Visible, Hidden, or Collapsed.

  • .NET structs and objects

    .NET struct and object parameters must be passed as a list. The elements in the list must match one constructor for the .NET object in the test application. For example, if the method expects a parameter of the .NET type System.Windows.Vector, you can pass a list with two integers. This works because the System.Windows.Vector type has a constructor with two integer arguments.

Returned Values

The following values are returned for properties and methods that have a return value:
  • The correct value for all built-in Silk Test Classic types. These types are listed in the Supported Parameter Types section.
  • All methods that have no return value return NULL.
  • A string for all other types

    Call ToString on returned .NET objects to retrieve the string representation

Example

A custom calculator control has a Reset method and an Add method, which performs an addition of two numbers. You can use the following code to call the methods directly from your tests:
customControl.DynamicInvoke("Reset")
REAL sum = customControl.DynamicInvoke("Add",{1,2})
The calculator control also has a LastCalculationResult property. You can use the following code to read the property:
REAL lastResult =
customControl.GetProperty("LastCalculationResult")