簡體   English   中英

如何驗證HTML5中的約束驗證API生成的消息中的文本?

[英]How to verify text in message generated by Constraint Validation API in HTML5?

我測試了使用HTML5約束驗證API生成錯誤彈出窗口的Web應用程序。 如何在硒測試中的彈出窗口中驗證文本? 我找不到合適的定位器。

Selenium API不直接支持約束驗證。 但是,您可以使用一段JavaScript(Java)輕松獲得狀態和消息:

JavascriptExecutor js = (JavascriptExecutor)driver;

WebElement field = driver.findElement(By.name("email"));
Boolean is_valid = (Boolean)js.executeScript("return arguments[0].checkValidity();", field);
String message = (String)js.executeScript("return arguments[0].validationMessage;", field);

請注意,即使它是一個屬性,也可以使用getAttribute來獲取validationMessage

String message = driver.findElement(By.name("email")).getAttribute("validationMessage");

按照您的鏈接提供的示例HTML代碼段(可解決您的問題)是:

<form id="myForm">
<input id="eid" name="email_field" type="email" />
<input type="submit" />
</form>

我已經查看了上面的html源代碼,並非常清楚地查看了它,但是單擊“提交查詢”后生成的驗證消息源代碼中沒有html源代碼。

但是,我可以指導您提出一種變通辦法,當您收到錯誤消息時,該變通辦法將像已經正常工作一樣好。

請理解這種方式:

1.Please observer source code of input boxes with Constraint Validation API in HTML5?
2.you will clearly see that its attribute defines what type of data they can take in 
  our example attribute name and type.
3.they both clearly say that above html code will take input in the form of email only.
4.so your logic will be to check value entered in the input box is of the type of 
  email or not.if its email then pass the test otherwise fail.

希望這對您有幫助,並最終對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM