This article explains how custom actions can be implemented and used directly within Ranorex Recorder. Additionally, it provides an example implementation of a custom (user code) action. It furthermore describes how this action can easily be shared – e.g. to also make such actions available for non-programmers, mainly through the use of Ranorex Recorder.
- Example: Step-by-Step Instructions
- Spread your Custom Action by Inheriting from a Base Class
- Advanced Example: Validate Content of Whole Table
- Post Your Top Use Case
- Conclusion
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.
Since very often different scenarios demand different solutions, we would also like to see your “top custom actions” on our user voice platform. This platform allows users to share ideas and also vote for implementations that have been shared by others in the community. Let’s build up a useful library of custom actions! 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: 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”. 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. Click the “Args…” button to bring up the argument editor, allowing you to create input parameters (i.e. arguments) for this 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. 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). 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: 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: 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. 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). 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 using a simple trick: inheritance! Now move the code of your custom action “KeySequenceAndValidate” from the “UserCode”-file to the newly created class. Finally, every recording that should have access to the methods provided in the base class needs to be modified with a simple change: derive the recording’s user code from the base class. Simply open the <RecordingName>.UserCode.cs file and add a colon followed by the name of the base class to the existing declaration of the class (see screenshot below). Doing so, all methods from the base class are combined (globally shared) with the ones from the corresponding user code file (locally shared) and are also automatically available in the recording. This approach can be applied to all new and existing recordings.
Example: Step-by-Step Instructions for Custom “Key Sequence and Validation” Action
1. Merge Relevant Items to a “User Code” Action
2. Make Action Flexible by Providing Arguments
3. User Parameters in User Code
public void KeySequenceAndValidate(Ranorex.Adapter MyTextBox, string MyKeySequence)
{
...
}
public void KeySequenceAndValidate(Ranorex.Adapter MyTextBox, string MyKeySequence)
{
MyTextBox.PressKeys(MyKeySequence); // Perfom key sequence action
Validate.Attribute(MyTextBox, “InnerText”, MyKeySequence ); // validate result
}
Spread your Custom Action by Inheriting from a Base Class
To start with the inheritance approach, first add a new item from type “Class” to your Ranorex Project and give it a meaningful name, e.g.: RecorderBaseClass (see screenshot below). It will be your base class for future as well as existing recordings.
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.
Post Your Top Use Case
Apart from the use cases mentioned above, you might have other frequently used cases of custom actions. To share them with others, please visit http://uservoice.ranorex.com and contribute your custom actions! Also feel free to vote for ideas and implementations being shared by other Ranorex users. Top voted custom user codes might later be added to the default set of Ranorex actions.
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 repository item. By using inheritance, these custom actions can easily be shared and therefore can be used in the Ranorex Recorder by someone without development skills. Concrete examples of custom actions and inheritance were shown, including the complex validation of the content of an entire table.