Returning a value from User Code into a recorder variable

Class library usage, coding and language questions.
tgagel
Posts: 33
Joined: Tue Jun 04, 2013 7:50 pm

Returning a value from User Code into a recorder variable

Post by tgagel » Wed Jul 15, 2015 6:11 pm

A developer has created a small C# user code snippet to allow those building tests to parse some formatted text passed into the user code method and pull out certain pieces for later use. I intended to store the returned data in a recorder variable (similar to using the 'Get Value' action to populate a variable) but can't find a way to do that.

When calling user code from Ranorex Recorder Script, is there a way to capture the returned value from the user code? As you can see in the screenshot, I am able to specify the 2 parameters for the user code method but I don't see any Ranorex options for grabbing the output. Ideally I am looking for something that combines the User Code and Get Value (storing something in a variable) Actions.

More info on the user code:
A single static method to the CodeLibrary class, GetTextFromJson(string json, string propertyName).

The first parameter is the full Json string read off the page. (e.g. {"Organization":"OrgID1","Abbreviation":"35225","BatNumber":"2272","ComNumber":"03986"})

The second is the name of the field you'd like the text from, for example "Organization".

The resulting value (e.g. "OrgID1") is returned by the method.
Attachments
User Code.JPG
User Code.JPG (29.85 KiB) Viewed 3895 times

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

Re: Returning a value from User Code into a recorder variable

Post by odklizec » Thu Jul 16, 2015 8:02 am

Hi,

If you want to use the return value from UserCode within the same recording module, then all you need to do is to create a new recording module variable (e.g. returnVal) and then assign this variable with return value from user code. This must be done within the UserCode itself...
ReturnValue.png
ReturnValue.png (45.57 KiB) Viewed 3883 times
If you want to share the UserCode result amongst multiple modules, learn here how to do this properly:
http://www.ranorex.com/blog/sharing-dat ... to-another
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

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

Re: Returning a value from User Code into a recorder variable

Post by Support Team » Thu Jul 16, 2015 1:43 pm

Hi odklizec,
Thank you for your answer we really appreciate your work in this forum, but I have to correct you in this regarding.

You can bind the return value of a user code module direct to a module variable by opening the properties tab of your code action as shown here
ReturnValueUserCode.png
ReturnValueUserCode.png (13.21 KiB) Viewed 3878 times
Please also find attached a short sample solution.
ReturnValue.zip
(10.79 KiB) Downloaded 290 times
Regards,
Markus (S)
.
Image

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

Re: Returning a value from User Code into a recorder variable

Post by odklizec » Thu Jul 16, 2015 2:04 pm

Hi Markus,

Cool! One learns something new every day ;)
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

tgagel
Posts: 33
Joined: Tue Jun 04, 2013 7:50 pm

Re: Returning a value from User Code into a recorder variable

Post by tgagel » Thu Jul 16, 2015 2:24 pm

Thanks. That's exactly what I needed!