Friday 22 June 2018

Data Generation using Java

Data Generation in Test Automation is one of the tedious task which consumes lot of time before the start of execution. So, Why provide the data required for Automation manually? Automate it. !!

Having said that, it is not so easy to address every requirement with single solution. In the below example, i am trying to address few of the generic use cases which will let us automate the data creation. 

NOTE: 

  • Below sample uses 'Xegeran automation library to generate text from regular expressions.
  • Apache commons RandomStringUtils class to generate the random text.


Below example will let you know the way to generate the Random strings of different types.


public class RandomTextGenerator {



public static void main(String[] args){
//Generate Differnt types of Strings
String testStr = generateRandomString("email","",9);
System.out.println(testStr);
testStr = generateRandomString("number","",10);
System.out.println(testStr);
testStr = generateRandomString("alphabet","",10);
System.out.println(testStr);
testStr = generateRandomString("alphanumeric","",10);
System.out.println(testStr);
testStr = generateRandomString("ascii","",10);
System.out.println(testStr);
testStr = generateRandomString("url","",0);
System.out.println(testStr);
testStr = generateRandomString("RegEx","ABCD-[0-9]{5}",0);
System.out.println(testStr);
}


/**
 * Generates the random string
 * @param type
 * @param format
 * @param length
 * @return String
 */
public static String generateRandomString(String type,String format,int length){
String randomStr = "";

switch(type.toUpperCase()){
case "EMAIL":
    randomStr = RandomStringUtils.randomAlphanumeric(length) + "@abcd.com";
    break;
case "NUMBER":
    randomStr = RandomStringUtils.randomNumeric(length);
    break;
case "ALPHABET":
    randomStr = RandomStringUtils.randomAlphabetic(length);
    break;
case "ALPHANUMERIC":
    randomStr = RandomStringUtils.randomAlphanumeric(length);
    break;
case "ASCII":
   randomStr = RandomStringUtils.randomAscii(length);
   break;
case "URL":
 try {
     randomStr = new URL("https",RandomStringUtils.randomAlphabetic(6).toLowerCase()+"."+RandomStringUtils.randomNumeric(3),"/random").toString();
 } catch (MalformedURLException e) {
    e.printStackTrace();
  }
 break;
case "REGEX":
    Xeger gen = new Xeger(format);
    randomStr = gen.generate();
    break;
default:
    System.out.println("Type " + type + "is not found.");
    break;
}

return randomStr;
}

}




2 comments:

  1. Glad to know you found it useful.

    ReplyDelete
  2. Thanks for sharing nice post and nice urging commented at this place, I am in fact enjoying by these.I like visiting your site since I always come across interesting articles like this one. Keep sharing! Regards. Read more about
    testing services companies
    Software Testing and Quality Assurance Services
    Performance testing services
    Security testing services
    software testing company

    ReplyDelete