【发布时间】:2015-04-20 22:08:05
【问题描述】:
我正在使用带有 java 的 webdriver,我想测试从其他文本字段复制内容的按钮。我创建了一个返回剪贴板内容的方法:
private String getClipboardContents() throws Exception {
java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents=clipboard.getContents(null);
boolean hasTransferableText=(contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
if (hasTransferableText) {
return (String)contents.getTransferData(DataFlavor.stringFlavor);
}
else {
return null;
}
}
我相信,这很好用。 当我将内容复制到变量并更改文本字段的值之后,再次单击按钮以从剪贴板复制内容我从 getClipboardContents() 获取剪贴板的相同内容。我不知道为什么内容不刷新并保持不变,我什至尝试在第一次和第二次按下按钮的中间清除剪贴板,但第二个内容却为空。
我的测试片段:
@Test
public void checkClipboardForCopy() {
String cFirstContent = null;
String cSecondContent = null;
IIDG idGenTab = goToIdG();
idGT.getRadioButton().click();
idGT.getButton().click();
browser.wait.until(loadingMarkerGone());
browser.findElement(By.id("IDClip")).click();
try {
cFirstContent = getClipboardContents();
} catch (Exception e) {
e.printStackTrace();
}
String firstValue = idGT.getNewId().getAttribute("value") ;
verifyThat("Value of clipboard", cFirstContent, not(isEmptyOrNullString()));
verifyThat("Value of id", idGT.getNewId().getAttribute("value"), not(isEmptyOrNullString()));
verifyThat("Compare value of first clipboard content to attribute value", cFirstContent, equalTo(idGT.getNewId().getAttribute("value")));
// clearClipboardContent();
idGT.getButton().click();
browser.wait.until(loadingMarkerGone());
browser.findElement(By.id("IDClip")).click();
try {
cSecondContent = getClipboardContents();
} catch (Exception e) {
e.printStackTrace();
}
String secondValue = idGT.getNewId().getAttribute("value") ;
verifyThat("Value of clipboard", cSecondContent, not(isEmptyOrNullString()));
verifyThat("Value of id", idGT.getNewId().getAttribute("value"), not(isEmptyOrNullString()));
verifyThat("Compare value of second clipboard content to attribute value", cSecondContent, equalTo(idGT.getNewId().getAttribute("value")));
verifyThat("Compare value of first and second clipboard contents", cSecondContent, greaterThan(cFirstContent));
}
【问题讨论】:
标签: java automated-tests clipboard