Define variables | Ranorex Studio User Guide
Help CenterUser GuideRanorex Studio advanced

Define test and variables

In this chapter, we’ll first define the steps of the data-driven test. Then, we’ll define the required variables for this scenario.

Variables are one of the key parts of data-driven testing. They are the placeholders for the database values you want to feed your test. There are different kinds of variables in Ranorex Studio. They are explained in more detail in ⇢ Variables and parameters. In this chapter, we’ll focus on how to set them up, i.e. how to replace constant values with variables.

In this chapter

    Test definition

    As usual, we’ll use the Ranorex Studio Demo Application for our example. It has a database function that we’ll test. Testing a database is a common real-world application of a data-driven test.

    The test will contain the following steps:

    Start the Demo App.

    Click the Test database tab.

    Test database in Ranorex demo application

    Add an entry to the database.

    Principle of inserting data into the test database
    Repeat until there are 8 entries in the database…

    …and after each entry, validate that the Number of entries updates correctly.

    Test example with 8 persons in list
    Close the Demo App.

    Sample solution and prepared modules

    The ⇢ sample solution already contains all the modules with the necessary actions for the steps above. They are organized in a simple test suite. This will serve as our starting point for the next step: replacing constant values in several modules with variables.

    Sample solution working environment

    Module browser with recording modules structured in folders
    Test suite with a ready-to-run database test scenario

    Define Recorder variables

    First, we’ll replace the constant text values in the respective actions for entering the first name, last name, and age of a person in the Demo App database.

    Examples for recorder variables
    Note icon

    Note

    In Ranorex Studio, variable names have the pattern $<variablename>, where the variable name cannot start with a number or contain any special characters, except for underscores.

    To replace a constant value with a variable:

    Open the drop-down list for the constant value you want to change and click As new variable…
    Name the variable so you can easily identify what value it is a placeholder for and click OK.
    The variable appears in the action table in green, replacing the constant value.

    Adding a new text variable
    Repeat the above process for the last name (InsertName module) and the age (InsertAge module).

    The final result should look like this:

    Definition of three recorder variables in test example

    Variable $txtFirstName replacing the constant value ‘John’ for the first name
    Variable $txtLastName replacing the constant value ‘Public’ for the last name
    Variable $intAge replacing the constant value ’48’ for age

    Further reading

    Recorder variables are explained in detail in

    Ranorex Studio advanced > Variables and parameters > ⇢ Action variables.

    Define repository variables

    Aside from making action values variable, data-driven testing often involves making repository items variable. This works by replacing a certain part of an item’s RanoreXPath, the defining attribute, with a variable. You can make any repository item variable this way.

    For our example, we’ll make a list selection and radio buttons variable. These are repository items where it often makes sense to make them variable because they are usually part of a selection process with multiple options.

    Further reading

    Repository variables are explained in detail in 

    Ranorex Studio advanced > Variables and parameters > ⇢ Repository variables.

    Make a list selection variable

    Here, we’ll make the repository item for the Department list selection variable. In the module SelectDepartment of the sample solution, the Click action is linked to a constant repository item that points to the Project Management entry in the list. In the item’s RanoreXPath, the attribute @text=’Project Management’ is responsible for this.

    We will now replace the constant attribute value Project Management with the variable $lstDepartment so the Click action is performed on whatever list entry our future data source specifies.

    Making list items variable
    Note icon

    Note

    When we create our data source later, we will need to make sure the entries for the department selection are exactly the same as in the list, or else Ranorex Studio won’t be able to identify the UI elements. For example, there is no entry IT, so a repository item with the resulting attribute @text=’IT’ wouldn’t point to anything.

    To define the variable:

    Right-click the second Click action and choose Make repository item variable…

    Making a repository item variable...

    Ranorex Spy opens with the constant value of the text attribute already highlighted. Click the variable symbol next to it.

    Name the variable so you can easily identify what repository item it refers to and click OK.

    In the yellow bar, click Apply.

    Assigning a repository variable
    In the repository, you can now see the variable in the item’s RanoreXPath.
    Defined list item variabel

    Repository variable $lstDepartment replaces the constant value for department selection.

    Make radio buttons variable

    Here, we’ll make the repository item for the Gender selection variable. In the module SelectGender of the sample solution, the Click action is linked to a constant repository item that points to the radio button for Male. In the item’s RanoreXPath, the attribute @controlname=’rdbMale’ is responsible for this.

    We will now replace the constant attribute value in the RanoreXPath of this repository item with a variable ($selGender) so the Click action is performed on whatever Gender radio button our future data source specifies.

    Making radio button selection variable
    Note icon

    Note

    When we create our data source later, we will need to make sure the entries for the gender selection are exactly the same as the respective @controlname attributes of the two radio buttons, i.e. rdbMale and rdbFemale, or else Ranorex Studio won’t be able to identify the UI elements. A data source with just Male and Female would not work, because the repository item with the resulting attribute @controlname=’Male’ wouldn’t point to anything.

    tipp icon

    Hint

    To make a data-driven testing robust against localization (other languages), it is recommended to use attributes such as ControlName.

    To define the variable:

    Right-click the Click action and choose Make repository item variable…

    Making a repository item variable...

    Ranorex Spy opens with the constant value of the text attribute already highlighted. Click the variable symbol next to it.

    Name the variable so you can easily identify the repository item it refers to and click OK.

    In the yellow bar, click Apply.

    Applying repository variable
    In the repository, you can now see the variable in the item’s RanoreXPath.
    Radio button selection made variable

    Repository item representing the Gender radio button selection with variable $selGender

    Define validation variable

    Our example test includes a step that validates whether the database counter updates correctly as entries are added. For this validation to work, we need to make it variable.

    We’ll therefore replace the constant Match value of the validation action in the ValidateEntries module with a variable.

    Example for test validation variables
    Note icon

    Note

    In principle, validation variables are the same as action variables. However, as the Validation action is more complex than most others, we treat their variables separately and therefore also explain the process separately.

    To define the variable:

    Open the drop-down list for the Match value and click As new variable…
    Name the variable so you can easily identify the value it is a placeholder for and click OK.
    The variable appears in the action table in green, replacing the constant value.

    Definition of validation variable

    Further reading

    Validation variables are explained in detail in:

    Ranorex Studio advanced > Variables and parameters > ⇢ Validation variables.

    Overview of defined variables

    We’ve now defined all the variables we need for our data-driven test. When you switch to the test suite view, you can now see the number of variables defined per module. Note they are still unbound, i.e. they have not been assigned a data source.

    In the next chapter, we’ll define this data source and assign it to our test case. We’ll also discuss the options for managing data sources.

    Defined, but unbound example variables

    The test suite view displays the defined, but currently unbound variables