Ranorex Blog

Posted by jherget on Saturday, March 1st, 2008 at 12:01 am to Test Automation

Making 3rd party controls fit for automation

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.

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,…).

Share/Save/Bookmark

6 comments

  1. Torsten Zelger

    Very nice and I am excited to test this, but Ranorex 1.4 cannot be downloaded yet. I know, I am impatient.

  2. mohan

    Hi,

    I have an ActiveX (infragistic ssTree.ocx) control application which is not recognised by Ranorex. This particular treeview control is embedded in VB application. Is it possible to get recognised this control using above approach?

    Thanks in advance,
    Mohan

  3. RichardP

    Hi,
    I’m trying this sample in R2.0.1 and I can’t compile. Application, TextBox, Form.FindTextBox are unknown. I can’t find such classes and methods in Ranorex API.
    Thanks

  4. ahoisl

    The code in this sample is specific to Ranorex V1.X, the API changed with Ranorex 2.0. However, you can adapt the code easily for use with Ranorex 2.X:

    Form form = “/form[@controlname='Form1']“;
    Control textBox = form.FindDescendant(”textBox1″);
    for (int i = 0; i < 20; i++)
    {
    textBox.SetPropertyValue(”MyValue”, i.ToString());
    Delay.Milliseconds(1000);
    }

    Regards,
    Alex

  5. sandrar

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  6. Johannes

    If you want to keep the object identification string in the repository just follow the code below:

    MyRepository myRep = MyRepository.Instance;
    RxPath path = myRep.repFolder1.MyCustomControl.GetPath();
    Control c = path.ToString();
    c.SetPropertyValue(”MyProperty”, 75);

    Here the short version with dynamic object identification:

    Control c = “”/form[@controlname='myApp']//element[@controlname='MyCustomControl']“;
    c.SetPropertyValue(”MyProperty”, 75);

Leave a Reply