Java 如何使用Selenium验证必需的输入字段
我是自动化测试方面的新手,有一项任务是使用给定的注册表实现自动注册学生 这个场景让我对如何验证注册表中的输入字段有点困惑 我的意思是,如果我没有填写必填字段,浏览器将指示我必须填写该字段 另一件事是,我为两个学生使用了一个Java 如何使用Selenium验证必需的输入字段,java,selenium,selenium-webdriver,automation,Java,Selenium,Selenium Webdriver,Automation,我是自动化测试方面的新手,有一项任务是使用给定的注册表实现自动注册学生 这个场景让我对如何验证注册表中的输入字段有点困惑 我的意思是,如果我没有填写必填字段,浏览器将指示我必须填写该字段 另一件事是,我为两个学生使用了一个for循环,我希望第一次将输入字段留空。提交后,我必须查看它是否经过验证,然后填写空输入字段以再次提交(现在在字段中插入数据) 我正在分享我的代码,并将感谢任何帮助 public class Cartus { public static void fill() th
for循环
,我希望第一次将输入字段留空。提交后,我必须查看它是否经过验证,然后填写空输入字段以再次提交(现在在字段中插入数据)
我正在分享我的代码,并将感谢任何帮助
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");
}
}
}
公共类Cartus{
public static void fill()引发InterruptedException{
System.setProperty(“webdriver.chrome.driver”,“E:\\workspace\\chromeDriver\\chromeDriver.exe”);
ChromeDriver驱动程序=新的ChromeDriver();
对于(int i=0;i查看您的代码并了解到您是自动化测试的新手,我能告诉您的第一件事是尽量避免使用XPath
,并尽可能使用cssSelector
。
如果您有一个ID或CSS类可以识别您的元素,那么它会更安全、更短
一个很好的阅读和理解的来源
之后,我相信你的问题中有两个问题:
如何测试留空字段的验证
当您不填写信息时,页面有几种方法验证字段(例如,在输入失去焦点后,单击按钮提交表单后等)
无论如何,您必须检查隐藏的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");
}
为不同的数据创建一个循环
在这种情况下,您可以使用一个对象来保存表单中使用的信息,将其放入列表中并使用它
下面是一个示例,说明了它是如何实现的:
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)
});
}
班级学生{
私有字符串名称;
私人约会生日;
私有字符串类名;
公立学生(字符串名称、日期、生日、字符串类){
this.name=名称;
这个生日=生日;
this.className=class;
}
//所有的能手和二传手
}
班级学生测验{
Student emptyStudent=新学生(“,null)”;
学生=新生(“约翰”,新日期(),“七年级”);
List student=Arrays.asList(emptyStudent,student);
//设置您的硒
//循环要测试的数据
学生。forEach(学生->{
//用学生信息填写信息
driver.findElement(By.id(“name”)).sendKeys(student.getName());
driver.findElement(By.id(“生日”)).sendKeys(student.getBirthday());
driver.findElement(By.id(“className”)).sendKeys(student.getClassName());
//提交表格
driver.findElement(By.id(“保存”))。单击();
//检查是否显示警告消息(对逻辑进行正确编码,以便在有和无警告消息的情况下工作)
});
}
希望它能帮助您解决问题。看看您的代码,了解到您是自动化测试新手,我能告诉您的第一件事是尽量避免使用XPath
并尽可能使用cssSelector
。
如果您有一个ID或CSS类可以识别您的元素,那么它会更安全、更短
一个很好的阅读和理解的来源
之后,我相信你的问题中有两个问题:
如何测试留空字段的验证
当您不填写信息时,页面有几种方法验证字段(例如,在输入失去焦点后,单击按钮提交表单后等)
无论如何,您必须检查隐藏的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");
}
为不同的数据创建一个循环
在这种情况下,您可以使用一个对象来保存表单中使用的信息,将其放入列表中并使用它
下面是一个示例,说明了它是如何实现的:
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)
});
}
班级学生{
私有字符串名称;
私人约会生日;
私有字符串类名;
公立学生(字符串名称、日期、生日、字符串类){
this.name=名称;
这个生日=生日;
this.className=class;
}
//所有的能手和二传手
}
班级学生测验{
Student emptyStudent=新学生(“,null)”;
学生=新生(“约翰”,新日期(),“七年级”);
List student=Arrays.asList(emptyStudent,student);
//设置您的硒
//循环要测试的数据
学生。forEach(学生->{
//用学生信息填写信息
driver.findElement(By.id(“name”)).sendKeys(student.getName());
driver.findElement(By.id(“生日”)).sendKeys(student.getBirthday());
driver.findElement(By.id(“className”)).sendKeys(student.getClassName());
//提交表格
driver.findElement(By.id(“保存”))。单击();
//检查是否显示警告消息(对逻辑进行正确编码,以便在有和无警告消息的情况下工作)
});
}
希望它能帮助您解决问题。如果您的文本在HTML值范围内,那么下面的内容就可以了。例如-
value=“YourText”
var UniqueName=YourElement.namegoeSher.GetAttribute(“值”);
调试时,将断点放在上面的后面,并将鼠标悬停在UniqueName上,您将看到返回的文本,并且不会得到NULL,因此验证此操作要比运行整个测试只看到它消失要容易得多。如果您的文本在HTML值内,则下面的操作将有效。例如-
value=“YourText”
var UniqueName=YourElement.Na