How to call a repository variable inside a User Code Method

Ask general questions here.
s.abdourafiq
Posts: 12
Joined: Mon May 03, 2021 12:44 pm

How to call a repository variable inside a User Code Method

Post by s.abdourafiq » Fri May 07, 2021 2:37 pm

Hi,

I need to call a repository variable inside my user code method because i need to add conditions and results for each value of the variable and I don't know how to call it.
I hope my question is clear.

Thank you

Jacob
Certified Professional
Certified Professional
Posts: 120
Joined: Mon Mar 22, 2021 10:01 pm

Re: How to call a repository variable inside a User Code Method

Post by Jacob » Fri May 07, 2021 3:45 pm

Hi s.abdourafiq,

My name is Jacob and I'm one of the engineers here at Ranorex. As long as the variable you need to call is in scope, you can access it from within a User Code Method. For example, I have a Recording Module with a User Code Method called "userCodeMethod" that does not accept any arguments. I also have a Recording Module variable called "recordingModuleVariable" with a default value of 4 set:
userCodeVariableMethod.png
I can access the "recordingModuleVariable" from within the User Code Method named "userCodeVariableMethod" with the code below:

Code: Select all

var newVariable = recordingModuleVariable;
If you'd like to access a Global Parameter, you can do so in your User Code Method as follows:

Code: Select all

var globalParameterValue = <Test_Suite_Name>.Current.Parameters["<Global_Parameter_Name>"];
Just replace <Test_Suite_Name> with the name of your Test Suite, and <Global_Parameter_Name> with the name of the Global Parameter. I hope this helps!

--Jacob
You do not have the required permissions to view the files attached to this post.
Image

s.abdourafiq
Posts: 12
Joined: Mon May 03, 2021 12:44 pm

Re: How to call a repository variable inside a User Code Method

Post by s.abdourafiq » Mon May 10, 2021 9:26 am

Hi Jacob,

Thank you for responding, I'm actually a beginner in Ranorex Studio and I didn't get what do you mean by the variable in scope however I tried what you suggested and it didn't work and i bet it didn't because my variable might not be in scope :?:

For info, I'm putting my user code methods in a user code collection.

Jacob
Certified Professional
Certified Professional
Posts: 120
Joined: Mon Mar 22, 2021 10:01 pm

Re: How to call a repository variable inside a User Code Method

Post by Jacob » Mon May 10, 2021 4:40 pm

Hi s.abdourafiq,

Thanks for your response! Would it be possible for you to share a bit more information on the issue? Would you be able to post the following?
  • The item that is not working. How is it not working? Are you receiving an error message, a compile time error, or are you receiving unexpected results during execution?
  • Screenshot of all arguments being passed to the function in the "User code" action within the recording
  • Code snippet if possible
Image

s.abdourafiq
Posts: 12
Joined: Mon May 03, 2021 12:44 pm

Re: How to call a repository variable inside a User Code Method

Post by s.abdourafiq » Mon May 10, 2021 4:55 pm

Hi Jacob,

Actually I didn't code my method yet, I just declared my recording variable as a variable inside the code showed in the attached screenshots and then i runned the code an it gave me the following error.
You do not have the required permissions to view the files attached to this post.

Jacob
Certified Professional
Certified Professional
Posts: 120
Joined: Mon Mar 22, 2021 10:01 pm

Re: How to call a repository variable inside a User Code Method

Post by Jacob » Mon May 10, 2021 7:00 pm

Hi s.abdourafiq,

I believe I understand now. My apologies. Since this is a User Code Module that will be called from within a Recording Module, the CheckConditions method you have defined will need to have the Recording Module Variable passed into it. I'll guide you through that process here:

In your CheckConditions method definition, make sure that you add a parameter that you can bind your Recording Module Variable to. For example:
[UserCodeMethod]
    	public static void CheckConditions(RepoItemInfo EditResConsult)
    	{
    		var newVariable = EditResConsult;
    	}
With this in your User Code Method in the Library, you can now add it to your Recording Module and pass the Repo Item (or any other variable) through to the method. To do this, we open the Recording Module and click "Add new action" -> "User code" -> "Select from library"
selectFromLibrary.png
Then select the correct method. In this case, I'm selecting the highlighted item below:
selectFromLibraryMethod.png
Once added, we'll need to tell the method which Repo Item we need to evaluate. To do that, click on the ellipses on the right of the highlighted box in the screenshot below:
methodAdded.png
Once added, that item will be passed to the method.

I hope this information helps to get you started in the right direction!
You do not have the required permissions to view the files attached to this post.
Image

s.abdourafiq
Posts: 12
Joined: Mon May 03, 2021 12:44 pm

Re: How to call a repository variable inside a User Code Method

Post by s.abdourafiq » Tue May 11, 2021 11:06 am

Thank you Jacob,

I guess I didn't clarify my question, I don't need to pass an Item to the user code method, I actually need to pass a recording variable that's already binded and connected to an Excel DataSource and its value changes to a userCodeMethod, the purpose of creating the method is click on a checkbox when the value of the variable is True and to skip and do nothing if its value is False (the True of False values are given in th Excel file connected).


Sorry for the inconvenience.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to call a repository variable inside a User Code Method

Post by odklizec » Tue May 11, 2021 11:33 am

Hi,

Recording variables are visible directly in UserCode of that given recording. Just start typing the name of variable (in User Code) and it should appear in the list of methods, parameters or variables...
RecordingVar.png
You do not have the required permissions to view the files attached to this post.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

s.abdourafiq
Posts: 12
Joined: Mon May 03, 2021 12:44 pm

Re: How to call a repository variable inside a User Code Method

Post by s.abdourafiq » Tue May 11, 2021 11:39 am

Hi odklizec,

Thanks I get it now, i used to call my variable in a separate user code collection, now once I call the variable please how can I get the value of the variable in the user code?

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to call a repository variable inside a User Code Method

Post by odklizec » Tue May 11, 2021 11:45 am

I think that what you are looking for, is very well explained here:
https://www.ranorex.com/blog/sharing-da ... o-another/
This blog nicely explains, how to properly share data between modules.
Last edited by odklizec on Tue May 11, 2021 11:51 am, edited 1 time in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

s.abdourafiq
Posts: 12
Joined: Mon May 03, 2021 12:44 pm

Re: How to call a repository variable inside a User Code Method

Post by s.abdourafiq » Tue May 11, 2021 11:49 am

Okay Thank you :)