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 Silk4J 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 Silk4J 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 Silk4J type DataGrid, you can call all methods that MSDN defines for the type System.Windows.Forms.DataGrid.
//Java code boolean isExpanded = (Boolean) dataGrid.invoke("IsExpanded", 3);
//Java code int result = (Integer) 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")
List<List<String>> contents = 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];
WPFControl dataGrid = mainWindow.find("//WPFControl[@automationId='Custom Data Grid']"); // Get text contents of third cell in first row. int rowIndex = 0; int columnIndex = 2; List<String> methodNames = Arrays.asList("Rows", "get_Item", "Cells", "get_Item", "Text"); List<List<Object>> parameters = Arrays.asList(new ArrayList<Object>(), Arrays.<Object>asList(rowIndex), new ArrayList<Object>(), Arrays.<Object>asList(rowIndex), new ArrayList<Object>()); String cellText = (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:
String cellText = (String) mainWindow.invoke("GetCellText", dataGrid, rowIndex, columnIndex);
For additional information, see Adding Code to the Application Under Test to Test Custom Controls.
Silk4J 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.
Control parameters can be passed or returned as TestObject.