In Testing it is important to provide input data to complete the execution of Test-cases. Irrespective of any type of testing such as Functional Testing,Integration Testing,System Testing and Performance Testing the input data plays a vital role.
In Automation tools such as Selenium WebDriver the input data can be maintained in three different ways:
1. Maintain parameters in the testng.xml. Read the parameter values using @Parameters annotation.
2. Maintain input data in the Excel File. Read the data and pass the values using @DataProvider annotation.
3. Input Data in the properties file.
In the above 3 cases, the input data stored is static. If we suppose, the dynamic data should be generated used in the execution flow of test cases. We have to Java API to generate the dynamic data.
So, we will go through now how to generate the random/dynamic input data.
Note:
Do not get confuse with the dynamic object properties. In this post we are discussing about only the input data.
Generating two digit Random Number
java.util.Random is a java class available for generating random integers.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package totalqa; import java.util.Random; public class DynamicInputData{ public static void main(String args[]) { int uLimit = 99; int lLimit = 10; int val = lLimit + rand.nextInt(uLimit - lLimit ); System.out.println("2 digit Random Number:"+ val); } } |
Generating three digit Random Number
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package totalqa; import java.util.Random; public class DynamicInputData{ public static void main(String args[]) { int uLimit = 999; int lLimit =100; int val = lLimit + rand.nextInt(uLimit -lLimit ); System.out.println("3 digit Random Number:"+ val); } } |
Generating Random ASCII Character
To generate the random characters we should glance through the ASCII table from this link.
http://www.asciitable.com/
-
Lower Case Alphabets– Decimal Number Ranging from 97 to 122
-
Upper Case Alphabets– Decimal Number Ranging from 65 to 90
1 2 3 4 5 6 7 8 9 10 11 12 |
package totalqa; import java.util.Random; public class RandomASCII{ public static void main(String args[]) { //Generating Random Upper Case ASCII Characters char ch = (char) (65+rand.nextInt(26)); System.out.println(ch); } } |
Generating five digit Random Number
1 2 3 4 5 6 7 8 9 10 |
package totalqa; import java.util.Random; public class DynamicInputData{ public static void main(String args[]) { //Generate a 5 digit unique number System.out.println(Calendar.getInstance().getTimeInMillis()%100000); } } |
Conclusion:
The Dynamic input data generated is useful to execute the test case in different Testing Environment without any dependency on resource files.