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.
Test scenario
The test uses a code module that pulls personal data from an external data source (CSV file) and enters it into the database of the Ranorex Studio Demo Application.
- External data source, for example, a CSV file.
- Ranorex Studio Demo Application with all of the data entered into the database.
Create a module variable
To access external data, you need module variables. These 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.
Follow these steps to create a module variable that pulls data for a text field:
- 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 and enter John as the default value.
- Click OK.
Ranorex Studio generates the module variable definition with Get and Set methods.
- Private string constant _varFirstName with the default value of the variable.
- Internal reference.
- Public module variable varFirstName with Get and Set methods.
You can see the variable in the Module browser (1) and as an unbound variable in the Test suite view (2).
Connect the module variable to the repository item
You must link the module variable to a specific UI element so the data reaches its destination.
-
Add a private static repository at the beginning of the InsertPerson class.
private static RXDatabaseRepository myRepo; -
Instantiate the repository in the empty constructor.
myRepo = new RxDatabaseRepository(); -
Implement the data transfer in the Run() method to pass the value from the module variable to the repository item.
myRepo.DemoApplication.DatabaseTab.FirstName.TextValue = varFirstName; myRepo.DemoApplication.DatabaseTab.LastName.TextValue = varLastName; myRepo.DemoApplication.DatabaseTab.Age.TextValue = varAge;
Bind the module variable to the data
Complete the data chain from the external data source to the repository item by binding the module variable to your source via the Ranorex Studio UI:
- Create a CSV file with the personal data you want to pull. Alternatively, use the RxTestDatabase.csv file in the sample solution below.
- Add the CSV data source using a CSV data connector and name it myData. For more information, see Manage and Assign Data Sources.
- Assign the data source to the test case containing your code module.
-
Under Data binding…, bind the column FirstName to the module variable varFirstName and repeat for the other columns.
The module variable pulls data from the external data source and then passes it to the repository item.
Address variable repository items
Just like the value of a text entry action, repository items can also be variable. This is particularly useful for UI elements like menu entries, list elements, radio buttons, etc. The variables used for this purpose are repository variables.
This section describes how to use module variables to address existing repository variables in code.This requires that you first define the variables in the Ranorex Studio UI. For more information, see Define Test and Variables.
For this example, the repository variables apply to the gender and department entries, as they are radio buttons and list elements, respectively.
Test scenario
Test to select the radio button based on values (“Male” or “Female”) and pull from a data source. In a recording module, the solution would look as follows:
-
- 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.
Address radio buttons
To select a Gender radio button based on external data:
- Create a new code module and name it SelectGender.cs.
-
Declare the private, static repository myRepo in the SelectGender class.
private static RxDatabaseRepository myRepo; -
Instantiate the repository in the constructor of the SelectGender class.
myRepo = new RxDatabaseRepository(); - Insert a module variable named ModVarGender below the constructor.
- Use the Get method to make the module variable take on the current value of variable $varGender.
-
Use the Set method to set the value of the variable $varGender to the current value pulled from the data source through data binding. Then call the Select() method to select the radio button defined by the value of the variable.
public string ModVarGender { get { return myRepo.varGender;} set { myRepo.varGender = value; myRepo.DemoApplication.DatabaseTab.SelectGender.Select(); } }
This sample solution contains all the finished examples of this and the previous chapters.
- Theme: Module variables
- Time: 30 minutes
Install the sample solution
- Unzip to any folder on your computer.
- Start Ranorex Studio and open the solution file
RxDatabase.rxsln - Agree to the automatic solution upgrade if you are using Ranorex versions 8.2 or higher.