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.
control.Invoke("SetTitle", "my new title")
For an object of the Silk4NET type DataGrid, you can call all methods that MSDN defines for the type System.Windows.Forms.DataGrid.
//VB .NET code Dim isExpanded As Boolean = dataGrid.Invoke("IsExpanded", 3)
//C# code bool isExpanded = (bool) dataGrid.Invoke("IsExpanded", 3);
//VB .NET code Dim result as Integer = (Integer) mainWindow.Invoke("System.String.Compare", "a", "b")
//C# code int result = (int) mainWindow.Invoke("System.String.Compare", "a", "b");
This example shows how you can dynamically invoke the user-generated method GetContents.
You can write code which you can use to interact with a control in the application under test (AUT), in this example an UltraGrid. Instead of creating complex dynamic invoke calls to retrieve the contents of the UltraGrid, you can generate a new method GetContents and then just dynamically invoke the new method.
//C# code, because this is code in the AUT namespace UltraGridExtensions { public class UltraGridUtil { /// <summary> /// Retrieves the contents of an UltraGrid as nested list /// </summary> /// <param name="grid"></param> /// <returns></returns> public static List<List<string>> GetContents(Infragistics.Win.UltraWinGrid.UltraGrid grid) { var result = new List<List<string>>(); foreach (var row in grid.Rows) { var rowContent = new List<string>(); foreach (var cell in row.Cells) { rowContent.Add(cell.Text); } result.Add(rowContent); } return result; } } }
FormsWindow.LoadAssembly(String assemblyFileName)You can load the assembly by using the full path, for example:
mainWindow.LoadAssembly("C:/temp/ultraGridExtensions.dll")
//VB.NET code Dim assemblyLocation = GetType(UltraGridExtensions.UltraGridUtil).Assembly.Location mainWindow.LoadAssembly(assemblyLocation)
//C# code string assemblyLocation = typeof(UltraGridExtensions.UltraGridUtil).Assembly.Location; mainWindow.LoadAssembly(assemblyLocation);
var contents = (IList) mainWindow.Invoke("UltraGridExtensions.UltraGridUtil.GetContents", ultraGrid);
The mainWindow object, on which the Invoke method is called, only identifies the AUT and can be replaced by any other object in the AUT.
string cellText = dataGrid.Rows[rowIndex].Cells[columnIndex].Text;
string cellText = dataGrid.Rows[0].Cells[2];
// C# code var dataGrid = mainWindow.WPFControl("@automationId='Custom Data Grid'"); // Get text contents of third cell in first row. var rowIndex = 0; var columnIndex = 2; var methodNames = new List<string>(); methodNames.Add("Rows"); // Get the list of rows from the grid. methodNames.Add("get_Item"); // Get a specific row from the list of rows by using the indexer method. methodNames.Add("Cells"); // Get the list of cells from the the row. methodNames.Add("get_Item"); // Get a specific cell from the list of cells by using the indexer method. methodNames.Add("Text"); // Get the text of the cell. var parameters = new List<List<object>>(); parameters.Add(new List<object>()); // Parameters for the Rows property. parameters.Add(new List<object>() { rowIndex }); // Parameters for the get_Item method. parameters.Add(new List<object>()); // Parameters for the Cells property. parameters.Add(new List<object>() { columnIndex }); // Parameters for the get_Item method. parameters.Add(new List<object>()); // Parameters for the Text property. string cellText = (string)dataGrid.InvokeMethods(methodNames, parameters);
' VB .NET code Dim dataGrid = mainWindow.WPFControl("@automationId='Custom Data Grid'") ' Get text contents of third cell in first row. Dim rowIndex = 0 Dim column = 2 Dim methodNames = New List(Of String)() methodNames.Add("Rows") ' Get the list of rows from the grid. methodNames.Add("get_Item") ' Get a specific row from the list of rows by using the indexer method. methodNames.Add("Cells") ' Get the list of cells from the the row. methodNames.Add("get_Item") ' Get a specific cell from the list of cells by using the indexer method. methodNames.Add("Text") ' Get the text of the cell. Dim parameters = New List(Of List(Of Object))() parameters.Add(New List(Of Object)()) ' Parameters for the Rows property. parameters.Add(New List(Of Object) From {rowIndex}) ' Parameters for the get_Item method. parameters.Add(New List(Of Object)()) ' Parameters for the Cells property. parameters.Add(New List(Of Object) From {columnIndex}) ' Parameters for the get_Item method. parameters.Add(New List(Of Object)()) ' Parameters for the Text property. Dim cellText As String = dataGrid.InvokeMethods(methodNames, parameters)
// C# code, if the AUT is implemented in C#. public static string GetCellText(Infragistics.Win.UltraWinGrid.UltraGrid dataGrid, int rowIndex, int columnIndex) { return dataGrid.Rows[rowIndex].Cells[columnIndex].Text;
' VB code, if the AUT is implemented in VB. public static string GetCellText(Infragistics.Win.UltraWinGrid.UltraGrid dataGrid, int rowIndex, int columnIndex) { return dataGrid.Rows[rowIndex].Cells[columnIndex].Text;To get the text contents of the cell, dynamically invoke the GetCellText method from your test script:
// C# code string cellText = (string) mainWindow.Invoke("GetCellText", dataGrid, rowIndex, columnIndex);
'VB .NET code Dim cellText As String = mainWindow.Invoke("GetCellText", dataGrid, rowIndex, columnIndex)
For additional information, see Adding Code to the Application Under Test to Test Custom Controls.
Silk4NET types includes primitive types (such as boolean, int, string), lists, and other types (such as Point and Rect).
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 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.
WPF control parameters can be passed as TestObject.
Call ToString on returned .NET objects to retrieve the string representation
public void Reset() public int Add(int number1, int number2) public System.Windows.Vector StrechVector(System.Windows.Vector vector, double factor) public String Description { get;}The tester can call the methods directly from his test. For example:
customControl.Invoke("Reset") Dim sum as Integer = customControl.Invoke("Add", 1, 2) ' the vector can be passed as list of integer Dim vector = New List(Of Integer) vector.Add(3) vector.Add(4) ' returns "6;8" because this is the string representation of the .NET object Dim strechedVector As String = customControl.Invoke("StrechVector", vector, 2.0) Dim description As String = customControl.GetProperty("Description")