Module variables and data-driven testing
To perform data-driven testing, you need to define variables. Outside of code modules, you can do so easily through the Ranorex Studio UI. Within code modules, however, you must use module variables. Only module
We want the test to pull personal data from an external data source (CSV file) and enter it into the database of the Ranorex Studio Demo Application. All of this needs to be accomplished with a code module.
Ranorex Studio Demo Application with all of the data entered into the database.
The role of module variables
As mentioned above, local or global code variables cannot access external data. This is where module variables come in. Module variables act as a bridge between an external data source and the code module.
Module variable in the test suite view, bound to the external data.
Module variable in code.
Create a module variable
Now we’ll create a code module that pulls the first name from an external data source and enters it in the respective database field. This process is the same for all fields that use ⇢ action variables.
For gender and department, which are radio buttons and list elements, we’ll need to connect module variables to ⇢ repository variables. This is explained further below.
Create a new code module and name it InsertPerson.cs.
Right-click below the empty constructor and click Insert new module variable…
Name the module variable varFirstName, enter John as the default value, and click OK.
Public module variable varFirstName with Get and Set methods.
The variable also appears in the UI of Ranorex Studio:
Unbound variable in the test suite view next to the code module.
This shows that the module variable has been defined correctly and is recognized outside of the code module. It’s ready to be fed with external data.
Connect the module variable to the repository item
We’ve defined the module variable, but it now needs to be connected to the correct repository item. Otherwise, the value it will receive from the external data source will end up nowhere.
Connect the module variable to the repository item representing the text field for the first name:
At the beginning of the class InsertPerson, add a private, static repository.
Bind the module variable to the data
Finally, to complete the data chain from the external data source to the repository item, we need to bind the module variable to the data. This is done through the Ranorex Studio UI and not in code.
Add a CSV data source using a CSV data connector and name it myData. You can find the CSV file RxTestDatabase.csv in the sample solution. Alternatively, create your own CSV file.
Assign the CSV data source to the test case that contains the code module.
Under Data binding…, bind the column FirstName to the module variable varFirstName and repeat for the other columns.
Address variable repository items with module variables
Just like the value of a text entry action, repository items can also be made variable. This is particularly useful for UI elements like menu entries, list elements, radio buttons etc. The variables used for this purposes are called repository variables. In our example, this applies to the gender and department entries, as they are radio buttons and list elements, respectively.
In this section, we’ll show you how to use module variables to address existing repository variables in code. This requires that you first define them in the Ranorex Studio UI, which is explained ⇢ here. First, we’ll take a look at the radio buttons, then list elements.
We want the test to select the radio button based on values (“Male” or “Female”) it pulls from a data source. In a recording module, the solution would look like this:
Variable repository item that represents the radio buttons. Note the repository variable $varGender in the RanoreXPath.
Mouse-click action linked to the variable repository item.
For a more details on how this is accomplished with a recording module, please refer to
Ranorex Studio advanced > Data-driven testing > ⇢ Define variables
or take a look at the sample solution for this chapter.
Address radio buttons
In a code module named SelectGender.cs, carry out the following steps:
In the class SelectGender, declare the private, static repository myRepo.
Sets the value of the variable $varGender to the current value pulled from the data source through data binding.
Then calls the Select() method to select the radio button defined by the value of the variable.
Download the sample solution
This sample solution contains all the finished examples of this and the previous chapters.
Install the sample solution:
Unzip to any folder on your computer.
Start Ranorex Studio and open the solution file