Friday, September 13, 2013

Generating Random Data





Generating Random Data
Say , you are running RecordCreate/FormFilling automation for 100 times.
  1. Passing every time same values (Hardcoded values)
  2. Passing randomly generated values from allowed characters
Which one is more reliable ? 

How will you verify forms by filling allowed max length values ?


Let's see how to fill the forms in web application with random & specified length values.

Consider below form

You want to fill below form with randomly generated values with specified lengths.

Register here

First Name
Last Name
Email
Phone
Income


See below testcase to know how can we pass random & specified length values to different fields.

RegistrationForm.java


package com.test.registration;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.test.general.GenerateData;

public class RegistrationForm {
 
 WebDriver driver;
 GenerateData genData;
 
 @Before
 public void before(){
  driver=new FirefoxDriver();
  genData=new GenerateData();
 }
 
 @Test
 public void testRegistrationForm() {
  driver.findElement(By.id("f_name")).sendKeys(genData.generateRandomAlphaNumeric(30));    
  driver.findElement(By.id("l_name")).sendKeys(genData.generateRandomString(20));
  driver.findElement(By.id("email")).sendKeys(genData.generateEmail(30));
  driver.findElement(By.id("skype")).sendKeys(genData.generateStringWithAllobedSplChars(30, "abc123_-."));
  driver.findElement(By.id("phone")).sendKeys(genData.generateRandomNumber(10));
  driver.findElement(By.id("income")).sendKeys(genData.generateRandomNumber(9));
  driver.findElement(By.id("submit")).click();
  assertEquals("Success", driver.getTitle());
 }
 
 @After
 public void after(){
  driver.quit();
 }

}




GenerateData.java


package com.test.general;

import org.apache.commons.lang3.RandomStringUtils;

public class GenerateData {

 public String generateRandomString(int length){
  return RandomStringUtils.randomAlphabetic(length);
 }
 
 public String generateRandomNumber(int length){
  return RandomStringUtils.randomNumeric(length);
 }
 
 public String generateRandomAlphaNumeric(int length){
  return RandomStringUtils.randomAlphanumeric(length);
 }
 
 public String generateStringWithAllobedSplChars(int length,String allowdSplChrs){
  String allowedChars="abcdefghijklmnopqrstuvwxyz" +   //alphabets
    "1234567890" +   //numbers
    allowdSplChrs;
  return RandomStringUtils.random(length, allowedChars);
 }
 
 public String generateEmail(int length) {
  String allowedChars="abcdefghijklmnopqrstuvwxyz" +   //alphabets
    "1234567890" +   //numbers
    "_-.";   //special characters
  String email="";
  String temp=RandomStringUtils.random(length,allowedChars);
  email=temp.substring(0,temp.length()-9)+"@test.org";
  return email;
 }
 
 public String generateUrl(int length) {
  String allowedChars="abcdefghijklmnopqrstuvwxyz" +   //alphabets
    "1234567890" +   //numbers
    "_-.";   //special characters
  String url="";
  String temp=RandomStringUtils.random(length,allowedChars);
  url=temp.substring(0,3)+"."+temp.substring(4,temp.length()-4)+"."+temp.substring(temp.length()-3);
  return url;
 }
}
 

    While doing web application automation make a habit of passing random data with different lengths (<= Allowed max length) every time to improve reliability.


Related Topics
Generating Dynamic Dates



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.