When you are testing Windows Forms applications or WPF applications, and you want to test complex custom controls or custom controls that you cannot test by simply using the invoke and invokeMethods methods, you can create a static method that interacts with the actual control in the application under test (AUT) and you can add this code to the AUT.
The benefit for you from adding code to the AUT is that the code in the AUT can use regular method calls for interacting with the control, instead of using the reflection-like style of calling methods with the dynamic invoke methods. Therefore you can use code completion and IntelliSense when you are writing you code. You can then call the code in the AUT with a simple invoke call, where you pass the control of interest as a parameter.
This example demonstrates how you can retrieve the content of an UltraGrid control. The UltraGrid control is included in the NETAdvantage for Windows Forms library which is provided by Infragistics. You can download a trial of the library from http://www.infragistics.com/products/windows-forms/downloads.
' VB code Public Class UltraGridUtil Public Shared Function GetContents(ultraGrid As Infragistics.Win.UltraWinGrid.UltraGrid) As List(Of List(Of String)) Dim contents = New List(Of List(Of String)) For Each row In ultraGrid.Rows Dim rowContents = New List(Of String) For Each cell In row.Cells rowContents.Add(cell.Text) Next contents.Add(rowContents) Next Return contents End Function End Class
// C# code using System.Collections.Generic; namespace AUTExtensions { public class UltraGridUtil { 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; } } }
mainWindow.loadAssembly("C:/buildoutput/AUTExtensions.dll");
// Java code Control ultraGrid = mainWindow.find("//Control[@automationId='my grid']"); List<List<String>> contents = (List<List<String>>) mainWindow.invoke("AUTExtensions.UltraGridUtil.GetContents", ultraGrid);