HomeJava

functions for test automation in java with examples

Like Tweet Pin it Share Share Email

In this article you will have access to a huge number of reusable functions for test automation in java with examples. This can be considered as your bible for all of your needs.

We will divide them in to multiple categories as below

  • String manipulation
  • Date and time
  • Number manipulation
  • Currency related

Remember that you can customize all the functions written in this article for your own needs. One of our assumption is that we are writing all the methods in a class by name “CM”. The methods have static scope, which allows you to call the methods without creating an object.

String manipulation functions for test automation in java

1. Iterate through all pattern matches in a string and replace with a given string
Purpose: This method is generally used when we want to replace a certain pattern of words in a string with different string or value. You can customize this code to your needs.
Code:
public static String rep(String input,String pattern,HashMap repVals)
{
    Pattern patt = Pattern.compile(pattern);
    Matcher m = patt.matcher(input);
    String retVal = input;
    while(m.find())
    {
        String temp = m.group(0);
        String newValue = "";
        if(repVals.containsKey(temp))
            newValue = repVals.get(temp).toString();
        retVal = retVal.replace(temp,newValue );
    }

    return retVal;
}
Example: If i want to replace all words which look like {{var1}} , {{var2}} etc with a certain value, then you can use above method as below
public static void main(String rs[])
{
    String ip = "This PO {{var1}} has order of value {{var2}}";

    // generally this is generated from some other code. 
    //for demonstration purpose we are creating it here.
    HashMap reps = new HashMap();
    reps.put("{{var1}}","1234");
    reps.put("{{var2}}","100 USD");

    //now call the method
    //CM below is the class name and contains the 
    //reusable methods declared in static scope. 
    String transformedString = CM.rep(ip,"[a-zA-Z]+[0-9]+",reps);
    System.out.println(transformedString);
}

Date & Time functions for test automation in java

1. Function to get current date in a specific format
Purpose: Generally in a test case you might want to enter today’s date in an input field. You can get date using below method.
Code:
public static String today(String format)
{
     Date date=java.util.Calendar.getInstance().getTime(); 
 
     // example format: "dd-MMM-YYYY HH:mm:ss"
     SimpleDateFormat formatter = new SimpleDateFormat(format); 
    String dt = formatter.format(date);
    return dt;
}
Example: You need to test a scenario where you need to provide today’s date as a product received date and continue the test case.
public static void main(String rs[])
{
    System.out.println(CM.today("dd-MMM-YYYY"));
}

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *