How get a dynamic value and paste it.

Best practices, code snippets for common functionality, examples, and guidelines.
kcochran
Posts: 9
Joined: Thu Sep 16, 2021 3:05 pm

How get a dynamic value and paste it.

Post by kcochran » Thu Sep 16, 2021 3:20 pm

Good day to all,

I am just starting my Ranorex journey, so forgive me if I am missing the basics.

My goal is to get a dynamic value and paste it.



I assume I would be using the GetValue and SetVaule actions. However, I do not know the proper syntax or what attribute to reference.

"/form[@title='Confirm Device Reset']/text[@text='Reset will delete all information and restore your device back to factory settings.If you wish to continue, enter the following digits:26553 ']//div[#'confirmation']/p[@innertext~'digits:*']" ???






I appreciate the assistance,
thanks
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 get a dynamic value and paste it.

Post by Jacob » Fri Sep 17, 2021 11:11 pm

Hi kcochran, welcome to the forums!

This is an excellent question! The answer will depend on the structure of your application a bit. If you can target just the digits, it will be just as easy as you outline: Get Value (store this value as a variable) and then Set Value.

If the entire section "If you wish to continue, enter the following digits: 20752" is one text element, then you'll likely need to use a User Code Method to parse out the digits. For the User Code Method, I would use pass in the text object, use String.Format (or another method that can concatenate strings) to grab the last 5 digits, and then have the method return that result (which you can then store in a variable). From there you should be able to pass that value to the empty text box. As an example, the code would probably look something like this in C#:
        public void grabDigits(Adapter argument1)
        {
                var result = argument1.Element.GetAttributeValueText("Text");  //Get the text value
        	result = result.Substring(result.Length - 4); //Only pull the last four digits
        	digits = result; //Store those digits in the "digits" variable which we can access back in the Recording Module (see below)
        }
In this example, I have a local variable in my recording called "digits" where I'm storing the result of the previous line's Substring action. This User Code Method grabs those digits and stores them in the "digits" variable so you can then use Set Value to pass the value from the "digits" variable into the empty Text Box.

These are just two methods, but if you provide a Snapshot of the application, we may be able to provide some additional methods or insight. I hope this helps point you in the right direction!

--Jacob
Image

kcochran
Posts: 9
Joined: Thu Sep 16, 2021 3:05 pm

Re: How get a dynamic value and paste it.

Post by kcochran » Mon Sep 20, 2021 3:56 pm

Jacob,

Thanks for the warm welcome and the help!

Yes, the entire section is one element, so I guess I will need to read up on the user code method. But attached is the snapshot as requested. If you have any advice after you take a look at the snapshot, please let me know.
Again, thanks for the assistance!!
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 get a dynamic value and paste it.

Post by Jacob » Mon Sep 20, 2021 4:21 pm

Hi kcochran!

It does look like User Code will be the best option (although others in the community may have other thoughts on the matter). The code provided in my last post will work, the only change I would make is to use Length - 5 (instead of Length - 4) to get the last 5 characters. As long as the structure of that text field doesn't change that should work.

--Jacob
Image

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

Re: How get a dynamic value and paste it.

Post by odklizec » Tue Sep 21, 2021 7:24 am

Hi,

In my opinion, the easiest (and codeless) way to get the value from string like this, is to use Get Value action with regex like this:
[^:\s]+$
This should get the numeric value behind colon. So you don't have to use code at all ;) Simply enter the regex to "regex capture" field and Get Value should do the rest.
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

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

Re: How get a dynamic value and paste it.

Post by Jacob » Tue Sep 21, 2021 2:58 pm

odklizec wrote:
Tue Sep 21, 2021 7:24 am
Hi,

In my opinion, the easiest (and codeless) way to get the value from string like this, is to use Get Value action with regex like this:
[^:\s]+$
This should get the numeric value behind colon. So you don't have to use code at all ;) Simply enter the regex to "regex capture" field and Get Value should do the rest.
You know, I never noticed the "Capture regex" option in the Get value Smart Action before. Good call, odklizec!

--Jacob
Image

kcochran
Posts: 9
Joined: Thu Sep 16, 2021 3:05 pm

Re: How get a dynamic value and paste it.

Post by kcochran » Tue Sep 21, 2021 5:36 pm

odklizec and Jacob :) ,

I appreciate the assistance with this problem.

Odklizec: the input would be like this??
So the "/form[@title='Confirm Device Reset']/text[@text='Reset will delete all information and restore your device back to factory settings.If you wish to continue, enter the following digits:26553 ']//div[#'confirmation']/p[@innertext~'digits:[^:\s]+$]"

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

Re: How get a dynamic value and paste it.

Post by odklizec » Wed Sep 22, 2021 7:39 am

Hi,

No, you don't need to apply the regex in the xpath. The xpath should look like this:

Code: Select all

/form[@title='Confirm Device Reset']/text[@text~'following digits:']
And then apply the regex I provided in recording action Get Value, like this:
GetValue_regex.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

kcochran
Posts: 9
Joined: Thu Sep 16, 2021 3:05 pm

Re: How get a dynamic value and paste it.

Post by kcochran » Tue Sep 28, 2021 5:01 pm

Ok, I think I got the Get Value figure out. Now the next step is to get in the textbox. I am having trouble with that. See snapshot and error log.
You do not have the required permissions to view the files attached to this post.
Last edited by kcochran on Tue Sep 28, 2021 5:03 pm, edited 1 time in total.

kcochran
Posts: 9
Joined: Thu Sep 16, 2021 3:05 pm

Re: How get a dynamic value and paste it.

Post by kcochran » Tue Sep 28, 2021 5:02 pm

2021-09-28 10_59_52-Unified Client - Ranorex Studio - (32bit).png
Device_Reset_help.rxsnp
You do not have the required permissions to view the files attached to this post.

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

Re: How get a dynamic value and paste it.

Post by odklizec » Tue Sep 28, 2021 6:42 pm

Hi,

The problem is, that the xpath for given element apparently contains variable string and generally too many words. You must manually optimize the xpath, by removing all variable strings and eventually find better attribute for identification purposes.
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