WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Accessing Clipboard

Open Senerader opened this issue 7 years ago • 4 comments

Hey, I am using WinAppDriver for an IDE test automation and I went in a situation where I was required to compare a specific portion of text inside an editor to a template. I am not able to access this portion of the text directly. You can either save the whole text inside of an editor to a string using Element.Text; or you can select this portion of the text, using IDE functionality and keyboard input, and copy it to the Clipboard using a keyboard shortcut Ctrl + c. I thought it would be possible to read data from the Windows Clipboard inside of my script, however, whenever I was trying to add the corresponding library (for Clipboard class), references inside of my program had become broken (not possible to use locators). All in all, I want to access the Window Clipboard to store the data inside to a string in order to perform some comparisons to relevant templates. Could you, please, suggest a solution or give a hint of how is it possible to overcome the issue? Thank you.

Senerader avatar Jun 28 '19 16:06 Senerader

Could you share your code? I would like to see it and perhaps I can help you.

riccione avatar Jul 07 '19 01:07 riccione

Sure, for simplicity I will demonstrate it on NotepadTest sample:

        public void EditorKeyboardShortcut()
        {
            // Type a known text sequence, select, copy, and paste it three times
            editBox.SendKeys("789");
            editBox.SendKeys(Keys.Control + "a" + Keys.Control); // Select all using Ctrl + A keyboard shortcut
            editBox.SendKeys(Keys.Control + "c" + Keys.Control); // Copy using Ctrl + C keyboard shortcut

Here editBox represents the main editor in a notepad, so firstly I send a text to the editor ("789"), select all the text inside of the editor using Ctrl+a shortcut, and, finally, I copy selected text by using "Ctrl+c" shortcut. My question is how can I access the text I have just copied to a windows clipboard in a C# script?

Senerader avatar Jul 08 '19 15:07 Senerader

What do you mean by "references inside of my program had become broken (not possible to use locators)"? For accessing the Clipboard you can either use the WPF class from System.Windows or the Forms class from System.Windows.Forms. As a reference look this stackoverflow post

stefantinger avatar Jul 09 '19 05:07 stefantinger

I fixed the issue I was trying to describe. Indeed, class System.Windows.Forms is required. Moreover, you have to add it in Visual Studio references in an explorer window manually, and then include the namespace to a script. The problem was that when you include the namespace, nonprintable Keys enumerator from Appium and Keys from Windows Forms were conflicting because they have the same name. So, in order to fix the issue, you just have to precede Keys, which I use to simulate non-printable keys, with the Appium namespace, not the Windows Forms one.

Senerader avatar Jul 11 '19 14:07 Senerader