Tutorial 3 - Ranorex V2

9
Windows Programming Tutorials #3 – Ranorex 1 Tutorial #3 – Ranorex Spirit Du Ver. 1.5, 22 nd November, 2007 Ver. 2.3, 29 th October, 2008 Contents About this Document....................................................................................... 1 Tutorial – Ranorex ............................................................................................. 2 Appendix A – Install Ranorex .......................................................................... 6 Appendix B – Activate Program .................................................................... 6 Appendix C – Modal Dialog ........................................................................... 7 Appendix D – Menu Item ................................................................................ 9 About this Document Unit test framework is very useful for testing programs without GUI. However, how do testers test the correctness of GUI interactions? This document introduces an approach: combining the Ranorex tool and unit test framework. Ranorex simulates the user’s interactions and then testers perform assertions with unit test framework after the interactions. This tutorial integrates Ranorex with the unit test framework to test the calculator in Microsoft Windows.

Transcript of Tutorial 3 - Ranorex V2

Page 1: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 1

Tutorial #3 – Ranorex Spirit Du

Ver. 1.5, 22nd November, 2007 Ver. 2.3, 29th October, 2008

Contents About this Document....................................................................................... 1 Tutorial – Ranorex ............................................................................................. 2 Appendix A – Install Ranorex .......................................................................... 6 Appendix B – Activate Program .................................................................... 6 Appendix C – Modal Dialog........................................................................... 7 Appendix D – Menu Item ................................................................................ 9

About this Document Unit test framework is very useful for testing programs without GUI. However, how do testers test the correctness of GUI interactions? This document introduces an approach: combining the Ranorex tool and unit test framework. Ranorex simulates the user’s interactions and then testers perform assertions with unit test framework after the interactions. This tutorial integrates Ranorex with the unit test framework to test the calculator in Microsoft Windows.

Page 2: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 2

Tutorial – Ranorex Step 1 Setup environment Create a new C# Test project. In this tutorial, we need add an additional reference into

the project: RanorexNet. Because the reference is not in the default .Net library

location, change the tab from “.Net” to “Browse”, and find the RanorexNet.dll in

“C:\Program Files\Ranorex-1.1.0\Bin\Net2.0”.

Figure 1 Add a new test project

Page 3: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 3

Figure 2 Add the RanorexNet reference

Step 2 Modify the class This is similar to the previous tutorial; replace all of the automatically generated class

with the following codes. using Microsoft.VisualStudio.TestTools.UnitTesting;

using Ranorex;

namespace GUITesting {

[TestClass]

public class CalculatorGUITests {

Form _testee;

}

}

Step 3 Activate the calculator Add a member method, setUp(), into the class using following codes to activate the

calculator. You may need to modify the path if the calculator is not found in

“C:\Windows\system32\calc.exe.” Here, using the Assert.IsNotNull(_testee) method is

to check whether the application is activated or not. If the calculator is fail to open,

the test fails, too. /// <summary>

/// Initialize the test environment

/// 1. Use the Ranroex library to load the calc.exe

/// 2. Get the displayed form on the screen by its title

/// 3. Set the moving time and sleeping time of the mouse

/// </summary>

[TestInitialize]

public void SetUp() {

string app = "C:\\Windows\\system32\\calc.exe";

Application.SleepTime = 50;

Mouse.MoveTime = 10;

Application.Start(app);

// Wait 2000ms for the calculator waking up

_testee = Application.FindFormTitle("小算盤",

SearchMatchMode.MatchExact, true, 2000);

Assert.IsNotNull(_testee, "Fail to activate the calculator!");

}

Page 4: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 4

Step 4 Close the activated windows In the previous tutorial, there is no 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. /// <summary>

/// Close the activated program

/// </summary>

[TestCleanup]

public void TearDown() {

_testee.Close();

}

Step 5 Find and click the button Ranorex offers several ways to find GUI widgets (controls) on the screen: by the object

name, id, displayed text, or the class name. For more information, please refer the

Ranorex online document. Besides, Ranorex also offers methods to control the mouse

and keyboard. Add a member method which can move the cursor to the specific

control and then click on it. Before clicking, the method also checks that the element

is not found or not with Assert. If the element is not found, it stops running and the test

fails. /// <summary>

/// Find GUI component by its displayed text and then click on it

/// </summary>

/// <param name="text">Displayed text</param>

private void ClickButton(string text) {

Element element =

_testee.Element.FindChild(Role.PushButton, text);

Assert.IsNotNull(element,

"Can't find a button with the specific text: " + text);

Mouse.MoveToElement(element);

Mouse.ClickElement(element);

}

Step 6 Write a test script Now, use the above ClickButton() method to write the test cases. Add a new member

method: runScript() using following codes. This method automatically clicks the button,

Page 5: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 5

just like a user is using the computer. Design your actions sequence for practice if you

have time after finishing the tutorial, or leave the practice in your homework.

/// <summary>

/// Script: 1 + 12 - 3 - 4 * 5 / 6 =

/// </summary>

public void RunScript() {

ClickButton("1");

ClickButton("+");

ClickButton("1");

ClickButton("2");

ClickButton("-");

ClickButton("3");

ClickButton("-");

ClickButton("4");

ClickButton("*");

ClickButton("5");

ClickButton("/");

ClickButton("6");

ClickButton("=");

}

Step 7 Add assertion integrated with unit test framework In order to make sure that the result is correct or not, add a member method with

[TestMethod] annotation as follows: /// <summary>

/// Test the calc.exe by the script

/// </summary>

[TestMethod]

public void TestScript() {

RunScript();

string expected = "5. ";

Control control = _testee.FindClassName("Edit");

Assert.AreEqual(expected, control.Text);

}

Step 8 Run the unit test While the unit test is under running, please do not move your mouse or click any key.

Page 6: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 6

Any event generated by mouse moving or clicking will cause the test failed.

-- The End --

Appendix A – Install Ranorex 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. It can be downloaded from the URL:

http://www.ranorex.com/download/ In school computers, the Ranorex has been installed, but you need

to download the free edition and install it at the home computer. You can download the version 1.1 which offers no limitation of actions; the version higher than version 1.1 only can offer 200 actions at most.

In order to facilitate the development, you can copy the file: RanorexCore.dll from C:\Program Files\Ranorex-1.1.0\Bin\Net2.0\ to C:\Windows \system32\ as a global library.

Appendix B – Activate Program In the tutorial, the program runs executable file from the file system

to activate the calculator. However, the tester can’t verify inner statuses of objects in this way because the calculator and the test program are running in different processes. In order to obtain these statuses, the test program must keep an instance of AUT (application under test). It’s not difficult. In general, the tester can create an instance of program. The trick is that the tester don’t use Application.Run(form) method to display the form. There is a while loop in this method and if the tester uses it, the test process will be blocked. Just call Form.Show() method and the window won’t be closed until the test is finished. Example codes: [TestInitialize]

public void SetUp() {

_testee = new Calculator();

CalculatorView form = new CalculatorView(_testee);

// Don’t use Application.Run(form)

form.Show();

Page 7: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 7

// Wait 2000ms for the calculator to wake up

_form = Ranorex.Application.FindFormTitle("Calculator",

SearchMatchMode.MatchExact, true, 5000);

}

Appendix C – Modal Dialog Besides the Application.Run(Form form) method, a modal dialog

blocks the test process, too. Although multithreading is not the topic in the tutorial, the test process needs a separated thread to keep sending mouse/keyboard events. In the example code, the TestAboutDialogByESC() method create a thread which will find the dialog and then send ESC key event. The FindAboutDialog(string title) tries to find the dialog by the title five times at least, and returns it. Note that the return value may be null anyway. private Ranorex.Form FindAboutDialog(string title) {

Ranorex.Form dialog = null;

int times = 0;

while (dialog == null && times < 5) {

Thread.Sleep(500);

dialog = Ranorex.Application.FindFormTitle(title,

SearchMatchMode.MatchExact, true, 5000);

times++;

}

// The dialog may be null

return dialog;

}

[TestMethod]

public void TestAboutDialogByESC() {

Thread runner = new Thread(new ThreadStart(delegate () {

Form dialog = FindAboutDialog("About the Calculator");

Assert.IsNotNull(dialog);

dialog.SendKeys("{ESC}");

}));

runner.Start();

ClickMenuItem("MainMenu", "Help", "About");

}

Page 8: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 8

Page 9: Tutorial 3 - Ranorex V2

Windows Programming Tutorials #3 – Ranorex 9

Appendix D – Menu Item There may be more than one menu strip in a form. The following

function can be used to click menu items one by one in the specific menu. public void ClickMenuItem(string menuName, params string[] path) {

MenuStrip menubar = _form.FindMenuStrip(menuName);

Element element;

for (int i = 0; i < path.Length; i++) {

element = menubar.Element.FindChild(Role.MenuItem, path[i]);

Mouse.MoveToElement(element);

Mouse.ClickElement(element);

}

}