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

时间:2015-04-09 11:42:05

标签: java automated-tests 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));
}

1 个答案:

答案 0 :(得分:0)

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

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