[英]How can I validate required input fields using Selenium
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.另一件事是我为 2 个学生使用了
for loop
,我希望第一次将输入字段留空。 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).提交后,我必须看到它已被验证,然后填写空的输入字段以再次提交(现在在字段中插入数据)。
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.谢谢。
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.看看您的代码并了解到您是自动化测试的新手,我可以告诉您的第一件事是尽量避免使用
XPath
并尽可能使用cssSelector
。 If you have an ID or CSS class that can identify your element, it's safer and shorter.如果你有一个 ID 或 CSS 类可以识别你的元素,那么它更安全、更短。
Here's a good source to read and understand. 这是阅读和理解的好来源。
After that, I believe you have 2 problems in your question:在那之后,我相信你的问题有两个问题:
When you don't fill the information, the page has a few ways to validate the field (eg 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 - eg "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");
}
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.希望它可以帮助您解决您的问题。
Providing you text is within VALUE of the HTML then the below will work.如果您的文本在 HTML 的 VALUE 范围内,则以下内容将起作用。 For example -
例如——
value="YourText"
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,因此可以验证这个工作比运行整个测试更容易看到它死了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.