java 如何使用 Selenium 验证必需的输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40041565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I validate required input fields using Selenium
提问by Ahmed Ali QA Engineer
I'm new in automation testing and have a task to implement the automation to Register Student using a given Registration Form.
我是自动化测试的新手,有一项任务是使用给定的注册表来实现自动化以注册学生。
The scenario is letting me a little bit confused on how to validate the input field in the registration form.
这个场景让我对如何验证注册表中的输入字段有点困惑。
I mean, if I don't fill a required field, the browser will indicate that I must fill that field.
我的意思是,如果我不填写必填字段,浏览器将指示我必须填写该字段。
Another thing is that I'm using a for loop
for 2 students, where I want the first time to leave the input fields empty. After submit I have to see it was validated and then fill the empty input fields to submit it again (now with data inserted in the fields).
另一件事是我正在使用for loop
2 个学生,我希望第一次将输入字段留空。提交后,我必须看到它已被验证,然后填写空的输入字段以再次提交(现在在字段中插入数据)。
I'm sharing my code and would appreciate any help.
我正在分享我的代码,希望得到任何帮助。
public class Cartus {
public static void fill() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "E:\workspace\chromeDriver\chromeDriver.exe");
ChromeDriver driver = new ChromeDriver();
for(int i=0; i<2; i++) {
driver.get("http://qa-5.ls.vu/v2/landing-page/cartus/en");
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[1]/div")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[1]/div/select/option[2]")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys(/*"sami"*/);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys(/*"Mohaname"*/);
WebElement ele=driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input"));/*.sendKeys("0221-1234567");*/
String date = FastDateFormat.getInstance("yyyyMMddHHmmss").format(System.currentTimeMillis());
Thread.sleep(1000);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input"))
.sendKeys(/*"aali_"+date+"@outlook.com"*/);
driver.findElement(By.id("submitButton")).click();
if(ele.getAttribute("value").isEmpty()){
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys("abcds");
}
if(ele.getAttribute("value").isEmpty()){
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys("abcds");
}
if(ele.getAttribute("value").isEmpty()){
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input")).sendKeys("abcds");
}
if(ele.getAttribute("value").isEmpty()){
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input")).sendKeys("aali_"+date+"@outlook.com");
}
System.out.println("Test case succesfully run");
}
}
}
Thank you.
谢谢你。
回答by Tom
Taking a look at your code and reading that you're new to automation tests, the first thing I can tell you is to try to avoid XPath
and use cssSelector
when possible.
If you have an ID or CSS class that can identify your element, it's safer and shorter.
查看您的代码并阅读您对自动化测试不熟悉,我可以告诉您的第一件事是尽可能避免XPath
和使用cssSelector
。如果您有一个 ID 或 CSS 类可以识别您的元素,则它更安全、更短。
Here'sa good source to read and understand.
这是一个很好的阅读和理解来源。
After that, I believe you have 2 problems in your question:
在那之后,我相信你的问题有两个问题:
- How to test the validation for fields left empty
- 如何测试对空字段的验证
When you don't fill the information, the page has a few ways to validate the field (e.g. after the input has lost its focus, after clicking the button to submit the form etc).
当您不填写信息时,页面有几种方法来验证该字段(例如,在输入失去焦点后,单击按钮提交表单后等)。
In any way, you must check the HTML that was hidden and is then shown (with the warning message - e.g. "Field name is required") and use Selenium
to check if this element is now present.
无论如何,您必须检查隐藏然后显示的 HTML(带有警告消息 - 例如“需要字段名称”)并用于Selenium
检查该元素现在是否存在。
// findElements (using the plural) will not result in "Element not found exception"
WebElement nameMessage = driver.findElements(By.id("nameErrorMessage"));
if (nameMessage.size > 0) {
nameMessage.get(0).getText(); // do your validation to check if the message is the one expected
} else {
throw new Exception("Warning message was not displayed properly");
}
- To have a loop for different data
- 对不同的数据进行循环
In this situation, you could have an object to hold the information used in the form, put it in a list and use it.
在这种情况下,您可以有一个对象来保存表单中使用的信息,将其放入列表中并使用它。
Here an example of how it could be:
这是一个如何实现的示例:
class Student {
private String name;
private Date birthday;
private String className;
public Student(String name, Date birthday, String class) {
this.name = name;
this.birthday = birthday;
this.className = class;
}
// all getters and setters
}
class StudentTest {
Student emptyStudent = new Student("", null, "");
Student student = new Student("John", new Date(), "7th grade");
List<Student> students = Arrays.asList(emptyStudent, student);
// Set up your selenium
// Loop your data to be tested
students.forEach(student -> {
// Fill the information with the student info
driver.findElement(By.id("name")).sendKeys(student.getName());
driver.findElement(By.id("birthday")).sendKeys(student.getBirthday());
driver.findElement(By.id("className")).sendKeys(student.getClassName());
// Submit the form
driver.findElement(By.id("save")).click();
// Check if warning message is shown (code the logic properly to work with and without it)
});
}
Hope it helps you solve your problem.
希望它可以帮助您解决您的问题。
回答by CalAtk
Providing you text is within VALUE of the HTML then the below will work. For example -
如果您的文本在 HTML 的 VALUE 范围内,则以下内容将起作用。例如 -
value="YourText"
value="YourText"
var UniqueName = YourElement.NameGoesHere.GetAttribute("value");
var UniqueName = YourElement.NameGoesHere.GetAttribute("value");
When debugging put a break point just after the above and hover over the UniqueName and youll see your text is returned and you do not get a NULL and thus can validate this works alot easier than running the whole test to just see it die.
调试时在上面的后面放置一个断点并将鼠标悬停在 UniqueName 上,你会看到你的文本被返回并且你没有得到 NULL,因此可以验证这个工作比运行整个测试更容易看到它死了。