How to make use code reusable in another action

Class library usage, coding and language questions.
ziasim
Posts: 43
Joined: Wed Dec 16, 2015 4:03 pm

How to make use code reusable in another action

Post by ziasim » Thu Jan 14, 2016 3:55 pm

Hi,

Just started learning this tool. My question is

Can we call/use same user code added as an action in recording A to recording B? I am not asking about copying an action from one recording to another. Because it adds all user code created in recording A to recording B.

My scenario is like I added some user code in recording AA in Test Case A. In recording BB(Test Case B) I want o call same user code without adding all code in Test Case B.usercode.cs file (as we know in case of copy user code action, it will add all relevant user code in Test Case B.usercode.cs file which I don't want ).

Is there a possible way that I call same user code method from recording BB and it executes the code written in recording AA?

If its possible it will help me very much to use same user code as an action in various recordings.

Thanks,

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to make use code reusable in another action

Post by Support Team » Fri Jan 15, 2016 9:19 am

Hi ziasim,

Welcome to Ranorex!

There is a simple way to call the same user code method in your recording BB by using inheritance.

Every recording that should have access to your user code method, added as an action in recording AA (called 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 (in your case ‘BB.UserCode.cs’) and add a colon followed by the name of the base class to the existing declaration of the class (e.g. ‘public partial class BB : AA’). Doing so, all methods from the base class are combined and are also automatically available in the recording.

For detailed information about ‘Creating and sharing your own custom actions for the recorder’ please have a look at the following blog post: Create and Share your own Custom Actions for the Recorder

I hope this answers your question.

Regards,
Manuel
.
Image

ziasim
Posts: 43
Joined: Wed Dec 16, 2015 4:03 pm

Re: How to make use code reusable in another action

Post by ziasim » Fri Jan 15, 2016 11:25 am

Hi Manuel,

Thanks for your feedback. I found it very useful and helping especially the blog link. Though I resolved my issue by creating an object of the class which have implemented those methods in recording BB user code file and then accessed all those methods of AA recording using this object as

AArecordingClass obj = new AArecordingClass();

and then

obj.methodName();

But I found this inheritance concept more reusable and efficient so surely I will replace above mentioned logic with one you suggested.

Thanks again,

ziasim

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to make use code reusable in another action

Post by Support Team » Fri Jan 15, 2016 12:50 pm

Hi,

Thank you for your feedback.
I am happy to have helped you.

Regards,
Manuel