【问题标题】:How to verify text in message generated by Constraint Validation API in HTML5?如何在 HTML5 中验证约束验证 API 生成的消息中的文本?
【发布时间】:2016-09-22 18:48:27
【问题描述】:

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

【问题讨论】:

  • 你能告诉我们你的应用程序的源代码和你做了什么
  • 你不需要我的源代码。我问如何验证这些消息中的文本:sagarganatra.com/2011/07/…
  • 我认为这是源代码
  • 在您的网站中,消息在图像中。从图像中您无法获取文本。

标签: java html selenium-webdriver


【解决方案1】:

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");

【讨论】:

  • 非常感谢!我使用了 ....getAttribute("validationMessage") 并收到了消息。
【解决方案2】:

根据您的链接提供的解决您的问题的示例 HTML 代码 sn-p 是:

<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.

希望这对您有意义并最终对您有所帮助。

【讨论】:

    猜你喜欢
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多