我正在「添加新的用戶表單」。在這種形式下,有幾個必填字段。當任何必填字段爲空時,表單將不會被提交,並且將顯示驗證消息,如:「值是必需的且不能爲空」。如何檢查/自動化表單域是否爲空?如何在使用Java的Selenium WebDriver中檢查必填字段是否爲空?
2
A
回答
1
方法1: 首先,寫出下面的方法:
private boolean isTextPresent(String text){
try{
boolean b = driver.getPageSource().contains(text);
return b;
}
catch(Exception e){
return false;
}
}
現在做斷言預期的消息是否是在頁面上存在或不通過調用上述方法:
assertTrue(isTextPresent("Value is required and can't be empty"), "Msg is absent/wrong/misspelled");
路2: 另一種方法如下:
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
private StringBuffer verificationErrors = new StringBuffer();
try {
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Value is required and can't be empty[\\s\\S]*$"));
} catch (Error e) {
verificationErrors.append(e.toString());
}
}
1
可以使用assert
檢查必填字段警報出現與否,用的try-catch
try {
assertEquals("Value is required and can't be empty", driver.findElement(By.xpath("XPATH_OF_LABLE")).getText());
} catch (Error e) {
//code for the case when the texts are not same
verificationErrors.append(e.toString());
}
的例子中,使用XPath,但你可以使用任何其他定位方法這一點。
+0
你的一個也workin g.非常感謝! –
相關問題
- 1. 檢查是否有多個必填字段爲空
- 2. 使用Selenium Webdriver檢查字段錯誤
- 3. 如何測試/驗證Selenium webdriver中的必填字段
- 4. 如何檢查是否填寫了必填字段?
- 5. CodeIgniter:如何檢查其中一個字段是否爲必填字段
- 6. 如何使用Java在Selenium WebDriver中檢查元素是否可點擊
- 7. 如何使用Selenium WebDriver檢查網站是否使用Ajax?
- 8. 如何檢查彈出窗口是否打開使用Java中的Selenium WebDriver
- 9. 檢查文本字段是否爲空
- 10. 如何檢查dom是否有使用WebDriver(Selenium 2)的類?
- 11. 如何檢查html表單中的必填字段是否已填充?
- 12. 獲取必填字段並檢查是否爲真
- 13. 如何在Java中使用Selenium Webdriver檢查複選框?
- 14. 如何使用selenium webdriver java在Search Criteria字段中輸入值?
- 15. 我想用JavaScript驗證我的必填字段是否爲空
- 16. Laravel auth檢查字段是否爲空
- 17. 如何檢查表單中的任何字段是否爲空
- 18. 在Python中,如何檢查Selenium WebDriver是否退出?
- 19. PHP檢查字段是否爲空
- 20. mysql檢查是否爲空字段
- 21. 檢查winforms字段是否爲空
- 22. Django檢查字段是否爲空?
- 23. laravel 5.2 - 檢查字段是否爲空
- 24. PHP檢查是否爲空字段
- 25. 檢查輸入字段是否爲空
- 26. phpMailer檢查字段是否爲空
- 27. 檢查字段是否爲空
- 28. 如何檢查drupal 8中的字段是否爲空?
- 29. 如何檢查ABNewPersonViewController中的所有字段是否爲空
- 30. 如何檢查codeigniter中的文件字段是否爲空?
第二個是更可取的 –
兩個選項都工作正常。非常感謝! –
但最後我用第二個。 –