Modify Click action by injecting code before/after action

Best practices, code snippets for common functionality, examples, and guidelines.
Posts: 1
Joined: Fri Jul 17, 2015 2:58 pm

Modify Click action by injecting code before/after action

Post by ahall » Fri Jul 17, 2015 7:28 pm

I want to write a method and have it run, by default, before or after Ranorex replays a "Click" action. I want there to be as little extra work necessary after the developer has recorded themselves performing the steps they want automated. So - hypothetically - let's say I want to print out to the console the RanoreXPath of the element that the "Click" action is being performed on whenever Ranorex replays a "Click" action... very loosely a part of the steps to be performed would look like...

1 - Move mouse to element to be "Clicked"
2 - Perform "Click"
3 - Continue on with test

No steps would have to be added to get the xpath printed to the console, it's performed in step number 2 as part of the "Click" action.

Thanks for any help you can give.

Posts: 107
Joined: Fri Jul 03, 2015 9:18 am

Re: Modify Click action by injecting code before/after action

Post by jma » Mon Jul 20, 2015 8:32 am

Hi ahall,

As default, Ranorex logs the element name of the repository item if click actions are recorded. This behavior is not configurable.

One way to solve your issue would be to add logging actions after every click action. This could be very time consuming because you would have to add these actions for every existing and for all new modules.

The second possibility would be to create your own click action which combines the logging- and the click-action. Depending on your needs, you have to adjust the method sample below and pass for example different parameters to Ranorex.Mouse.Click.

Code: Select all

public static void PerformClick(Element element, System.Windows.Forms.MouseButtons button)
	Report.Info("Click on element: "+element.GetPath(PathBuildMode.Default));
	Ranorex.Mouse.Click(element, button);
The question is, whether logging this information would be really necessary because in case of error ('No element found for path...') the xPath is logged to the report in any case.

I hope, this could help you.