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. Open Microsoft Visual Studio and create a new class library project in C# or VB .NET. Call the new project AUTExtensions.
    Note: The class library should use the same .NET version as the AUT.
  2. Add references to the required dependencies to the project. 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. In the AUTExtensions project, create the new class UltraGridUtil with the following content:
      ' 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;
          }
      
        }
      
      }
      Note: The Shared modifier makes the GetContents method a static method.
  3. Build the AUTExtensions project.
  4. Load the assembly into the AUT during playback.
    • Open an existing test script or create a new test script.
    • Add code to the test script to load the assembly that you have built from the file system. For example:
      mainWindow.loadAssembly("C:/buildoutput/AUTExtensions.dll");
  5. Call the static method of the injected code in order to get the contents of the UltraGrid:
    // Java code
    Control ultraGrid = mainWindow.find("//Control[@automationId='my grid']");
    List<List<String>> contents = (List<List<String>>) mainWindow.invoke("AUTExtensions.UltraGridUtil.GetContents", ultraGrid);