Ranorex

English|Deutsch

Quick Start

print friendly page
Before you can start automating GUI elements, you have to learn how to identify them. In the following sample you will learn how to automate the windows calculator. Our Test Case will:
  • Start and activate the calculator
  • Click on a button
  • Check the result

Analysing the Calculator Application

To automate controls in a Windows application, the main form has to be found first. For this we take the RanorexSpy (tool for analysing applications and their controls). With the help of RanorexSpy we can get the information we need to write GUI automation scripts. An application can be found by its:
    1. Title
    2. Class name
    To start and find the automated application by its title, use following code:
    C#
    Application.Start("calc.exe");
    Form form = Application.FindFormTitle("Calculator");
    Python
    Ranorex.ApplicationStart('calc.exe')
    form = Ranorex.FormFindTitle('Calculator')
    cpp
    RxApplicationStart("calc.exe");
    HWND form = RxFormFindTitle("Calculator");

    Automating a button

    There are several ways to automate controls. You can find controls with the control approach or with the element approach. Controls like the treeview have child elements. To find these elements you can use both the control or the element approach.

    Automating with the control approach

    To use the control functionality, a control must be uniquely identified. The control properties are shown in the Control group of RanorexSpy.
    A control can be found by its:
    1. Text
    2. Class name
    3. Control name
    4. Control ID
    You can find several controls by the class name "Button" in the Calculator GUI. So we wouldn't be able to identify a button by its class name. We have to use the caption or control ID to find a distinct control. Automated clicking of a button by its caption:
    C#
    Control button2 = form.FindChildText("2");
    Mouse.ClickControl(button2);
    Python
    button2=Ranorex.FormFindChildText(form,'2',Ranorex.MATCH_EXACT)
    Ranorex.ButtonClick(button2)
    cpp
    HWND button2 = RxFormFindChildText(form, "2", MATCH_EXACT);
    RxMouseClickControl(button2);

    Automating with the element approach

    With the element functionality almost every GUI control can be automated. Use the properties in the Element group (in the RanorexSpy) to find GUI elements.
    The following code shows how to find and click on a GUI control with the Ranorex element functions:
    C#
    Element button2 = form.Element.FindChild(Role.PushButton, "2");
    Mouse.ClickElement(button2);
    Python
    element = Ranorex.ControlGetElement(form)
    button2 = Ranorex.ElementFindChild(element, Ranorex.ROLE_SYSTEM_PUSHBUTTON, '2')
    Ranorex.MouseClickElement(button2)
    cpp
    ElementStruct element;
    RxControlGetElement(form, &element);
    ElementStruct button2;
    RxElementFindChild(&element, ROLE_SYSTEM_PUSHBUTTON, "2", NULL, &button2);
    RxMouseClickElement(&button2);

    Verifying the results

    The purpose of automated tests is to verify that a program works correctly and gives the right results. After performing some GUI actions we would like to check the results. In the last few steps, we learned how to click on the button "2". To check that the Calculator does this, we read the value of the TextBox.
    The following code reads the value of the TextBox:
    C#
    Control textBox = form.FindControlId(403);
    if (textBox.Text != "2, ")
        return 1;
     
    return 0;
    Python
    textBox = Ranorex.FormFindChildControlId(form, 403)
    value = Ranorex.ControlGetText(textBox)
    if value != "2, ":
            return 1
     
    return 0
    cpp
    HWND textBox = RxFormFindChildControlId(form, 403);
    char value[256];
    RxControlGetText(textBox, value, 256);
    if( strcmp(value , "2, ") != 0)
    	return 1;
     
    return 0;