Selenium For Beginners

Share this post

User's avatar
Selenium For Beginners
How to make your Selenium tests dynamic with realistic random data
Tests

How to make your Selenium tests dynamic with realistic random data

Alex Siminiuc's avatar
Alex Siminiuc
Aug 12, 2023
∙ Paid

Share this post

User's avatar
Selenium For Beginners
How to make your Selenium tests dynamic with realistic random data
Share

The following short test automates a simple user registration flow:

public class UserRegistrationTests {

   //urls
   private static final String REGISTER_PAGE_URL = 
       "https://parabank.parasoft.com/parabank/register.htm";

   private static final String URL = "https://parabank.parasoft.com";

   //test data
   private static final String FIRST_NAME = "Stan";
   private static final String LAST_NAME = "McGlynn";
   private static final String ADDRESS = "2690 Weston Canyon";
   private static final String CITY = "Port Jutta";
   private static final String STATE = "Alberta";
   private static final String ZIP_CODE = "P7E 2N2";
   private static final String PHONE = "(365) 317-1322";
   private static final String SSN = "647-65-6745";
   private static final String USERNAME = "cleveland.gutkowski";
   private static final String PASSWORD = "al2qxycn3zq";

   private ChromeDriver driver;

   //locators
   private By registerLinkXpath = By.xpath("//a[text() = 'Register']");

   private By firstNameId = By.id("customer.firstName");
   private By lastNameId = By.id("customer.lastName");
   private By addressId = By.id("customer.address.street");
   private By cityId = By.id("customer.address.city");
   private By stateId = By.id("customer.address.state");
   private By zipCodeId = By.id("customer.address.zipCode");
   private By phoneId = By.id("customer.phoneNumber");  
   private By ssnId = By.id("customer.ssn");
   private By usernameId = By.id("customer.username");
   private By passwordId = By.id("customer.password");
   private By repeatedPasswordId = By.id("repeatedPassword");

   private By registerButtonXpath = By.xpath("//input[@value = 'Register']");

   @Before
   public void setUp() {
      driver = new ChromeDriver();
      driver.manage().window().maximize();
   }

   @After
   public void tearDown() {
      driver.quit();
   }

   @Test
   public void userCanRegisterTest() throws InterruptedException {

      driver.get(URL);
      Assert.assertTrue(driver.getCurrentUrl().contains(URL));

      WebElement registerLink = driver.findElement(registerLinkXpath);
      registerLink.click();

      Assert.assertTrue(driver.getCurrentUrl().contains(REGISTER_PAGE_URL));

      WebElement firstNameBox = driver.findElement(firstNameId);
      firstNameBox.sendKeys(FIRST_NAME);

      WebElement lastNameBox = driver.findElement(lastNameId);
      lastNameBox.sendKeys(LAST_NAME);

      WebElement addressBox = driver.findElement(addressId);
      addressBox.sendKeys(ADDRESS);

      WebElement cityBox = driver.findElement(cityId);
      cityBox.sendKeys(CITY);

      WebElement stateBox = driver.findElement(stateId);
      stateBox.sendKeys(STATE);

      WebElement zipBox = driver.findElement(zipCodeId);
      zipBox.sendKeys(ZIP_CODE);

      WebElement phoneBox = driver.findElement(phoneId);
      phoneBox.sendKeys(PHONE);

      WebElement ssnBox = driver.findElement(ssnId);
      ssnBox.sendKeys(SSN);

      WebElement usernameBox = driver.findElement(usernameId);
      usernameBox.sendKeys(USERNAME);

      WebElement passwordBox = driver.findElement(passwordId);
      passwordBox.sendKeys(PASSWORD);

      WebElement repeatedPasswordBox = driver.findElement(repeatedPasswordId);
      repeatedPasswordBox.sendKeys(PASSWORD);

      Thread.sleep(5000);

      WebElement registerButton = driver.findElement(registerButtonXpath);
      registerButton.click();

  }  

}

The test is very straightforward.

It sets a value in each registration field and then clicks the registration button.

There is only 1 problem with it.

Every time it runs, it uses the same data from the following constants:

private static final String FIRST_NAME = "Stan";
private static final String LAST_NAME = "McGlynn";
private static final String ADDRESS = "2690 Weston Canyon";
private static final String CITY = "Port Jutta";
private static final String STATE = "Alberta";
private static final String ZIP_CODE = "P7E 2N2";
private static final String PHONE = "(365) 317-1322";
private static final String SSN = "647-65-6745";
private static final String USERNAME = "cleveland.gutkowski";
private static final String PASSWORD = "al2qxycn3zq";

The test would be so much better if it runs every time on different data.

Keep reading with a 7-day free trial

Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Alex Siminiuc
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share