简体   繁体   中英

Simulate "Copy" and check text on Clipboard

I am working on a test for a Web Application. So I want to simulate Copy and verify the value of the Clipboard.

I have two ways to make a copy :

  1. I simulate "Ctrl+C" by using this code:
System.Windows.Forms.SendKeys.SendWait("^{c}");
  1. I use a button on my App which Copy a text and put it on the Clipboard

This two works and after using one of them, I can do "Ctrl+V" and it paste the text correctly.

On my test, I am supposed to verify that the Clipboard contains the good value.

I am using this code to check if the Clipboard is not empty and that it contains the good string :

Clipboard.ContainsText(); // verify that Clipboard is not empty
Clipboard.GetText(); // verify that string on the Clipboard contains the good string

But after simulate a copy (with one of options above), the code just above return respectively :

false

""

Does anyone have a solution to fill the Clipboard and to see its content ?

You can use Clipboard.SetText("your text here") to manually set text to the clipboard. Afterwards, you can use Clipboard.ContainsText() and Clipboard.GetText() to check the clipboard contents as you mentioned above.

See the documentation here .

Ok I added this on the attribute of my test :

Apartment(apartmentState.STA)

I can access the value of the Clipboard now.

(Source: https://github.com/nunit/docs/wiki/Apartment-Attribute )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM