Dynamically Invoking UI Automation Methods

To ensure that the methods supported for a UI Automation control cover the corresponding controls in all supported technologies, the Silk4NET API supports only a subset of the methods and properties available for these controls. To call additional methods and properties that are not available in the Silk4NET API for a control, use dynamic invoke.

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 Silk4NET 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 Silk4NET API.

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

Call multiple dynamic methods on objects with the InvokeMethods 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.Invoke("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.

Supported Parameter Types

The following parameter types are supported:
  • All built-in Silk4NET types.

    Silk4NET types include primitive types, for example boolean, int, and string, lists, and other types, for example 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.

    Pass .NET struct and object parameters 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.

  • Other controls.

    Control parameters can be passed as TestObject.

Supported Methods and Properties

The following methods and properties can be called:

Returned Values

The following values are returned for properties and methods that have a return value:
  • The correct value for all built-in Silk4NET types.
  • All methods that have no return value return null in C# or Nothing in VB.
  • A string for all other types.

    To retrieve this string representation, call the ToString method on returned .NET objects in the application under test.

Example

This example shows how you can call the scrolling methods of a UIADocument control by using dynamic invoke. Silk4NET does not expose these scrolling methods in the API, as these methods are not available for the UIADocument control in all technologies that have implemented UI Automation provider interfaces.

To see which methods and properties are available for the control, you could use code similar to the following:
' VB .NET code
Dim textBox As UIADocument = mainWindow.Find("//UIADocument")
Dim propertyList = textBox.GetPropertyList()
Dim methodList = textBox.GetDynamicMethodList()

For this example, the propertyList that is returned by the GetPropertyList method includes the property ScrollPattern.VerticalScrollPercent. The methodList that is returned by the GetDynamicMethodList method includes the method ScrollPattern.ScrollVertical.

By using dynamic invoke, you can call the method ScrollPattern.ScrollVertical as follows:
textBox.Invoke("ScrollPattern.ScrollVertical", ScrollAmount.SmallIncrement)
Alternatively, you can call the property ScrollPattern.VerticalScrollPercent as follows:
textBox.GetProperty("ScrollPattern.VerticalScrollPercent")