This chapter describes how to access the repository and individual repository items from a code module to link them to actions programmed in code.
Test scenario
The AUT will be the Ranorex Studio Demo Application, which is contained in the sample solution linked in Introduction. 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”.
Let's create a code module with the same functionality.
Create the code module
-
Create a code module as described in the Introduction and name it InsertName.cs.
Instantiate the repository
To act 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
-
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 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 dynamic help.
Coding the action for entering the last name works in the same way:
Run the code module
Now that you programmed the required actions, 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.
-
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.
-
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
-
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.
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