如何验证HTML5中的Constraint Validation API生成的消息中的文本? [英] How to verify text in message generated by Constraint Validation API in HTML5?

查看:94
本文介绍了如何验证HTML5中的Constraint Validation API生成的消息中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I test web application that uses HTML5 Constraint Validation API to generate error pop-ups. How to verify text in the pop-ups in selenium tests? I cannot find the appropriate locators.

推荐答案

Selenium API不直接支持约束验证。但是,您可以使用一段JavaScript(Java)轻松获取状态和消息:

The Selenium API doesn't support directly the constraint validation. However, you could easily get the state and the message with a piece of 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 ,即使它是属性:

Note that it is also possible to use getAttribute to get the validationMessage even though it is a property:

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

这篇关于如何验证HTML5中的Constraint Validation API生成的消息中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆