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

clipboard content does not refresh during test

我将 webdriver 与 java 一起使用,我想测试从其他文本字段复制内容的按钮。我已经为 return 剪贴板的内容创建了一个方法:

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