In this chapter
Creating Code Modules
Alternatively you are able to add a new code module by using the context menu in the Test Suite.
Specifying the name used for the code module
After clicking the ‘Create’ button a new file is added to the project and automatically opened in the file view. Ranorex Studio creates a new test module class which contains a ‘Run’ method that is ready to be extended with test automation code.
Using Repository within Code Module
In the same way you use a repository in the recording to identify UI elements for automation, you can also use it in code. Simply add a new private member which represents the repository to your code module class as shown below:
By default the class name of a repository is the same as the repository file name (*.rxrep) shown in the project’s view.
Now the class uses a private member to refer to the repository in order to reuse some of the objects (‘Title’, ‘Username’, ‘Password’, ‘PasswordRepeat’ and ‘URL’) within the ‘Run’ method.
Depending on the structure of your repository, accessing items in code might become more and more complex. To reduce complexity – especially when UI elements are used more than once – you should use local variables instead of coding the whole structure of your repository everytime you need to automate a UI element.
To create local variables as shown in the code above, simply drag and drop elements from the repository browser directly into the code.
If the repository itself is not already part of the class(e.g. newly created code modules), a local variable for the repository is generated too.
Accessing Screen Shots within Code Modules
Starting with Ranorex 3.3 it’s possible to access screen shots directly in code using the Info object of a repository item. This can be useful if you are going to compare a captured screen shot with the actual appearance of your application under test, for example.
Screenshot captured in repository
Using the FindOptions, you can set custom values like ‘similarity’. This option allows you to define the minimum similarity that the image region to search for needs to have in common with the screenshot in order to be considered a match. For further details have a look at
⇢ Image Based Automation.
Using Variables with Code Modules
Add a new variable to your code module
Specify the variable name and the default value
By adding a new variable Ranorex Studio inserts a new code fragment at the current cursor position. A variable implementation consists of a public property ” and a private member ‘_’.
Now create additional variables for the ‘Username’, ‘Password’ and ‘URL’. All the module variables will appear immediately in the module browser.
Accessing Repository Variables with the Use of Setter Methods
To bind repository variables to external data when accessing the repository element via the code module, you have to create a new module variable to act as a bridge. You can use the setter method for the public variable to also set the repository variable.
Variables used by the repository (e.g. ‘varExpires’ for the Menu Item in the context menu of KeePass’s ‘Add Entry Dialog’) are easily accessible via the repository, even from code. In order to bind these variables to external data (e.g. one row in our Excel file) in a code module you have to create a new module variable to act as a bridge between external data and repository variables. Following such an approach, it is obviously best to use the setter methods for public variables. A public variable’s setter method is called every time the value of this variable is set; i.e. assigning the value to the private variable holding the information for the public property. This method can easily be extended in order to set the repository variable as well.
First two new module variables, ‘varExpires’ and ‘varIconIndex’, have to be created the same way as was shown for ‘varTitle’, ‘varPassword’. After that, a simple code line has to be inserted into the setter method for each variable. This code line is used for assigning the passed value to the repository variable and facilitates binding to external data.
Using Code Modules within Test Cases
Drag and drop the code module into a test case and combine it with recordings
Reuse the existing data connector by choosing the item from the drop-down
Bind the newly created variables to the data connector’s columns, i.e. columns from the Excel file