-2

So I have a Test where I am filling out a form. What I want is to run this test multiple times and each time to use different input values such as a different name. I think I can use some kind of word list to do this? But I'm not sure exactly how to go about it.

     .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);

    }
OhAye
  • 93
  • 1
  • 4
  • 12
  • IF you are voting this down please provide a comment. Otherwise I'm not going to know why I'm being voted down and can't better myself and in turn help the community as others have helped me. What you may think is a stupid question should still be given constructive feedback to encourage people to continue to learn. – OhAye Jul 12 '16 at 12:23

1 Answers1

0

You can create a method to generate random text. See mine below

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;
}

Then when you want to fill out a form you can do the following:

String firstName = ClassName.generateRandomName(9); // 9 Characters long
driver.findElement(By.xpath("Your xpath")).sendKeys(firstName);

You can call that method for wherever you want to call a random string of text. Hope it helps.

Moser
  • 287
  • 2
  • 12
  • It really did, thank you so much. If you wanted to do a list of names would you just create an array of Strings and then pick one at random? – OhAye Jul 12 '16 at 13:24
  • Yes, you could just create a string array and add each string to the array and then pick one at random. Picking one at random has been answered here so this link could help you out also. http://stackoverflow.com/questions/8065532/how-to-randomly-pick-an-element-from-an-array – Moser Jul 13 '16 at 08:06