Code Actions

This Documentation is only for Ranorex 2.x versions.

Click here for the Ranorex 3.x test automation documentation

In some cases it might be required to extend simple recorded user actions like mouse clicks or keyboard inputs with some user specific test automation code. Within Ranorex Studio, the Ranorex Recorder provides the ability to declare user code actions which are executed as standard actions. This feature provides the user with a maximal flexibility in combination with Ranorex Recorder for many different use cases. Here are a few examples:

  • Convert simple keyboard inputs to data driven actions
  • Extend recording with conditional code or loop actions
  • Extend recording with specific validation actions
  • Reuse of commonly used test modules within recordings
  • Allow testers without extensive programming skills to reuse code actions through the Recorder's actions table
  • Create configurable, parameter driven recordings
  • Easily decrease entry barriers to implement small automation snippets for non-professionals
  • ... and a lot more!

How to extend recordings with user code

Within Ranorex Studio each recording manages two types of source code files. 

  • Automatically generated main recording source code file
    <RecordingName>.cs
  • User specific source code file
    <RecordingName>.UserCode.cs

Note: Any change of code should always be made within the recording's UserCode.cs file. The main recording.cs file which contains the Start() method will be overwritten each time the recording is saved.

There are two ways to create user code actions with Ranorex Recorder:

#1: Convert an existing action item to a code action
#2: Insert a new and empty code action

By converting an existing action item into a user code action, the Recorder predefines the name for the code action automatically. Simply rename the code action by changing it within the actions table or by changing the name within the property grid. When adding a new code action via the tool bar button, you'll be prompted to specify the name.

Regardless which way you choose to create your code actions, the Recorder declares a new method using the name of the code action within the UserCode.cs file. Simply use the context menu item 'View User Code' to navigate to the related method within the source file.

Recording1.UserCode.cs file with user code action named 'MyCodeItem'
Recording1.cs file executing all simple actions and calling 'MyCodeItem' within Start() method

Reuse of existing code actions

Each method within the UserCode.cs file can be reused several times within a single recording. Simply add a new User Code item by clicking the tool bar button as described above. Open the property grid to see which methods already exist within the current UserCode.cs file.
Provides the list of currently available methods within UserCode.cs file
UserCode.cs file containing the methods which are listed in the property grid

Advanced use of user code files

The Ranorex Studio project example 'DataDriven Test Sample' shows one possible way to extend single recordings using not only methods. The example contains a recording which represents a single test module called 'AddVIP'. The additional use of public declared properties within the AddVIP.UserCode.cs file allows it to set parameters for the recording before execution.
'AddVIP.UserCode.cs' file declaring additional public properties
Data driven execution of the 'AddVIP' test module