Anda di halaman 1dari 7

Tutorial #6 GUI Testing

Spirit Du November 29th, 2007 Ver. 2.1

Contents
About this Document....................................................................................... 1 Ranorex Installation and Setup ...................................................................... 1 Tutorial 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 we perform assertions with NUnit framework. We will integrate this tool with the NUnit framework to test the calculator provided by Microsoft Windows.

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/ In school computers, the Ranorex has been installed, but you need to download the free edition and execute the installer with default settings at home. After installation, copy the file: RanorexCore.dll 1 from [Ranorex Location] 2 \Bin\Net2.0\ and then paste to [Windows] 3 \system32\ as global library cache 4 .
Any project developed with Ranorex will run with RanorexCore.dll The default location is C:\Program Files\Ranorex-1.3 3 The default location for Windows XP/2000 is C:\WinNT\ or C:\Windows 4 It will facilitate the development without denoting the location of RanorexCore.dll in
1 2

- 1-

Tutorial Ranorex
Step 1 Setup environment
Create a new C# Console Application project. In this tutorial, we need add two references into the project: nunit.framework and RanorexNet 5 . Add a class: GUITest into the project.

Figure 1 After adding references

Step 2 Modify the class


You can key in the following codes by yourself or just copy and paste to replace all the existing codes. 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. using System; using System.Collections.Generic; using System.Text; using NUnit.Framework; using Ranorex; namespace Tutorial6 { [TestFixture] public class GUITest { // If both "using System.Windows.Form;" and // "using Ranorex;" are declared in the same file, use the // full declaration with a namespace to avoid name conflicts. Ranorex.Form _testee;

each project. 5 In browser tab, you can find the RanorexNet.dll file in the installation location. - 2-

[STAThread] static void Main(string[] args) {} } }

Step 3 Activate the calculator


Add a member method, setUp(), into the GUITest class, and key in the following codes to activate the calculator provided by Microsoft Windows. You may need to modify the path if the calculator program is not found in C:\Windows\system32\calc.exe. We use the Assert.IsNotNull(_testee) to check whether the application is activated or not. If the calculator is fail to open, the test fail, 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> [SetUp] public void setUp() { // In your homework, the path should look like: bin\\Debug\\ string path = "C:\\Windows\\system32\\"; string application = "calc.exe"; // Sleep 100 milliseconds between each command Ranorex.Application.SleepTime = 100; // Each movement takes 100 milliseconds Ranorex.Mouse.MoveTime = 100; Ranorex.Application.Start(path + application); // Wait 2000ms for the calculator to wake up _testee = Ranorex.Application.FindFormTitle("", SearchMatchMode.MatchExact, true, 2000); Assert.IsNotNull(_testee); }

Step 4 Close the activated windows


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. /// <summary>

- 3-

/// Close the calc.exe /// </summary> [TearDown] public void tearDown() { _testee.Close(); }

Step 5 Find and click the button


Ranorex offers several ways to find GUI elements (controls) on the screen and also offers the control of mouse and keyboard. Now add a member method as below which move the mouse to the specific control and then click on it. /// <summary> /// Find the GUI element by its displayed text and then click on it. /// </summary> /// <param name="text">Displayed text</param> private void clickButton(string text) { Ranorex.Element element = _testee.Element.FindChild(Role.PushButton, text); Assert.IsNotNull(element); Ranorex.Mouse.MoveToElement(element); Ranorex.Mouse.ClickElement(element); }

Step 6 Lower level mouse event


Not only click events can be performed, but also down and up events can be performed. Add these two methods to do pressing left button at the specific location related to the Form location. /// <summary> /// Press at the specific screen location related to the Form /// </summary> private void pressLeftButtonAt(int x, int y) { Mouse.Move(_testee.Location.X + x, _testee.Location.Y + y); Mouse.ButtonDown(MouseButtonType.LeftButton); } /// <summary> /// Release at the specific screen location related to the Form /// </summary> private void releaseLeftButtonAt(int x, int y) {

- 4-

Mouse.Move(_testee.Location.X + x, _testee.Location.Y + y); Mouse.ButtonUp(MouseButtonType.LeftButton); }

Step 7 Write a test script


Now we can write test cases by using the above clickButton() method. Add a new member method: testScript1() with the following codes to automatically perform the users actions. /// <summary> /// Test the calc.exe by the script: 1 + 12 - 3 - 4 * 5 / 6 = /// </summary> [Test] public void testScript1() { // Pre-condition: the testee (calculator) is running now, Assert.IsNotNull(_testee); // and the text displayed on the screen should be 0. Ranorex.Control display = _testee.FindClassName("Edit"); Assert.AreEqual("0. ", display.Text); // Script clickButton("1"); clickButton("+"); clickButton("1"); clickButton("2"); clickButton("-"); clickButton("3"); clickButton("-"); clickButton("4"); clickButton("*"); clickButton("5"); clickButton("/"); clickButton("6"); clickButton("="); // Post-condition: the answer should be 5 with formatting Assert.AreEqual("5. ", display.Text); }

Step 8 Add assertion integrated with NUnit Framework


Here is another test script for testing press and release that may be useful to test your

- 5-

homework: /// <summary> /// Test the mouse pressed and relased function, much lower level event /// </summary> [Test] public void testScript2() { // Pre-condition: three buttons and one text box found Point oneButton = new Point(75, 190); Point addButton = new Point(190, 220); Point equalButton = new Point(230, 220); Ranorex.Control display = _testee.FindClassName("Edit"); // Press and release at "1" button: the same as clicking on "1" button pressLeftButtonAt(oneButton.X, oneButton.Y); releaseLeftButtonAt(oneButton.X, oneButton.Y); // Click (press and release) at "+" button pressLeftButtonAt(addButton.X, addButton.Y); releaseLeftButtonAt(addButton.X, addButton.Y); // Click "1" again pressLeftButtonAt(oneButton.X, oneButton.Y); releaseLeftButtonAt(oneButton.X, oneButton.Y); // Press at "=" button and release at "1" button: nothing happen pressLeftButtonAt(equalButton.X, equalButton.Y); releaseLeftButtonAt(oneButton.X, oneButton.Y); // Press at "+" button and release at "1" button: nothing happen pressLeftButtonAt(equalButton.X, equalButton.Y); releaseLeftButtonAt(equalButton.X, equalButton.Y); Assert.AreEqual("2. ", display.Text); }

Step 9 Run the unit test


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.

- 6-

Figure 2 NUnit activate simple calculator through Ranorex and assert the value.

End Tutorial

- 7-

Anda mungkin juga menyukai