Module variables and data-driven testing | Ranorex
Help CenterUser GuideRanorex Studio expert

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 variables can reference external data.

Note icon

Note

Don’t confuse module variables with local/global code variables, which are only for referencing code-internal values.
In this chapter

    Test scenario

    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.

    Reference

    To learn how to accomplish this with a recording module, please refer to

    Ranorex Studio advanced > ⇢ Data-driven testing

    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.

    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.

    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.

    Ranorex Studio will create a module variable definition with the following content:
    Private string constant _varFirstName with the default value of the variable.

    Internal reference.

    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.

    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.

    Instantiate the repository in the previously empty constructor.
    Implement the data transfer from the module variable to the repository item in the Run() method.

    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.

    Reference

    Managing and assigning data sources is explained in

    Ranorex Studio advanced > Data-driven testing > ⇢ Manage and assign data sources.

    Data binding is explained in

    Ranorex Studio advanced > Data-driven testing > ⇢ Data binding.

    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.

    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.

    Test scenario

    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.

    Reference

    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.

    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.

    Set method

    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.

    Sample solution

    Theme: Module variables
    Time: 30 minutes

    Download sample file

    Install the sample solution:

    Unzip to any folder on your computer.
    Start Ranorex Studio and open the solution file RxDatabase.rxsln

    tipp icon

    Hint

    The sample solution is available for Ranorex versions 8.0 or higher. You must agree to the automatic solution upgrade for versions 8.2 and higher.