Automating 3rd party controls can be a hard work. If the control does not support standard mechanisms like windows messages or active accessibility, it cannot be automated without adding some code to the application under test. But how does this trick work?

Using .NET, it’s very simple. You create a new class in the application under test, which inherits from the control class you want to automate and add some properties or methods you need for automation. Be sure that you instantiate the new class in the form designer instead of the old one.

The new version of Ranorex (V1.4 or higher) allows you to get/set properties and call methods of objects from a test application. This technique can be used with all kinds of 3rd party or custom .NET controls.

Please notice that this blog post targets Ranorex 1.X and might be obsolete if you use Ranorex 2.x or newer!”

Let’s demonstrate this approach with a simple sample.

Step 1: Creating a simple application under test.

Create a new Visual Studio C# project

New Visual Studio C# project dialog

Extend the form with a TextBox

Drag the TextBox in the ToolBox and drop it into the form.
Form1

Add a new class to the project and let it inherit from System.Windows.Forms.TextBox (or from the base class of the control you want to automate)

class MyTextBox : System.Windows.Forms.TextBox { }

Add a new property you want to call from your test application to the class.

class MyTextBox : System.Windows.Forms.TextBox
{
    public String MyValue
    {
        get
        {
            return this.Text;
        }
        set
        {
            this.Text = value;
        }
    }
}

Change the class name from TextBox to MyTextBox in the Form.Designer.cs file

(Instantiate the new class instead of the old one)

private MyTextBox textBox1;
private void InitializeComponent()
{
    this.textBox1 = new MyTextBox();
    ...
}

Compile and start the application under test.

Step 2: Checking the properties with RanorexSpyPro.

Start RanorexSpyPro and select the textbox of the form with the finder tool .
You should see the new property “MyValue” in the RanorexSpyPro application.
Dragging RanorexSpyPro to TextBox

Step 3: Automating the control in a test application.

Find the form and set the property MyValue of the TextBox as follows:

Form form = Application.FindFormTitle("Form1");
TextBox textBox = form.FindTextBox("textBox1");
for (int i = 0; i < 20; i++)
{
    textBox.SetPropertyValue("MyValue", i.ToString());
    Application.Sleep(1000);
}

Step 4: Calling a method of the control.

You can also extend your control class “MyTextBox” with methods and call them from the test application:

class MyTextBox : System.Windows.Forms.TextBox
{
    public void SetValue(String text)
    {
         this.Text = text;
    }
}

Use InvokeMethod(“MethodName”, params, …) for calling the method from your test script:

textBox.InvokeMethod("SetValue", “22”);

This article describes a simple but general way for automating 3rd party controls.
Developers and testers can work together to make am application fit for automation.
We have tested this approach with several kind of 3rd party controls (Infragistics, DevExpress, DevComponents,…).

You might also like these articles