2016-05-25 16 views
1

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

+1

你能告訴我們你的應用程序的源代碼以及你做了什麼 –

+1

你不需要我的源代碼。我問了如何驗證這些消息中的文本:http://www.sagarganatra.com/2011/07/custom-validation-messages-for-html5.html – Alex

+1

我認爲這是源代碼

回答

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

非常感謝!我用.... getAttribute(「validationMessage」)並得到了消息。 – Alex

1

按你的鏈接提供的示例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. 

希望這是有意義的頂部,你最終可以幫助你。