English|Deutsch
Subscribe Ranorex Announcements Feed Ranorex LinkedIn Ranorex twitter Ranorex Facebook

Data Driven Testing

This example illustrates how to integrate Data-Driven Testing into Ranorex recordings. The application under test is a simple application where you can add contacts.

In this tutorial you will create a simple recording and perform the same operations with multiple sets of data. You will use a data table (Excel CSV) with a set of parameters so that you can run your recording several times using a different set of data.

The Ranorex Data Driven Test Sample for C#, VB.NET and IronPython is shipped with the Ranorex setup package. You can find it using the link on the Ranorex Studio startpage or by navigating to "Ranorex Installation Folder\Samples\DataDrivenTest".

Record test case and parameterize

1. Open the application under test

In this tutorial the VIP-Application is our application under test. The VIP-Application (VIPApplication.exe) is just a simple example, where you can load, add or delete your data.

Application under test (VIPApplication.exe)
Application under test (VIPApplication.exe)

2. Create a new Ranorex Studio C# project

Start Ranorex Studio, choose "File" -> "New" -> "Solution..." and create a “Ranorex C# Test Automation” project.

3. Record your test case

On the Projects tab double-click ‘Recording1.rxrec’ to open the Recorder view.

On the Recorder View click the Record button. The Ranorex Recorder is minimized.

You can now begin to record some action on the VIP Application like this:

  • Click the First Name textbox and write 'John'.
  • Click the Last Name textbox and write 'Miller'.
  • Click the 'Add' Button.

Stop the recording by clicking the Stop button. The Recorder view should be visible then.

4.  Edit recorded actions

With some simple modifications you can change the recording to be data-driven:
  • Right-click on the second row (Key Sequence) and choose 'Convert to User Code Item'.
    On the right side you can see Properties Tab sheet, change the Method Name form 'Key_Sequence1' to 'SetFirstName'.
  • Right-click on the fourth row (Key Sequence) and choose  'Convert to User Code Item'.
    Change the Method Name form 'Key_Sequence2' to 'SetLastName'. 

5. Adding Properties to the recording's user code

Now we implement properties in our recording class to make it able to have parameters assigned.

  • Open the Recording1 user code by right-clicking on the Recording1.rxrec in the Projects tab and select "View Code"
  • Create a new property 'FirstName' and 'LastName' like this:

C#

// Create a new property 'FirstName'
public static string FirstName
{
  get;set;
}

// Create a new property 'LastName'
public static string LastName
{
  get;set;
}

VB.NET

' Create a new property 'FirstName'   
Public Shared Property FirstName() As String
	Get
		Return m_FirstName
	End Get
	Set
		m_FirstName = Value
	End Set
End Property
Private Shared m_FirstName As String

' Create a new property 'LastName'   
Public Shared Property LastName() As String
	Get
		Return m_LastName
	End Get
	Set
		m_LastName = Value
	End Set
End Property
Private Shared m_LastName As String

Python

@classmethod
def set_FirstName(cls,name):
	cls.firstName = name
	
@classmethod
def get_FirstName(cls):
	return cls.firstName
		
FirstName = property(get_FirstName, set_FirstName)
	
@classmethod
def set_LastName(cls,name):
	cls.lastName = name
	
@classmethod
def get_LastName(cls):
	return cls.lastName
		
LastName = property(get_LastName, set_LastName)

6. Edit user code actions

Edit the 'SetFirstName' and 'SetLastName' methods and replace the Keyboard.Press strings:

C#

public static void SetFirstName()
{
	Keyboard.Press(FirstName);
}
public static void SetLastName()
{
	Keyboard.Press(LastName);
}

VB.NET

Public Shared Sub SetFirstName()
	Keyboard.Press(FirstName)
End Sub
Public Shared Sub SetLastName()
	Keyboard.Press(LastName)
End Sub

Python

@classmethod
def SetFirstName(cls):
	Keyboard.Press(cls.FirstName)
		
@classmethod
def SetLastName(cls):
	Keyboard.Press(cls.LastName)
Now we have the ability to set our Keyboard.Press() text outside the recording class.

7. Set the 'FirstName' and 'Lastname' properties and start your test

Now we have the ability to configure our Keyboard.Press() action outside the recording class.

  • Open the 'Program.cs' file
  • Set the 'First Name' and 'Last Name' properties:

C#

...
try
{
      AddVIP.FirstName  = row["FirstName"].ToString();   
      AddVIP.LastName   = row["LastName"].ToString();   
  
      AddVIP.Start();   
}
catch (RanorexException e)
...

VB.NET

...
Try
    ' Read every row from connector and send to AddVIPApplication.
    For Each row As DataRow In csvConnector.Rows
         AddVIP.FirstName = row("FirstName").ToString()
         AddVIP.LastName = row("LastName").ToString()
         
         AddVIP.Start()
    Next
Catch e As RanorexException
...

Python

...
Try

    for row in rows:
	AddVIP_UserCode.AddVIP.FirstName = row["FirstName"].ToString()
	AddVIP_UserCode.AddVIP.LastName = row["LastName"].ToString()
	
	AddVIP.Start()

except RanorexException, e:
  • Save the changes and click on the green 'Run' button in the Ranorex Studio toolbar or press F5 to run the compiler and to start the compiled executable.

Add test data from a CSV file (data-driven test)

The implementation of properties in chapter:1 is the base for data driven testing. Now we can feed the textboxes with data.

1. Add the 'CSVConnector' and the test data file

Include the ‘CSVConnector’ file into the project (located in the DataDrivenTesting  sample directory).
  • Right-click on project node -> "Add" -> "Existing Item..." and select the "CSVConnector.cs" file

2. Load the test data with the 'CSVConnector' class

  • Open 'Program.cs' file
  • Create a new object of type 'CSVConnector' and set the CSV file location.

C#

...
try
{
      // Create a new CSVConnector object 
      CSVConnector csvConnector = new CSVConnector(@"..\..\TestData.csv"); 
}
catch (RanorexException e)
...

Now we are able to access the rows in the data table.

4. Get the data-sets from the CSV file

Within a foreach loop we start 'Recording1' for each data row. By executing the test you can see that the test is executed for each data set defined in the CSV file.


C#

...
try
{
		// Create a new CSVConnector object 
		CSVConnector csvConnector = new CSVConnector(@"..\..\TestData.csv"); 
		
		// Read every row from connector and send to AddVIPApplication.
		foreach(DataRow row in csvConnector.Rows)
		{     
			AddVIP.FirstName = row["FirstName"].ToString();   
			AddVIP.LastName  = row["LastName"].ToString();   
			
			AddVIP.Start();   
		}  
}
catch (RanorexException e)
...

5. Start your data driven test

Our test example is now connected to the test data CSV file. To run and execute the test it is necessary to compile the source code we've just added. Hence, click on the green 'Run' button in the Ranorex Studio toolbar or press F5 to run the compiler and to start the compiled executable.

Extend your test by adding new UI elements

1. Add 'Gender' radio buttons to the sample application

  • Open the Element Browser and click on the 'Track' button
  • Track the 'female' radio button in the VIP application
    • Open the Recorder View and drag drop the 'female' button form the element browser into the repository of the recording
  • Also track the 'male' button in the VIP application and add this item to the repository of the recording too.

2. Add a new 'Gender' property for parameterization

  • Open the 'AddVIP.Usercode.cs' file
  • Use the following code to add a 'Gender' property

C#

// Create a new property 'Gender'
public static string Gender
{
  get;set;
}

3. Add a new user code item to the recording

  • Open the 'AddVIP' recording view
  • Add a new user code action item after the 'SetLastName' action
  • Set the method name to 'SetGender'
  • Right-click on the 'SetGender' item and select 'View User Code'
  • Modify the 'SetGender' method as follows

C#

public static void SetGender()
{
	if(Gender == "Male")
	{
		repo.FormVIP_Database.RadioButtonMale.Click();   
	}
	else if(Gender == "Female")  
	{
		repo.FormVIP_Database.RadioButtonFemale.Click();   
	}
}

4. Set the 'Gender' property

  • Open the 'Program.cs' file
  • Set the 'Gender' property before AddVIP.Start()

C#

...
try
{
		// Create a new CSVConnector object 
		CSVConnector csvConnector = new CSVConnector(@"..\..\TestData.csv"); 
		
		// Read every row from connector and send to AddVIPApplication.
		foreach(DataRow row in csvConnector.Rows)
		{     
			AddVIP.FirstName = row["FirstName"].ToString();   
			AddVIP.LastName  = row["LastName"].ToString();   
			AddVIP.Gender = row["Gender"].ToString ();
			
			AddVIP.Start();   
		}  
}
catch (RanorexException e)
...