How to work with the clipboard properly

Best practices, code snippets for common functionality, examples, and guidelines.
Juan
Posts: 19
Joined: Wed Jul 20, 2016 1:18 pm

How to work with the clipboard properly

Post by Juan » Wed Jul 27, 2016 1:56 pm

Hi all.

I have my code and I'm trying to obtain the value from an input cell by selecting the text with double click, Click CTRL + C and use the clipboard to obtain the value in my code. I have problems because sometimes works and others not.

Code: Select all

//Double click to select all the text
numericEditor.Click();
numericEditor.Click();
Keyboard.Press("{CONTROL DOWN}{C}{CONTROL UP}");
var idata = Clipboard.GetDataObject();
int initialValue = Int32.Parse((string)idata.GetData(DataFormats.Text));
Any ideas? Is there any better option? I tried with Keyboard.press in every single way. One by one and in one line.

Thanks!

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

Re: How to work with the clipboard properly

Post by odklizec » Wed Jul 27, 2016 2:10 pm

Hi,

To double click an element, you should use DoubleClick method instead of two separate clicks.
Additionally, I would suggest to use Ctrl+A shortcut, to select entire input (instead of double clicking the input).

By far the easiest way to read a content of input, is to use GetValue action, where you don't have to select anything or copy the content of input to clipboard. You can use it like this:

Code: Select all

string idata = numericEditor.Element.GetAttributeValueText("InnerText");
Because this is not exactly user-driven action, it should be used with caution.
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

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to work with the clipboard properly

Post by krstcs » Wed Jul 27, 2016 2:11 pm

May I ask why you are using the clipboard?

The clipboard is a Windows OS object and, unless you are testing Windows itself (which I highly doubt), it would be out of scope for your tests.

If you are just trying to validate that a value is set correctly, you can use the Validate.Attribute() action/method on the element. See the Ranorex API documentation for more info: http://www.ranorex.com/Documentation/Ranorex/
Shortcuts usually aren't...

Juan
Posts: 19
Joined: Wed Jul 20, 2016 1:18 pm

Re: How to work with the clipboard properly

Post by Juan » Thu Jul 28, 2016 8:20 am

The main problem is that the input where I want to obtain the value shows a different value while is focused and that's the value I want to get. That's the reason why I use the clipboard to select the text while typing. I think ranorex get methods don't help me with that.

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to work with the clipboard properly

Post by krstcs » Thu Jul 28, 2016 3:16 pm

Could you post a Ranorex Snapshot (not a screenshot!) of the item? This might help us understand the issue.

Also, tell us what value you enter, and what you expect to be displayed, both when focused, and when not focused.
Shortcuts usually aren't...

Juan
Posts: 19
Joined: Wed Jul 20, 2016 1:18 pm

Re: How to work with the clipboard properly

Post by Juan » Fri Jul 29, 2016 8:46 am

This is what happens when I focus in and focus out
Focus.png
Focus.png (9.3 KiB) Viewed 1033 times
And this is the Snapshot
Snapshot.rxsnp
(64.93 KiB) Downloaded 29 times

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to work with the clipboard properly

Post by krstcs » Fri Jul 29, 2016 2:41 pm

So, what happens when you type "10.50"? Does that work, or is this in whole dollars?

I think it would be easier to just validate that the 'Value' attribute is the same as what you entered but formatted as money, which is what this appears to be. If you know that the field formats the text, it is usually easier to just do the same formatting in your code to the input value.

So, take your input value, add '$' to the front and use the "{0:C2}" format string on it:

Code: Select all

var inputValue = "10";

var expectedValue = string.Format("${0:C2}", inputValue);

var actualValue = {Get the actual value from the element};

Validate.AreEqual(actualValue, expectedValue);
Shortcuts usually aren't...

Juan
Posts: 19
Joined: Wed Jul 20, 2016 1:18 pm

Re: How to work with the clipboard properly

Post by Juan » Wed Aug 10, 2016 1:31 pm

The problem is that we use Kendo to format the number so I don't know which could be the symbol because is an special conversion. Is not specified anywhere in ranorex. The only option is getting the value when i'm focused within the cell

User avatar
RobinHood42
Posts: 238
Joined: Fri Jan 09, 2015 3:24 pm

Re: How to work with the clipboard properly

Post by RobinHood42 » Thu Aug 11, 2016 1:54 pm

I agree with krstcs. If you just want to have the not-formatted value, why don't you use the substring method of the .NET Framework?
TagValue = repo.YourRepoElement.GetAttributeValueText("TagValue");
MyTagValue= MyTagValue.Substring(1,MyTagValue.LastIndexOf('.')-1);
Cheers,
Robin

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to work with the clipboard properly

Post by krstcs » Thu Aug 11, 2016 2:19 pm

For any given test scenario, you should know the EXACT expected outcome. You should know what the format SHOULD be every time. The test should behave exactly the same way every time.

So, you should know than when your locale is set to EN-US, that you will have the format "$d.cc" (where 'd' is dollars and 'c' is cents) and be able to account for that in your validation.
Shortcuts usually aren't...