Ranorex Blog

Posted by cpreschern on Thursday, January 3rd, 2008 at 11:50 am to Test Management

Manage test data and execute automated test scenarios with FitNesse and Ranorex

Writing test scripts is more time consuming than maintaining already existing test scripts. One way to minimize the time needed to implement test scripts is to keep test data separated from test code. The less test data the test code contains, the more reusable it is. Another advantage of separating test data from test code is that testers – in most cases non-programmers – are able to generate and execute tests or test suits without thorough the deeper knowledge of test script implementation.

The following example describes how FitNesse and Ranorex can be combined to automate GUI testing without having static test scripts. Moreover, it gives a quick overview as to how to manage, maintain and execute test scripts using FitNesse.

ranorxnfitnesseoverview.png

First, download and install FitNesse from http://fitnesse.org/FitNesse.DownLoad. More information about installing and getting started with FitNesse is provided by the following document written by Gojko Adzic: http://gojko.net/FitNesse/fitnesse.pdf. He describes how one implements and executes .NET test cases using FitNesse and demonstrates how one can create human-readable test cases.

Creating Calculator Automation Base Library

To automate and test the Calculator application with FitNesse it is necessary to implement a .NET fixture class which does all the GUI automation work.

using System;
using System.Collections.Generic;
using System.Text;
using Ranorex;

namespace NetFit
{
    /// 
    /// .NET fixture to enable tests with fitnesse
    /// 
    public class Calculator : fit.ColumnFixture
    {
        public Ranorex.Form form;

        // Public properties for test data input
        public string Number1;
        public string Number2;
        public string Operation;

        /// 
        /// Finds “Calculator” form using Ranorex
        /// 
        public Calculator()
        {
            form = Application.FindFormTitle(”Calculator”);
            form.Activate();
            Ranorex.Application.Sleep(1000);
            Ranorex.Application.ErrorAsException = true;
            // Speed up automation
            Ranorex.Mouse.MoveTime = 10;
            Ranorex.Application.SleepTime = 50;
        }
        /// 
        /// Automates a calculation depending on the properties
        /// Number1, Number2 and Operation
        /// 
        /// Returns the calculators output as string.
        [STAThread]
        public string Calculate()
        {
            Ranorex.Control bt;
            // Automates neccessary mouse clicks
            // to input Number1
            foreach (char c in Number1)
            {
                bt = form.FindChildText(c.ToString());
                Ranorex.Mouse.ClickControl(bt);
            }

            // Clicks on specified operation button
            bt = form.FindChildText(Operation);
            Ranorex.Mouse.ClickControl(bt);

            // Automates neccessary mouse clicks
            // to input Number2
            foreach (char c in Number2)
            {
                bt = form.FindChildText(c.ToString());
                Ranorex.Mouse.ClickControl(bt);
            }              

            // Clicks on “=” to finish calculation
            bt = form.FindChildText(”=”);
            Ranorex.Mouse.ClickControl(bt);

            Ranorex.Application.Sleep(100);

            // Reads text out of the Calculator TextBox
            Ranorex.Control textBox = form.FindControlId(403);
            return textBox.Text.Substring(0, textBox.Text.Length - 2);
        }
    }
}

The “Calculator” class is derived from a simple FitNesse fit.ColumnFixture class. All public declared fields within this class (”Number1″,”Number2″,”Operation” and “Calculate”) are accessible through a FitNesse test case as follows:

 

!|NetFit.Calculator|
|Number1|Operation|Number2|Calculate?|
|40|+|2|42|
|18|-|3|15|
|100|*|4|400|

All test data is specified in columns and rows, and “Number1”, “Operation” and “Number2” are declared as test input data. Read test output data by adding the question mark ‘?’ to the “Calculate” column. The last column in our example is used to call our “Calculator” method and to compare the returned value with an expected value.

FitNesse uses so called “Test Pages” to execute test tables as described above. The next steps describe how to create “Test Pages” with FitNesse using its web application server.

After downloading and installing FitNesse, just start “run.bat”. Start your preferred browser and enter http://localhost/CalcTest in the address field to create a new FitNesse test called CalcTest.
createcalctest.png

Click on the link “create this page” in order to specify your test scenario as follows:

!define COMMAND_PATTERN {%m %p}
!define TEST_RUNNER { ..\fitnesse\FitNesseRoot\files\Net20\FitFdn\FitServer.exe}
!path ..\ NetFit\bin\Debug\netfit.dll

!|NetFit.Calculator|
|Number1|Operation|Number2|Calculate?|
|40|+|2|42|
|18|-|3|15|
|100|*|4|400|

 

Save the “CalcTest” page. To enable a “Test” button, set up the page properties as shown below: setuppageproperties.png

 

Now you can start your FitNesse-managed Ranorex automated test script.

runcalctest.png

Each test case should be passed successfully. Try to force an error by changing one of the expected values. Restart the test and see what happens.

Manage Test Suites with FitNesse

FitNesse is a Wiki-based web application and allows one to create different types of pages. Test suites are used to group single test scripts and to run single tests in series. The following steps show how to create a suite with test cases for each operation type for the calculator application.

Create a new suite called “CalculatorTest” by creating a new page. Enter http://localhost/CalculatorTest into the address field of your browser. Use the “CalculatorTest” suite to setup required environment variables for all test cases within the suite.

!define COMMAND_PATTERN {%m %p}
!define TEST_RUNNER { ..\fitnesse\FitNesseRoot\files\Net20\FitFdn\FitServer.exe}
!path ..\ NetFit\bin\Debug\netfit.dll
!contents -R

suiteproperty.png

Save the page content and mark the page as suite by enabling the “Suite” checkbox within the property page.

To append a single test to the “CalculatorTest” suite case which automates and verifies the calculation of different summation operations, enter http://localhost/CalculatorTest.SummationTest.

Create the page by clicking again on the link and add a test table to test the summation functionality of the calculator.

!|NetFit.Calculator|
|Number1|Operation|Number2|Calculate?|
|5|+|5|10|
|80|+|3|83|
|2|+|6|8|

Do the same procedure for all other operation types (substraction, multiplication and division) and create specific test pages. Don’t forget to enable each page as a “Test” page. After that your CalculatorTest suite page should look like this:

suiteoverview.png

Start your suite test by clicking the “Suite” button at the left site of the page.

suitetestresult.png

Depending on your expected calculation results (specified within your test tables), you’ll get a positive or negative test result.

Start automated tests from command line

To start FitNesse tests cases automated via command line, call “TestRunner.exe” located at ..\fitnesse\FitNesseRoot\files\Net20\FitFdn. Please visit the official FitNesse documentation site to get more information about command line test execution:
http://fitnesse.org/FitNesse.DotNet.DotNetTestRunner

TestRunner.exe localhost 80 CalculatorTest

Use the “-results” option to generate log information:

TestRunner.exe -results resultFile.dat localhost 80 CalculatorTest

Use the FormattingOption class to store generated results after test execution in HTML format:

java -cp ..\..\fitnesse.jar fitnesse.runner.FormattingOption resultFile.dat html testResult.html localhost 80 CalculatorTest

2 comments

  1. Jason Galyon Says:

    Thanks for this informative post. I am currently taking a similar approach with my Ranorex tests.

    I work with the Python tests and manually am replacing values with variables (var1 … varN) and have created a framework to feed these variables in at runtime. By tracking the input to each run I am able to verify in the database that the data was entered correctly thus providing another level of validation.

    Further reuse (re) organization of the scripts come from the concept of using them as “scriptlets” that are pieced together like legos to create tests. Reuseability is very high now.

    I have been wanting to further integrate with FitNesse and this provides the perfect starting point.

    Thanks again,
    Jason

  2. narasareddy Says:

    please send me details about ranorex

Leave a Reply