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.

fitnessandranorex

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

Creating Automation Base Library

UPDATED: This example is based on Ranorex 2.2.
To automate and test the VIP 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
{
   public class AddVIPTest : fit.ColumnFixture
   {
	///
	/// UI Repository instance for VIP Application
        ///
	private VIPRepo repo = VIPRepo.Instance;
	private string gender;
	private string lastName;
	private string firstName;
	///
	/// Property for FirstName parameter.
	/// By setting the property Ranorex directly clicks
	/// the text box and simulates the keyboard events
	/// Returns the current text value of the text box.
	///
	public string FirstName
	{
	   set
	       	{
	       		this.firstName = value;
	       		repo.VIPApplication.FirstName.Click();
	       		Ranorex.Keyboard.Press(firstName);
	       	}
	       	get
	       	{
	       		return repo.VIPApplication.FirstName.TextValue;
	       	}
	    }
	    ///
	    /// Property for FirstName parameter.
	    /// By setting the property Ranorex directly clicks
	    /// the text box and simulates the keyboard events
	    /// Returns the current text value of the text box.
	    ///
	    public string LastName
	    {
	       	set
	       	{
	       		this.lastName = value;
	       		repo.VIPApplication.LastName.Click();
	       		Ranorex.Keyboard.Press(lastName);
	       	}
	       	get
	       	{
	       		return repo.VIPApplication.LastName.TextValue;
	       	}
	    }
	    ///
	    /// Property for Gender parameter.
	    /// Depending on the given value Ranorex selects
	    /// the right radio button.
	    /// Returns the currently selected gender
	    ///
	    public string Gender
	    {
	    	set
	    	{
	    		gender = value;
	    		if (gender.Equals("Female"))
	    		    repo.VIPApplication.Gender.Female.Click();
	    		else if (gender.Equals("Male"))
	    			repo.VIPApplication.Gender.Male.Click();
	    	}
	    	get
	    	{
	    		if (repo.VIPApplication.Gender.Female.Checked)
	    			return "Female";
	    		else
	    			return "Male";
	    	}
	    }
	    /// Method is used to simulate a click on
	    /// specified button.
	    ///
	    ///
	    /// Specifies the label of the button to press
	    ///
	    public void Action(string button)
	    {
	    	repo.VIPApplication.Self.FindChild(button).Click();
	    }
	    ///
	    ///
	    /// Returns the current text value of
	    /// the status bar.
	    ///
	    public string ValidateStatusBox()
	    {
	         return repo.VIPApplication.StatusBar.TextValue;
	    }
}

The “AddVIPTest” class is derived from a simple FitNesse fit.ColumnFixture class. All public declared fields within this class are accessible through a FitNesse test case as follows:
!|NetFit.AddVIPTest|
|FirstName|LastName|Gender|Action|ValidateStatusBox?|
|Marylin|Monroe|Female|Add|VIP count: 1|
|Bill|Gates|Male|Add|VIP count: 2|
|Hillary|Clinton|Female|Add|VIP count: 3|


All test data is specified in columns and rows, and “FirstName”, “LastName”, “Gender”, and “Action” are declared as test input data. Read test output data by adding the question mark ‘?’ to the “ValidateStatusBox” column. The last column in our example is used to call our “ValidateStatusBox” 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/AddVipTest in the address field to create a new FitNesse test called AddVIPTest.

Specifying test

Edit the page and define the command patterns, the test runner and the Ranorex based .NET library as follows:

!!define COMMAND_PATTERN {%m -r fitnesse.fitserver.FitServer,C:RanorexFitnessedotnet2fit.dll, -c C:RanorexFitnessedotnet2myConfig.xml %p}
!define TEST_RUNNER {C:RanorexFitnessedotnet2Runner.exe}
!path C:RanorexFitnesseHelloWorldHelloWorldbinDebugNetFit.dll

!|NetFit.AddVIPTest|
|FirstName|LastName|Gender|Action|ValidateStatusBox?|
|Marylin|Monroe|Female|Add|VIP count: 1|
|Bill|Gates|Male|Add|VIP count: 2|
|Hillary|Clinton|Female|Add|VIP count: 3|

Save the “AddVipTest” page. To enable a “Test” button, set up the page properties within the properties menu.
Now you can start your FitNesse-managed Ranorex automated test script.

addviptestexecuted

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.

You might also like these articles