Regex applied in Ranorex Studio

On this page, you’ll find two examples that show how to apply regexes in Ranorex Studio. The first example is a validation, while the second example extracts a value with a Get value action.

In this chapter

    Sample solution

    The completed sample solution containing the examples on this page is available for download.  You are invited to experiment with it. Alternatively, you can follow the instructions below to build your own test solution.

    Download

    Theme: Regexes applied in Ranorex Studio
    Time: Less than 30 min

    Download sample file

    Instruction:

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

    Download the Ranorex Studio Demo Application

    If you don’t already have the Demo App on your computer, you can download it here and unpack it to a folder of your choice.

    Validation example

    Regexes are often helpful when validating values that change through interaction with the AUT. In this example, we want to validate a dynamically generated image ID that is displayed next to the image.

    The ID changes whenever the image is hidden and shown again.

    Solution

    A simple text-based validation would fail here because the ID changes dynamically. A regex, by contrast, can easily deal with it.

    Let’s take a look at the validation in Ranorex Studio:

    The validation operator is AttributeRegex, as we’re validating with a regex

    The Match name depends on the repository item linked to the action. In our case, we validate the Text attribute of the repository item

    The regex matches a string that starts with “img_” followed by a 16-digit string consisting of any letters or numbers – this is what the dynamic ID consists of

    Result

    The validation succeeds whenever the Text attribute of the linked repository item has a value that the defined regex matches.

    Extract information with a Get value action and a regex

    The Get value action allows you to extract attribute values. Often, you may only want to extract a certain part of a value, e.g. a number. This can be done with a regex.

    Start the Demo App and click the tab Test database.

    Enter the data for a person and add it to the database.

    Suppose we now want to extract a person’s age from the resulting string in the database and assign it to a variable.

    The database entry is displayed as a string in the Demo App The person’s age is part of this string.

    In Ranorex Studio, this string is represented by the repository item TextLine. The RanoreXPath expression for this repository item uses the attribute accessiblerole with the value ListItem.

    Solution

    We need to use a regex to extract the age from the string. Then we can pass it on to a variable, so it can be used in a validation, for example.

    Let’s take a look at the completed Get value action in Ranorex Studio.

    The entire action in Ranorex Studio

    Action type Get value to extract a value

    The linked repository item’s attribute that contains the value

    The variable that the value will be assigned to

    The regex to extract only the age. In this case, it matches any number with at least one and at most 2 digits, i.e. all numbers from 0-99

    Repository item linked to the action

    Result

    If the Text attribute of the linked repository item contains a number from 0-99, this number is extracted and assigned to the variable $myAge.

    In the report, this would look as follows: