使用 Selenium 测试文本框的验证
我正在尝试使用 Selenium 和 NUnit 测试网页。我的测试用例之一需要验证文本框。使用 Selenium 和 C#,我能够检索在文本框中输入的值。但是,当文本框验证失败时,文本框旁边会显示一条错误消息。
所以,这是我的问题: 1. 如何测试是否因验证失败而引发错误。 2. 我可以得到该错误的文本吗? 3. 或者,我是否偏离了目标,我想做的事情根本不可能。
我尝试读取元素的值,但它似乎总是一个空字符串。
举例来说,我正在尝试测试网页 https://edit.yahoo.com/registration 。当我输入“**我的名字&&”时在名字字段中,出现错误,指出“仅允许使用字母、空格、连字符和撇号”。我希望能够测试是否引发了此错误。
另外,我注意到当 Selenium 打开网页并在文本框中输入不正确的值时,错误消息不会显示在该文本框旁边。然而,当我自己打开网页并输入错误的文本时,会显示错误消息,
谢谢!
I am trying to test a webpage using Selenium and NUnit. One of my test cases entails the validation of text boxes. Using Selenium and C#, I am able to retrieve the value entered in the text box. But when the validation of the text box fails, an error message is displayed next to the text box.
So, here are my questions:
1. How can I test if an error was raised due to validation failure.
2. Can I get the text of that error.
3. Or, am I way off the mark and what I am trying to do is not at all possible.
I have tried reading the value of the element, but it always seems to be an empty string.
Say, for example, I am trying to test the webpage https://edit.yahoo.com/registration . When I enter "**myname&&" in the First Name field, an error appears stating "Only letters, spaces, hyphens, and apostrophes are allowed". I want to be able to test that this error was raised.
Also, I noticed that when Selenium opens the webpage and enters an incorrect value in the text box, the error message does not get displayed next to this text box. Whereas, when I open the webpage myself and enter an incorrect text, the error message is displayed
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将必须使用 thread.sleep,但以更好的方式。最好写一个这样的函数(我是用JAVA写的,你应该可以用C#写)。此方法将等待指定的秒数以使元素可见。如果即使在指定的秒数之后该元素仍然不可见,则该方法将返回 false。如果它变得可见,则该方法将返回 true。
或者,您可以使用断言而不是返回错误条件,以免测试失败。
You will have to use thread.sleep, but in a better way. It's better to write a function like this (I am writing this in JAVA, you should be able to write it for C#). This method will wait for the specified number of seconds for the element to be visible. If the element is not visible even after the specified number of seconds, then the method will return false. If it becomes visible then the method will return true.
Alternatively, you can use an assertion instead of returning a false condition so that your test fails.