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
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:
Mouse click into the text field represented by the repository item FirstName and text entry “John”.
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.
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.
Instantiate the repository in the Run() method of the code module as follows:
Repository in the projects view
Instantiation of the repository in the Run() method of the code module InsertName.cs
The class name and file name of the repository are always the same.
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:
Change to the test suite view.
Drag and drop the code module to the correct spot in the test case.
Deactivate or delete the recording module of the same name.
Code modules and recording modules are used in exactly the same way in the test suite. If a recording module and a code module share the same name, you can differentiate them by their symbol and the appended folder name in parentheses.
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.
These variables have nothing to do with those that you define for data-driven testing. These are called module variables in code and are explained in the chapter ⇢ Module variables and data-driven testing.
Create a new code module, name it AddEntry.cs, and enter the following code in the Run() method:
Instantiation of the repository
Definition of the variable ButtonAdd, which references the repository item BtnAddEntry
A click action on the referenced button
You can also define variables for repository items by dragging the repository item from the repository in the recording module view and dropping it in the code module at the desired position.
If the respective repository hasn’t been instantiated yet in this code module, this will be done automatically. Otherwise, the existing instantiation will be used.
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:
Validation action in the recording module
This action compares the text value of the repository item CounterEntries against the value 1.
Implementation in the code module
The method Validate.Equals compares the text value of the repository item CounterEntries against the value 1.
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.