我想在同一注册表中注册3名学生。当一名学生注册时,我想注册另一名学生使用相同的表格,只是在同一领域有不同的数据。
package Database;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Bayer extends Landindpage {
public void Bayerfill (){
System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("http://qa-0.ls.vu/v2/landing-page/bayer/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("Emmaname");
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys("Mohaname");
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input")).sendKeys("0221-1234567");
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input")).sendKeys("aali3005@gmai.com");
driver.findElementById("submitButton").click();
}
}
答案 0 :(得分:1)
当您将所有代码都放在这样的方法中时,它不是非常可重复使用的。这意味着对于下一个用例,您可能只是复制/粘贴相同的代码,然后稍微改变它。像这样重复自己是不好的做法。
不要让所有东西都在一个方法中,你应该重构一些事情。您设法破坏功能的较小模块,它们越有可能用于您开始工作的下一个用例。
为您的学生详细信息创建课程:
public class Student {
private String firstName;
private String lastName;
private String phoneNumber;
private String email;
// generate setters and getters
}
更改您的类以处理使用动态输入命令WebDriver:
package Database;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Bayer extends Landindpage {
private static final String FORM_URL = "http://qa-0.ls.vu/v2/landing-page/bayer/en";
private static final String SOME_DIV_XPATH = "/html/body/div[4]/div/form/div[1]/div";
private static final String SELECT_XPATH = "/html/body/div[4]/div/form/div[1]/div/select/option[2]";
private static final String FIRST_NAME_XPATH = "/html/body/div[4]/div/form/div[2]/div[1]/input"
private static final String LAST_NAME_XPATH = "/html/body/div[4]/div/form/div[2]/div[2]/input";
private static final String PHONE_NUMBER_XPATH = "/html/body/div[4]/div/form/div[3]/div[1]/input";
private static final String EMAIL_XPATH = "/html/body/div[4]/div/form/div[3]/div[2]/input";
private static final String SUBMIT_ID = "submitButton";
private ChromeDriver driver;
public Bayer() {
driver = new ChromeDriver();
}
public void loadForm() {
driver.get(FORM_URL);
}
public void fillUpForm(Student student) {
driver.findElement(By.xpath(SOME_DIV_XPATH)).click();
driver.findElement(By.xpath(SELECT_XPATH)).click();
driver.findElement(By.xpath(FIRST_NAME_XPATH)).sendKeys(student.getFirstName());
driver.findElement(By.xpath(LAST_NAME_XPATH)).sendKeys(student.getLastName());
driver.findElement(By.xpath(PHONE_NUMBER_XPATH)).sendKeys(student.getPhoneNumber());
driver.findElement(By.xpath(EMAIL_XPATH)).sendKeys(student.getEmail());
}
public void submitForm() {
driver.findElementById(SUBMIT_ID).click();
}
}
创建学生并在第三课程中控制流程:
public RegistrationFlowController {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe");
List<Student> students = new ArrayList<>();
Student student = new Student();
student.setFirstName("Emmaname");
student.setLastName("Mohaname");
student.setPhoneNumber("0221-1234567");
student.setEmail("aali3005@gmai.com");
students.add(student);
// create other students
Bayer registrationPage = new Bayer();
for (Student student : students) {
registrationPage.loadForm();
registrationPage.fillUpForm(student);
registrationPage.submitForm();
}
}
}
答案 1 :(得分:0)
首先,在数组中包含测试数据 其次,在java中创建一个接受驱动程序和测试数据作为参数的方法
然后直到测试数据大小完成循环testMethod并将驱动程序和测试数据作为参数传递
提交一次后,不要忘记加载表单所在的页面。
答案 2 :(得分:0)
String testData[][] = {{"Emmaname","Mohaname","0221-1234567","aali3005@gmai.com"},{"Emmaname123","Mohaname123","0221-4321","test1235@gmai.com"}}
for(String[] strings:testData){
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(strings[0]);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys(strings[2]);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input")).sendKeys(strings[3]);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input")).sendKeys(strings[4]);
driver.findElementById("submitButton").click();
}
答案 3 :(得分:0)
由于您是初学者,因此使用以下方法(您需要修改代码才能使其正常工作)。此代码有助于您理解dataProvider注释,因此稍后将尝试探索如何使用DataProvider注释。
学习硒时要记住的要点: 1)尝试分割您的数据和小部件 2)如果可能,从外部方法/文件中读取数据 3)使用页面工厂
创建Web元素public void fillForm(String firstName, String lastName, String phoneNumber, String email) {
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(firstName);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys(lastName);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input")).sendKeys(phoneNumber);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input")).sendKeys(email);
driver.findElementById("submitButton").click();
}
public void readData(Object[][] data)
{
for(int i = 0; i < data.length; i++){
for(int j=0; j < data.length; j++){
String firstName = (String)data[i][j];
String lastName = (String)data[i][j];
String phoneNumber = (String)data[i][j];
String email= (String)data[i][j];
fillForm(firstName, lastName, phoneNumber, email);
}
}
}
public Object[][] getData() {
//Rows - Number of times your test has to be repeated.
//Columns - Number of parameters in test data.
Object[][] data = new Object[4][4];
// 1st row
data[0][0] = "Emmaname";
data[0][1] = "Mohaname";
data[0][2] = "0221-1234567";
data[0][3] ="emmaname@test.com";
// 2nd row
data[1][0] = "Emmaname123";
data[1][1] = "Mohaname123";
data[1][2] = "0221-4321";
data[1][3] ="emmaname@test.com";
// 3rd row
data[2][0] = "abcdef";
data[2][1] = "ghijkl";
data[2][2] = "012345";
data[2][3] ="emmaname@test.com";
// 4rd row
data[3][0] = "abcdef";
data[3][1] = "ghijkl";
data[3][2] = "012345";
data[3][3] ="emmaname@test.com";
return data;
}
答案 4 :(得分:-1)
创建一个2D数组并将测试数据值放在其中,编写for循环并将代码插入数据并在该循环内提交。
或者,您也可以将这些测试数据存储到Excel文件中,使用Apache POI / jxl在脚本中读取ex excel文件并编写循环以插入数据。