Welcome to the new Ranorex Support Portal

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

Download


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

Download sample file

Instruction:

  1. Unzip the project directory to any folder on your computer

  2. 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.

  1. 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:

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

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

  3. 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.

  1. Start the Demo App and click the tab Test database.

  2. 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.

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

  2. 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.

  1. The entire action in Ranorex Studio

  2. Action type Get value to extract a value

  3. The linked repository item’s attribute that contains the value

  4. The variable that the value will be assigned to

  5. 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

  6. 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:

USERS LOVE US

SUBSCRIBE TO OUR NEWSLETTER

Get the latest test automation information right in your mailbox.

FOLLOW US

© 2024 Ranorex GmbH. All Rights Reserved