[英]Assert a text box is empty in Selenium
我目前正在編寫一個測試,我想知道是否有一種方法可以斷言一個文本框為空。
該測試適用於“清除數據”而不是記住您的電子郵件或用戶名的注銷命令。 我對該測試進行建模的方式包括測試登錄,注銷以及我遇到的問題-斷言退出后登錄屏幕上的電子郵件文本框為空。
我已經嘗試過這樣的事情:
if (driver.findElement(By.cssSelector("input[type=\"text\"]").equals(""))) {
// This will throw an exception
}
但這不起作用,因為參數不被接受。
有任何想法嗎?
我認為您需要獲取value
屬性:
WebElement myInput = driver.findElement(By.cssSelector("input[type=\"text\"]"));
if (!myInput.getAttribute("value").equals("")) {
fail()
}
先前的答案有效,但是斷言可以使代碼更簡潔。 斷言應始終給出一些合理的信息。 這是一個使用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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.