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 variables can reference external data.
In this chapter
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.
External data source, e.g. a CSV file.
Ranorex Studio Demo Application with all of the data entered into the database.
To learn how to accomplish this with a recording module, please refer to
Ranorex Studio advanced > ⇢ Data-driven testing
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.
External data source, added to the test suite by way of a CSV connector.
Module variable in the test suite view, bound to the external data.
Module variable in code.
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.
Ranorex Studio will create a module variable definition with the following content:
Private string constant _varFirstName with the default value of the variable.
Public module variable varFirstName with Get and Set methods.
The variable also appears in the UI of Ranorex Studio:
Variable in the module browser as part of the code module.
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.
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.
Instantiate the repository in the previously empty constructor.
Implement the data transfer from the module variable to the repository item in the Run() method.
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.
To bind the module variable(s) to the data:
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.
The data chain is now complete. The module variable will pull the data from the external data source and then pass it to the repository item.
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:
Gender radio buttons in the database of the Ranorex Studio Demo Application.
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.
In a code module named SelectGender.cs, carry out the following steps:
In the class SelectGender, declare the private, static repository myRepo.
Instantiate the repository in the constructor of the SelectGender class.
Below the constructor, insert the module variable ModVarGender. This module variable will take on the value of the repository variable $varGender. Complete the code as follows to implement the selection functionality:
Get method that returns the current value of the variable $varGender.
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.
This sample solution contains all the finished examples of this and the previous chapters.
Theme: Module variables
Time: 30 minutes
Unzip to any folder on your computer.
Start Ranorex Studio and open the solution file