Java 测试期间剪贴板内容不刷新

Java 测试期间剪贴板内容不刷新,java,automated-tests,clipboard,Java,Automated Tests,Clipboard,我在java中使用webdriver,我想测试从其他文本字段复制内容的按钮。我创建了一个返回剪贴板内容的方法: private String getClipboardContents() throws Exception { java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable contents=clipboard

我在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));
}

最后,代码不是问题,而是时间问题。 我不知道为什么第一次点击按钮复制剪贴板内容有效,第二次却没有。 我在点击按钮事件后添加了一个简单的睡眠,现在效果很好,例如:

 @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 {
   Thread.sleep(1000);
    } catch (InterruptedException interrupt) {
    }   
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")));
idGT.getButton().click();
browser.wait.until(loadingMarkerGone());
browser.findElement(By.id("IDClip")).click();
try {
    Thread.sleep(1000);
    } catch (InterruptedException interrupt) {
    }
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));  
}