Page 1 of 1

Variables in user code

Posted: Wed Sep 23, 2020 7:47 am
by HansSchl
A recording is a C# class that implements ITestModule, and variables (in the meaning of Ranorex) are properties of this class. Is there a way to figure out, in user code, the value of a variable which is known by name only? I assume I will have to use reflection, but to start with, how can the user code know the currently executing recording?
The background is that I want to write a function that replaces, in a string like "user=$UserName", the variable by its actual value. This function can then be called by other user code, for example to produce formatted output or to create a command line for an external tool.

Re: Variables in user code

Posted: Wed Sep 23, 2020 7:59 am
by odklizec
Hi,

You can find how to get current module name in this post:
viewtopic.php?f=13&t=13583&p=53346

I think that what you are trying to do is just somehow overcomplicated? But maybe I just don't have enough info? ;)

Re: Variables in user code

Posted: Wed Sep 23, 2020 8:26 am
by HansSchl
Hi odklizec,
thank you for the link, I think I'll get the information from the code fragment there.
I think that what you are trying to do is just somehow overcomplicated?
You may be right, and I'm open to better suggestions.

I'll give you some more details. The application under test (AUT) reads its configuration data from a structure which works similar to the Windows registry, and we have a command line tool "RegistryConsole" to manipulate this configuration.
The test case requires to enter a user name into the configuration before launching the AUT. The command line is like "RegistryConsole set user=<username>". I created user code that launches this command and waits until it has finished. However, the <username> comes from the $UserName variable that is bound to a parameter of the test run. I need to fiddle the value of this variable into the command line.
As a temporary solution, I created a user function "string ConcatStrings(string, string)" which appends strings to each other. I pass the constant part "set user=" as first string, and the variable $UserName as second string, then assign the result to a variable which in the next step is passed into the user action that launches RegistryConsole.
However this seems a bit clumsy to me, and I'd prefer to pass "set user=$UserName" as command line, and let my code replace "$UserName" by the actual value. Ranorex seems not to offer this functionality, this is what I understood from one or two other posts in this forum. I'm a programmer, so it is natural to me to write code to solve this type of problems (at the danger, admittedly, of sometimes over-engineering things).

Re: Variables in user code

Posted: Wed Sep 23, 2020 9:55 am
by odklizec
Hi,

Maybe I'm still overlooking something, but why don't you simply merge the variable with command line string in code?

Code: Select all

mergedCMDLine = "set user=" + UserName;
or

Code: Select all

mergedCMDLine = "RegistryConsole set user="  + UserName;
And then simply pass the mergedCMDLine string to command line or whatever you want to use it?
Also, you can create a Global (SmartFolder/TestCase) parameter and simply fill that parameter from command line.

Re: Variables in user code

Posted: Wed Sep 23, 2020 10:41 am
by HansSchl
@odklizec, I think I get what you mean. The <recording>.UserCode.cs file and the <recording>.cs file declare the same (partial) class and hence can access the same properties. That indeed makes your suggestion the easiest way.
Unfortunately (and sorry for not mentioning it), the user code where I want to access the variable is in a library that contains code which is shared across Ranorex projects.

Re: Variables in user code

Posted: Wed Sep 23, 2020 10:47 am
by odklizec
Hi,

Is the library/module a part of test suite and is binded to data connector? In this case, there should not be a problem with merging the variable with cmdline string? Or how exactly do you pass the UserName to the library? I still somehow don't see why you simple cannot merge the string with variable? ;)

Re: Variables in user code

Posted: Fri Sep 25, 2020 7:56 am
by HansSchl
Hi odklizec, and thank you for your help so far. I found an easy solution for my problem:

Code: Select all

DataProviderContext.Current.GetValue(varName);
To answer your question - I wanted to create a universal, one-and-for-all function which I could use to resolve variable references embedded in a string. This can be a command line, or something to write to the log, and it should be possible to call the function as user action and store the result in a variable, or from user code. I know this is coders' thinking, but after all, my main job is coding :)
Your comments (and those which I received from other people) have helped me to discard some ideas which might have lead to the same goal, but with much more effort. (And besides that, I learnt a lot.) Thanks again!
Best regards
Hans

Re: Variables in user code

Posted: Fri Sep 25, 2020 8:01 am
by odklizec
Hi,

Nice to hear you found a solution! This forum is invaluable source of solutions. I'm often searching it to find this or that code snippet. It's funny how often I find my answers with suggestions and code samples, which I already forgot about :D