【问题标题】:Assert a text box is empty in Selenium在 Selenium 中断言文本框为空
【发布时间】:2015-09-24 14:11:59
【问题描述】:

我目前正在编写一个测试,我想知道是否有办法断言文本框为空。

测试是针对“清除数据”而不是记住您的电子邮件或用户名的注销命令。我为这个测试建模的方式有测试登录、注销,以及我卡在的部分 - 断言登录屏幕上的电子邮件文本框在注销后是空的。

我试过这样的东西:

if (driver.findElement(By.cssSelector("input[type=\"text\"]").equals(""))) {
    // This will throw an exception
}

但这不起作用,因为参数不被接受。

有什么想法吗?

【问题讨论】:

    标签: java selenium selenium-webdriver junit


    【解决方案1】:

    我认为您需要获取value 属性:

    WebElement myInput = driver.findElement(By.cssSelector("input[type=\"text\"]"));
    
    if (!myInput.getAttribute("value").equals("")) {
        fail()
    }
    

    【讨论】:

      【解决方案2】:

      前面的答案有效,但断言可以更清晰。断言应该总是给出一些合理的信息。这是一个使用 JUnit assertThat 和 hamcrest 匹配器的示例。

      import org.junit.Assert;
      import static org.hamcrest.Matchers.isEmptyString;
      ...
      WebElement myInput = driver.findElement(By.cssSelector("input[type=\"text\"]"));
      Assert.assertThat(myInput.getAttribute("value"), isEmptyString());
      

      或者更好的是,给出原因信息:

      Assert.assertThat("Field should be empty", myInput.getAttribute("value"), isEmptyString());
      

      【讨论】:

        猜你喜欢
        • 2020-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-15
        • 1970-01-01
        • 1970-01-01
        • 2011-02-18
        • 1970-01-01
        相关资源
        最近更新 更多