This article 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 Application Under Test (AUT) is the Ranorex Studio Demo Application. The sample solution in Introduction to Code Modules contains the AUT.
The goal is to create a code module that enters personal data (first name, last name) into a database.
The process looks as follows 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”.
Create a code module with the same functionality.
Create the code module
- Create a code module and name it InsertName.cs. For more information, see Introduction to Code Modules.
- Instantiate the repository in the Run() method of your code module. This is necessary to act on a UI element. Use the file name as it appears in the Projects view.
Example:
- Repository in the Projects view
- Instantiation of the repository in the Run() method of the code module InsertName.cs
Use UI elements
Once you instantiate the repository, you can access its repository items and the corresponding UI elements 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:
myRepo.DemoApplication.DatabaseTab.FirstName.TextValue = “John”;
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:
myRepo.DemoApplication.DatabaseTab.LastName.TextValue = “Public”;
Run the code module
After programming 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:
- Switch to the test suite view.
- Drag and drop the code module to the correct spot in the test case.
- Deactivate or delete the equivalent recording module.
- Click RUN.
Define variables for repository items
For complex modules, you can define local or global variables to simplify your code. This is useful when you need to address the same repository item multiple times.
- Create a new code module and name it AddEntry.cs.
- Instantiate the repository.
-
Define a variable that references the repository item. For example, the following defines the variable ButtonAdd, which references the repository item BtnAddEntry:
var ButtonAdd = myRepo.DemoApplication.DatabaseTab.BtnAddEntry; -
Perform an action using the variable. For example, the following instructs the application to perform the Click() method of the Add Entry button:
ButtonAdd.Click();
You can also drag a repository item directly into your code to define a variable for that repository item. Ranorex Studio automatically instantiates the repository if it is not already present.
Validate with code modules
You can also create validations in code modules. The class Validate contains everything required for validations. For more information, see Validate Class.
The following example 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. A simple if-then-else condition is required to generate a report entry depending on the result of the comparison.
For more advanced examples for the Validate class, see Ranorex Code Examples.