private Iterator<Object[]> parseCsvData(String fileName) throws IOException
{
BufferedReader input = null;
File file = new File(fileName);
input = new BufferedReader(new FileReader(file));
String line = null;
ArrayList<Object[]> data = new ArrayList<Object[]>();
while ((line = input.readLine()) != null)
{
String in = line.trim();
String[] temp = in.split(“,”);
List<Object> arrray = new ArrayList<Object>();
for(String s : temp)
{
arrray.add(Integer.parseInt(s));
}
data.add(arrray.toArray());
}
input.close();
return data.iterator();
}
/**
Use the above method in dataprovider
**/
@DataProvider(name = "testData")
public Iterator<Object[]> testData() throws IOException
{
return parseTestData(<Location of csv file>);
}
@Test(dataProvider = “testData”)
public void verifyLoginUsingCsv (String username, String password, String testDescription)
{
/** Your Test Code **/
}
Friday, February 12, 2021
CSV dataset to DataProvider
Thursday, February 11, 2021
Random Data Generator
Random Number Generator:
public String randomNumberGenerator(int length) throws Exception{
Random random = new Random();
char[] digits = new char[length];
digits[0] = (char) (random.nextInt(9) + '1');
for (int i = 1; i < length; i++) {
digits[i] = (char) (random.nextInt(10) + '0');
}
long st = Long.parseLong(new String(digits));
String uniqueID = String.valueOf(st);
return uniqueID;
}
Random String Generator:
public String randomStringGenerator(int length) throws Exception {
// Chose a Character random from this String
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvxyz";
// create StringBuffer size of AlphaNumericString
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = (int) (AlphaNumericString.length() * Math.random());
// add Character one by one in end of sb
sb.append(AlphaNumericString.charAt(index));
}
return sb.toString();
}
Random Alpha Numeric Generator:
public String randomAlphaNumericGenerator(int length) throws Exception {
// chose a Character random from this String
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvxyz";
// create StringBuffer size of AlphaNumericString
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = (int) (AlphaNumericString.length() * Math.random());
// add Character one by one in end of sb
sb.append(AlphaNumericString.charAt(index));
}
return sb.toString();
}
Random Date Generator:
Enter Start and End Year.
public static int randomInBetween(int start, int end) {
return start + (int) Math.round(Math.random() * (end - start));
}
public String randomDateGenerator(int startYear, int endYear) throws Exception {
int day = createRandomIntBetween(1, 28);
int month = createRandomIntBetween(1, 12);
int year = createRandomIntBetween(startYear, endYear);
return LocalDate.of(year, month, day).toString();
}
Calculate The Discount Price:
public String calculateDiscount(String Percentage, String Amount) {
Amount= Amount.replace(",", ""); BigDecimal price = new BigDecimal(Amount);
BigDecimal discount = new BigDecimal(Percentage);
BigDecimal ONE_HUNDRED = new BigDecimal(100);
BigDecimal discountValue = price.multiply(discount).divide(ONE_HUNDRED);
discountValue = price.subtract(discountValue);
discountValue = discountValue.setScale(2, BigDecimal.ROUND_HALF_UP);
double value = Double.parseDouble(discountValue.toString());
String afterDiscount = NumberFormat.getCurrencyInstance().format(value);
// To remove prefix $ symbol
afterDiscount = afterDiscount.substring(1);
System.out.println("Amount after discount: " + afterDiscount);
return afterDiscount;
}