Welcome to the new Ranorex Support Portal

Access the repository and repository items

This chapter describes how to access the repository and individual repository items from a code module, in order to link them to actions programmed in code.

In this chapter

Test scenario

Our AUT will be the Ranorex Studio Demo Application, which is contained in the sample solution linked in ⇢ Introduction. We want to enter personal data (first name, last name) into a database with a code module.

Let’s first take a look at how this is done with a recording module:

  1. Mouse click into the text field represented by the repository item FirstName and text entry “John”.

  2. Mouse click into the text field represented by the repository item LastName and text entry “Public”.

We’ll now create a code module with the same functionality.

Create the code module

  1. Create a code module as described in ⇢ Introduction and name it InsertName.cs.

Instantiate the repository

To perform an action on a UI element with a code module, you first need to instantiate the repository where this UI element is represented. The repository is instantiated with its file name, as shown in the projects view.

  1. Instantiate the repository in the Run() method of the code module as follows:

  1. Repository in the projects view

  2. Instantiation of the repository in the Run() method of the code module InsertName.cs

Alternatively, you can also instantiate the repository with the method Instance:

Use UI elements

With the repository instantiated, you can now use the contained repository items, and therefore the UI elements represented by them, for your test.

In the repository structure, each repository item is its own class with a set of methods. For example, to enter the first name into the respective text field of the database form, you need to call the following method:

The class hierarchy for calling methods follows the structure of the repository. You can display the available variables and methods using the dynamic help.

Coding the action for entering the last name works in the same way:

Run the code module

Now that we’ve programmed the required actions, we can run the code module and see if it performs in the same way as the recording module.

To run the code module:

  1. Change to the test suite view.

  2. Drag and drop the code module to the correct spot in the test case.

  3. Deactivate or delete the recording module of the same name.

  4. Click RUN.

Define code-internal variables for repository items

As your code module grows, it can quickly become very complex if you always address repository items and their methods using their full path. This is especially true if you use the same repository items multiple times.

In this case, it can make sense to address a repository item using a local or global variable. We’ll show you how to do so with the Add entry button of the database form.

  1. Create a new code module, name it AddEntry.cs, and enter the following code in the Run() method:

  1. Instantiation of the repository

  2. Definition of the variable ButtonAdd, which references the repository item BtnAddEntry

  3. A click action on the referenced button

Validate with code modules

You can also create validations in code modules. The class Validate contains everything required for validations. For full documentation of the available methods and variables, please refer to Ranorex Namespace/Validate in the API documentation.

Take a look at the following example, which shows how to implement the validation of the number of database entries from the sample solution in a code module:

  1. Validation action in the recording module
    This action compares the text value of the repository item CounterEntries against the value 1.



Validations are explained in

Ranorex Studio fundamentals > ⇢ Test validation

  1. Implementation in the code module
    The method Validate.Equals compares the text value of the repository item CounterEntries against the value 1.

  2. By itself, the method Validate.Equals does not create a report entry. This is why we added a simple if-then-else condition to generate a report entry depending on the result of the comparison.



Configuring reporting in user code is explained in

Ranorex Studio fundamentals > Reporting > ⇢ Complex customizations



More advanced examples for the Validate class are available in

Hands-on application topics > ⇢ Code examples



Get the latest test automation information right in your mailbox.


© 2024 Ranorex GmbH. All Rights Reserved