Reputation: 53
I am working on a test for a Web Application. I want to simulate the Copy command and verify the value of the Clipboard.
I have two ways to simulate this:
System.Windows.Forms.SendKeys.SendWait("^{c}");
These two work and after using one of them, I can do "Ctrl+V" and it pastes the text correctly.
On my test, I am supposed to verify that the Clipboard contains the correct value.
I am using this code to check if the Clipboard is not empty and that it contains the correct string:
Clipboard.ContainsText(); // verify that Clipboard is not empty
Clipboard.GetText(); // verify that string on the Clipboard contains the good string
But after I simulate a Copy (with one of options above), the code just above returns respectively:
false
""
Does anyone have a solution to fill the Clipboard and to see its contents?
Upvotes: 1
Views: 2907
Reputation: 53
Ok I added this on the attribute of my test :
[Apartment(ApartmentState.STA)]
I can access the value of the Clipboard now.
(Source: https://docs.nunit.org/articles/nunit/writing-tests/attributes/apartment.html)
Upvotes: 1
Reputation: 748
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.
Upvotes: 0