如何validationHTML5中的Constraint Validation API生成的消息中的文本?

我测试使用HTML5 Constraint Validation API生成错误弹出窗口的Web应用程序。 如何在selenium测试中validation弹出窗口中的文本? 我找不到合适的定位器。

Selenium API不直接支持约束validation。 但是,您可以使用一段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代码段解决您的问题:

 

我已经浏览了上面的html源代码,看起来非常清楚,但是在点击“提交查询”后生成的validation消息的源代码中没有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. 

希望这对你有意义,并最终帮助你。