1

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:

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

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?

Jasperan
  • 2,154
  • 1
  • 16
  • 40
Alicia
  • 53
  • 8
  • 1
    There is no [MCVE] of the problem... So have to guess here - code does not correctly setup access to Clipboard for selenium tests (non WPF/WinForm app) which is described in standard https://stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp question. Please review linked question and [edit] this post to clarify how your clipboard access code is setup (preferably providing real [MCVE] at the same time) – Alexei Levenkov Dec 10 '19 at 23:41

2 Answers2

1

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)

Jasperan
  • 2,154
  • 1
  • 16
  • 40
Alicia
  • 53
  • 8
0

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.

inejwstine
  • 678
  • 1
  • 10
  • 30
  • I've heard about this method but the issue is that this permit to manually set the Clipboard value with a specified string. I don't know this string so I can't use this. The Clipboard needs to be fill by the two ways I explained above to simulate Copy. – Alicia Dec 10 '19 at 23:07
  • @Alicia Ah, so you don't currently have access to the value that is to be copied to the clipboard. I would recommend finding some way to access it, and then using `Clipboard.SetText`. What is the use case? I'm assuming you want to copy some text that is currently selected. Is that accurate? – inejwstine Dec 10 '19 at 23:18
  • @AlexeiLevenkov I'm not sure what you mean; I've never had a problem with the Clipboard functions mentioned, and I thought I _did_ mention how to read from the clipboard in my answer. If I've misunderstood the question though, I apologize, and I agree that a reproducible example would make Alicia's problem/needs more clear. – inejwstine Dec 11 '19 at 00:15
  • @inejwstine write console app to read/write clipboard (you can easily add `System.Windows.Forms` refence to access `Console` class) to have some problems… Note that it is somewhat said in the question - "test" and "selenium" pretty much guarantees that it is console based app... – Alexei Levenkov Dec 11 '19 at 00:23