【问题标题】:clipboard content does not refresh during test剪贴板内容在测试期间不刷新
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多