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
' VB code mainWindow.LoadAssembly(GetType(UltraGridUtil).Assembly.Location)
'VB code Dim ultraGrid = mainWindow.Control("@automationId='my grid'") Dim contents As IList = mainWindow.Invoke("UltraGridUtil.GetContents", ultraGrid)