select action from library

This article explains how custom actions can be created and used directly within the Ranorex Recorder. In a detailed example you can see how you can create custom (user code) actions and easily share them within a project.

Custom-Smart-Actions

Although Ranorex Recorder provides a number of smart actions to be used in various automation scenarios (Set Value, Close Application, etc.), it might be useful to provide custom smart actions (i.e. user code actions) tailored to your everyday automation needs.

User code actions are even more powerful since they now allow arguments of various data types, first and foremost a data type for repository items. This comes with a possibility for making custom actions more flexible and adjustable since repository items can be assigned as arguments in the Ranorex Recorder.

Example: Step-by-Step Instructions for Custom “Key Sequence and Validation” Action

We can assume that someone needs to automate a key sequence on a text box located on a website, followed by a subsequent validation. These two actions then need to be performed for several text boxes throughout the test. Although this scenario can easily be implemented with copy and paste (producing lots of redundancies), in best practice it is realized using the following steps:

1. Merge Relevant Items to a “User Code” Action

Merge the relevant actions into a user code item using the context menu and provide a meaningful name for this newly created action, e.g. “KeySequenceAndValidate”.

Merge-Relevant-Actions

Now these two steps are available as one “UserCode” action within the current recording. It can easily be called multiple times, but the underlying actions are still static and inflexible, being performed on the same “hardcoded” repository element and key sequence.

2. Make Action Flexible by Providing Arguments

Click the “Args…” button to bring up the argument editor, allowing you to create input parameters (i.e. arguments) for this user code action.

Name-Of-User-Code-Action

These arguments will later allow the action to be performed on any assigned repository item with any key sequence provided – without the need to modify the action itself.

Argument-Editor

For this sample, a parameter of type “Adapter” (representing the repository element) is needed as well as a parameter of type “String” representing the key sequence (more information on user code parameters).

3. User Parameters in User Code

Resulting from the modifications in the argument editor, two new arguments have been added to the header of the user code action “KeySequenceAndValidate”. These arguments can be reviewed in the <RecordingName>.UserCode.cs (or *.vb) file:

public void KeySequenceAndValidate(Ranorex.Adapter MyTextBox, string MyKeySequence)
{
    ...
}

The usage of the static repository item and key sequence should now be made flexible by using the given arguments so replace the hardcoded “repo.RanorexWebsite.InputTagQ” with the parameter “MyTextBox”. Additionally replace the hardcoded key sequence “Ranorex” with the parameter “MyKeySequence”. After these modifications and after dropping the two lines starting with “Report.Log(…”, your code should look like this:

public void KeySequenceAndValidate(Ranorex.Adapter MyTextBox, string MyKeySequence)
{
    MyTextBox.PressKeys(MyKeySequence); // Perfom key sequence action
    Validate.Attribute(MyTextBox, “InnerText”, MyKeySequence ); // validate result
}

Note: Logging (“Report.Log…”) has been removed in the sample above. Detailed information on custom logging from code can be found in our user guide.

Now this action can easily be called from the recorder view (see screenshot below) whenever these steps need to be performed. Since this user code action is now completely adjustable, the repository item as well as the key sequence can be provided as arguments in the recorder’s action table.

User-Code-Action-With-Arguments

Consequently the same action could also be called from another position to be performed on another repository item with another key sequence. To reuse existing custom actions, click “Add New -> User Code Action” and choose the desired method from the drop down. To quickly assign repository items, the drag and drop feature can be used (see screenshot below).

Assign-Repo-Item-To-User-Code-Action

 

Share custom user code actions using the user code library

When implementing the steps described above, the custom user code action is available within the current recording only. Usually with bigger projects, there is likely a need to call this method from multiple recording modules. This can be achieved by gathering your custom actions into user code collections.

To do so, first add a new “User Code Library” to your Ranorex Project and give it a meaningful name.

user code library

Now move the code of your custom action “KeySequenceAndValidate” from the user code file of the recording to the newly created user code collection. Add the keyword ‘static’ to your method declaration as well as the attribute ‘[UserCodeMethod]’ directly before the method to make it traceable in the user code library.

user code method

You can add new user code methods directly in the user code collection by using the “Insert New User Code Method” functionality from the context menu (See User Code Library – Collections And Methods for more details).

That’s all there is to it! All methods from the collection are available in every recording of your project.

In the actions table, you just have to select the method from the user code library to use it directly as an action.

select action from library

Advanced Example: Validate Content of Whole Table

Even more complex scenarios can be implemented with the workflow described above. Assuming the content of a whole table needs to be validated, using a user code action to accomplish this can save a lot of time, especially if the table to be validated can be passed as a parameter. Please find a sample implementation of the described scenario in our code examples: Advanced Validation – Whole Table.

Conclusion

User code actions can be extended with arguments of various data types whereas the repository item might be the one with the biggest impact. It allows a user to create custom actions (e.g. key sequence and a following validation) completely independent of a specific UI element. By utilizing user code collections, these custom actions can easily be shared within a project. User code methods within these collections can be accessed directly from the user code library in the Ranorex Recorder and used as an action by someone without programming skills.

This blog has been revised in January 2017 to reflect additional capabilities of Ranorex Studio, which are available from Ranorex version 6.2

You might also like these articles