Adding Code to the Application Under Test to Test Custom Controls

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.

You can add code to the AUT in the following ways:
  • Compile the code into the AUT. The implementation is simple, but you will be changing the AUT, which you might not want to do.
  • Inject code to the AUT at runtime by using the LoadAssembly method in a test script. This requires more effort than compiling the code into the AUT, but the injected code will be located close to the test code. The LoadAssembly method is available for the classes WPFWindow and FormsWindow.

Example: Testing the UltraGrid Infragistics control

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.

To create the UltraGridUtil class, perform the following actions:
  1. Create a new test script. Call the new script AUTExtensions.
    Note: Your AUT must use .NET version 4 or later. When you compile the .NET script a .NET 4.0 assembly is created and this assembly can only be loaded into an AUT that uses the same or a later .NET version. If your AUT uses a .NET version prior to 4.0, use Visual Studio to create an class library project with the UltraGridUtil class.
  2. Add references to the required dependencies to the script. For example, for Infragistics version 12.2 you need to reference the following assemblies:
    • Infragistics4.Shared.v12.2
    • Infragistics4.Win.UltraWinGrid.v12.2
    • Infragistics4.Win.v12.2
    If you are not sure which version of Infragistics is used in your AUT you can use the Process Explorer tool from Microsoft to see which assemblies are loaded in your AUT.
    1. Replace the content of the AUTExtensions script with the following code:
      ' 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
      Note: The Shared modifier makes the GetContents method a static method.
  3. Load the assembly into the AUT during playback.
    • Open an existing test script or create a new test script.
    • Add a reference to the AUTExtensions script.
    • Add the following code to your test script:
      ' VB code
      mainWindow.LoadAssembly(GetType(UltraGridUtil).Assembly.Location)
      Note: Silk Test Workbench compiles every script into a separate .NET assembly.
  4. Call the static method of the injected code in order to get the contents of the UltraGrid:
    'VB code
    Dim ultraGrid = mainWindow.Control("@automationId='my grid'")
    Dim contents As IList = mainWindow.Invoke("UltraGridUtil.GetContents", ultraGrid)