1. Ranorex Tutorial
Spirit Du
March 27th, 2007 Ver. 1.1.4
Contents
About this Document....................................................................................... 1
Ranorex Installation and Setup ...................................................................... 1
Tutorial – Test SimpleCalculator using Ranorex............................................ 2
About this Document
NUnit framework is very useful for testing a small piece of program
without GUI. But how do we test the correctness of GUI interactions? This
document introduces a tool Ranorex, which can “simulate” user
interactions and perform assertions. We will integrate this tool with the
NUnit framework to test the SimpleCalculator developed in your
homework.
Ranorex Installation and Setup
Ranorex is a Windows GUI test and automation Library for C++,
Python, and the .Net languages. The user (e.g. the software tester)
should use the functionalities of the programming languages like Python
or C# as a base, and enlarge it with the GUI automation functionality of
Ranorex. Before starting this tutorial, it needs to get the Ranorex installer
form the URL:
http://www.ranorex.com/download/
Download the free edition and execute the installer with default
settings. After installation 1 , copy the file: RanorexCore.dll 2 from [Ranorex
Location] 3 BinNet2.0 and then paste to [Windows] 4 system32 as
global library cache 5 .
1 You don’t have to install the Ranorex. But you do need to do this set up step by
yourself because there is no such file in system32 folder in the computer lab.
2 Any project developed with Ranorex will run with RanorexCore.dll
3 The default location is C:Program FilesRanorex-1.1.0
4 The default location for Windows XP/2000 is C:WinNT
5 It will facilitate the development without denoting the location of RanorexCore.dll in
each project.
Windows Programming Tutorial – Ranorex - 1-
2. Tutorial – Test SimpleCalculator using Ranorex
Step1.Open the existing Visual Studio Solution which contains your
calculator.
As shown in Figure 1, SimpleCalculator is the project developed in the previous
homework which is contained in Windows Programming solution.
Figure 1 The Solution contains SimpleCalculator project
Step2.Add a class: GUITester into existing project.
Figure 2 Add a new class: GUITester
Step3.Add RanorexNet references 6
Figure 3 Add Ranorex reference
Step4.Add using declarations
Open the GUITester.cs and add the following two declarations.
6 Default location: C:Program FilesRanorex-1.1.0BinNet2.0
Windows Programming Tutorial – Ranorex - 2-
3. using NUnit.Framework;
using Ranorex;
Step5.Declare a test fixture and a member data
This is similar to the previous tutorial; add a test fixture attribute before the class
declaration. Then, change the visibility to public and add a new field: _testee.
namespace SimpleCalculatorGUITests {
[TestFixture]
public class GUITester {
Form _testee;
}
}
Step6.Use the Ranorex to activate SimpleCalculator in setup method 7 .
Add a member method, setup(), into GUITester class. Note that the [SetUp]
attribute and the value of application string must be the same as the title
(name of the .exe file) of your simple calculator.
[SetUp]
public void setUp() {
// Project Name or executable file name
string application = "SimpleCalculator";
Application.SleepTime = 50;
Mouse.MoveTime = 10;
Application.Start(application);
_testee = Application.FindFormTitle(application,
SearchMatchMode.MatchExact, true, 2000);
Assert.IsTrue(_testee != null);
}
Step7.Add a tear down member method to close the activated form
In the previous tutorial, it is not necessary to declare the tear down method,
because garbage collection is automatically performed. But in this tutorial, a
tear down method is required to close the application (form) activated by the
set up method.
7The assertion failed in setup() that will skip the runTest() and then call teardown()
directly.
Windows Programming Tutorial – Ranorex - 3-
4. [TearDown]
public void tearDown() {
_testee.Close();
}
Step8.Add a member method to control the mouse 8
Ranorex offers several ways to find 9 GUI widgets (controls) on the screen and
also offers the control of mouse and keyboard. Now add a member method as
below which can move the mouse pointer to the target control and then click
on it.
public void moveToControlAndClick(Form form, string text) {
Assert.IsTrue(form != null);
Control control = form.FindChildText(text);
Assert.IsTrue(control != null);
Mouse.MoveToControl(control);
Mouse.ClickControl(control);
}
Step9.Write a test script
Now we can write test cases by using the above moveToControlAndClick
method. Add a new member method with the following codes to
automatically perform the user’s actions 10 : 1+12-3-4*5/6=
8 The same as the setUp() method, if there is any assertion failed in the
moveToControlAndClick() method, the NUnit will skip the remaining codes and then
call tearDown() directly.
9 Ranorex provides several methods: by object name or id, text and class name. For
more information, please refer to the Ranorex document.
10 Design your actions sequence for practice if you have more time after finishing the
tutorial
Windows Programming Tutorial – Ranorex - 4-
5. public void runScripts() {
moveToControlAndClick(_testee, "1");
moveToControlAndClick(_testee, "+");
moveToControlAndClick(_testee, "1");
moveToControlAndClick(_testee, "2");
moveToControlAndClick(_testee, "-");
moveToControlAndClick(_testee, "3");
moveToControlAndClick(_testee, "-");
moveToControlAndClick(_testee, "4");
moveToControlAndClick(_testee, "*");
moveToControlAndClick(_testee, "5");
moveToControlAndClick(_testee, "/");
moveToControlAndClick(_testee, "6");
moveToControlAndClick(_testee, "=");
}
Step10. Add assertion integrated with NUnit Framework
Now we would like to make sure the result of the previous calculation is 5. Add
a member method with [Test] attribute (so that it can be identified by UnitRun
add-in) as follows:
[Test]
public void runTest() {
Assert.IsTrue(_testee != null);
runScripts();
Control control = _testee.FindControlName("_resultDisplayer");
Assert.IsTrue(control != null);
Assert.AreEqual(control.Text, "5.");
}
Step11. Run the unit test through UnitRun add-in
While the unit test is under running, please do not move your mouse or click
any key. Any event generated by mouse moving or clicking will cause the test
failed.
Windows Programming Tutorial – Ranorex - 5-
6. Figure 4 Run unit test through UnitRun add-in.
Figure 5 NUnit activate simple calculator through Ranorex and assert the value.
– End Tutorial –
Windows Programming Tutorial – Ranorex - 6-