简体   繁体   English

模拟“复制”并检查剪贴板上的文本

[英]Simulate "Copy" and check text on Clipboard

I am working on a test for a Web Application.我正在测试 Web 应用程序。 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:我使用以下代码模拟“Ctrl+C”:
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.这两个有效,使用其中一个后,我可以执行“Ctrl + V”并正确粘贴文本。

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.您可以使用Clipboard.SetText("your text here")手动将文本设置到剪贴板。 Afterwards, you can use Clipboard.ContainsText() and Clipboard.GetText() to check the clipboard contents as you mentioned above.之后,您可以使用Clipboard.ContainsText()Clipboard.GetText()如上所述检查剪贴板内容。

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 ) (来源: https : //github.com/nunit/docs/wiki/Apartment-Attribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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