Assign a value to a variable

Best practices, code snippets for common functionality, examples, and guidelines.
HansSchl
Posts: 58
Joined: Wed Sep 19, 2018 10:05 am

Assign a value to a variable

Post by HansSchl » Wed Aug 19, 2020 10:20 am

Is there an action which I can use in a recording that assigns a constant value to a variable? I created a custom action that takes one string parameter and simply returns this parameter, so I can assign it to a variable, but this appears very clumsy to me. If this action type does indeed not exist, please put it on the feature wishlist.

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

Re: Assign a value to a variable

Post by odklizec » Tue Aug 25, 2020 7:43 am

Hi,

I'm not quite sure I understand what exactly you are trying to do? Could you please post some screenshots, eventually the code you are using?

As for feature requests, it's best to post them here:
https://uservoice.ranorex.com/forums/15 ... ve-ranorex
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

HansSchl
Posts: 58
Joined: Wed Sep 19, 2018 10:05 am

Re: Assign a value to a variable

Post by HansSchl » Tue Aug 25, 2020 8:03 am

Hi odklizec, thank you for your reply. I am attaching an image that is stitched together of 3 screenshots. It is about finding a table row which contains a cell with content "Reference number".
In recording step 5, I store the value "Reference number" in variable $columName. Forget step 6, this is only experimental.
In step 7, I want to access the repository item "RefNo_Row". As you can see from the screenshot, this item refers to $columnName. This can obviously only work if both $columnName are the same variable - but that is another topic, and I have created another post for that subject.
The user code function AssignValue is a simple one-liner, not a big deal, but I wonder whether there is a more elegant solution for storing a value in a variable. It is the kind of thing you would write as 'columnName="Reference number";' in most programming languages.

If you ask for the use case - I am trying to find the capabilities and limitations of Ranorex so that I can most effectively help our tester who are not programmers.

Hans
Attachments
2020-08-25_08h47_01.png
Screenshots as requested
2020-08-25_08h47_01.png (16.05 KiB) Viewed 92 times

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

Re: Assign a value to a variable

Post by odklizec » Tue Aug 25, 2020 8:31 am

Hi,

Thanks for the reply. I'm probably still missing something, because if the recording/repo variable already exists, you may fill it either from connected data source or via user code. From the screenshot you posted, I don't know from where is filled columnName variable, which you are using as a parameter in step 5. If columnName is already filled by an user code in steps 1-4, eventually from assigned data connector, I don't see a meaningfulness of step 5, because how I see it, columName is already filled?
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

HansSchl
Posts: 58
Joined: Wed Sep 19, 2018 10:05 am

Re: Assign a value to a variable

Post by HansSchl » Tue Aug 25, 2020 8:39 am

Sorry I failed to screenshot that one line... $columnName is assigned from the return value of the AssignValue function in step 5. The string "Reference number" is the function argument.
Attachments
2020-08-25_09h37_11.png
2020-08-25_09h37_11.png (6.2 KiB) Viewed 89 times

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

Re: Assign a value to a variable

Post by odklizec » Tue Aug 25, 2020 9:10 am

Hi,

Thanks, now I understand ;)

Well, I think that the most elegant (and most recommended) solution is to use data connector, instead of hardcoding "Reference Number" string directly in recording. Basically, you should create a data connector, with 'columnName' column, which should be connected to 'columnName' recording/repo variable. Eventually, if you don't want to create a data connector just for one variable, you may use Test Case/Smart Folder parameter. And if you don't want to use data connector, you can still add "Reference Number" string as a default value in columnName definition...
varDefaulVal.png
varDefaulVal.png (4.73 KiB) Viewed 86 times
But in this case, you should expect "unbound variable" warning in report. So I think using data connector is most elegant solution?
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

HansSchl
Posts: 58
Joined: Wed Sep 19, 2018 10:05 am

Re: Assign a value to a variable

Post by HansSchl » Thu Aug 27, 2020 9:49 am

Thank you for pointing me into the right direction. In my case, using a parameter was the solution. In fact, I split my recording so that the value of the parameter is created in a recording different from where it is used, which makes parameters a natural thing to be involved.