Selenium Webdriver - 如何随机化测试以每次输入新的详细信息?
Selenium Webdriver - How can I randomise a test to input new details every time?
所以我在填写表格时进行了测试。我想要的是多次 运行 这个测试,并且每次都使用不同的输入值,例如不同的名称。我想我可以使用某种单词列表来做到这一点?但我不确定具体该怎么做。
.completePersonalAddressDetails("04/06/2017","NONE","Mr","Ohaye",
"04/05/1985","Tester","British","123 boombastic avenue","G412LQ")
public NewStartPage completePersonalAddressDetails(String startDate, String NINumber,
String title, String Name, String DOB, String LastName,
String nationality, String addressLine, String postcode) {
helper.switchToMainFrame();
startDateInput.sendKeys(startDate);
helper.sleep();
payrollCompanyLookUp.click();
helper.switchToLookUpFrame();
firstPayrollCompany.click();
helper.switchToMainFrame();
payrollCompanySelectButton.click();
niNumberInput.clear();
niNumberInput.sendKeys(NINumber);
Select selectTitle = new Select(titleSelect);
selectTitle.selectByValue(title);
firstNameInput.sendKeys(Name);
maritalStatusInput.click();
helper.switchToLookUpFrame();
helper.sleep();
maritalStatusDivorced.click();
helper.switchToMainFrame();
maritalStatusSelectButton.click();
DOBInput.sendKeys(DOB);
lastNameInput.sendKeys(LastName);
Select selectNationality = new Select(nationalitySelect);
selectNationality.selectByVisibleText(nationality);
genderInput.click();
helper.switchToLookUpFrame();
helper.sleep();
genderMale.click();
helper.switchToMainFrame();
genderSelect.click();
helper.sleep();
addressLineInput.sendKeys(addressLine);
postcodeInput.sendKeys(postcode);
driver.switchTo().defaultContent();
return PageFactory.initElements(driver, NewStartPage.class);
}
您可以创建一种方法来生成随机文本。请参阅下面的
public String generateRandomName(int length) {
char[] chars =abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
.toCharArray();
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
String randomString = sb.toString();
return randomString;
}
然后当您想填写表格时,您可以执行以下操作:
String firstName = ClassName.generateRandomName(9); // 9 Characters long
driver.findElement(By.xpath("Your xpath")).sendKeys(firstName);
您可以在任何您想调用随机文本字符串的地方调用该方法。希望能帮助到你。
所以我在填写表格时进行了测试。我想要的是多次 运行 这个测试,并且每次都使用不同的输入值,例如不同的名称。我想我可以使用某种单词列表来做到这一点?但我不确定具体该怎么做。
.completePersonalAddressDetails("04/06/2017","NONE","Mr","Ohaye",
"04/05/1985","Tester","British","123 boombastic avenue","G412LQ")
public NewStartPage completePersonalAddressDetails(String startDate, String NINumber,
String title, String Name, String DOB, String LastName,
String nationality, String addressLine, String postcode) {
helper.switchToMainFrame();
startDateInput.sendKeys(startDate);
helper.sleep();
payrollCompanyLookUp.click();
helper.switchToLookUpFrame();
firstPayrollCompany.click();
helper.switchToMainFrame();
payrollCompanySelectButton.click();
niNumberInput.clear();
niNumberInput.sendKeys(NINumber);
Select selectTitle = new Select(titleSelect);
selectTitle.selectByValue(title);
firstNameInput.sendKeys(Name);
maritalStatusInput.click();
helper.switchToLookUpFrame();
helper.sleep();
maritalStatusDivorced.click();
helper.switchToMainFrame();
maritalStatusSelectButton.click();
DOBInput.sendKeys(DOB);
lastNameInput.sendKeys(LastName);
Select selectNationality = new Select(nationalitySelect);
selectNationality.selectByVisibleText(nationality);
genderInput.click();
helper.switchToLookUpFrame();
helper.sleep();
genderMale.click();
helper.switchToMainFrame();
genderSelect.click();
helper.sleep();
addressLineInput.sendKeys(addressLine);
postcodeInput.sendKeys(postcode);
driver.switchTo().defaultContent();
return PageFactory.initElements(driver, NewStartPage.class);
}
您可以创建一种方法来生成随机文本。请参阅下面的
public String generateRandomName(int length) {
char[] chars =abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
.toCharArray();
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
String randomString = sb.toString();
return randomString;
}
然后当您想填写表格时,您可以执行以下操作:
String firstName = ClassName.generateRandomName(9); // 9 Characters long
driver.findElement(By.xpath("Your xpath")).sendKeys(firstName);
您可以在任何您想调用随机文本字符串的地方调用该方法。希望能帮助到你。