39 – Data Driven Testing using Excel and TestNG

Selenium Class 39 – TestNG Testing Framework for Selenium Part-4

1) Introduction to TestNG Testing Framework
2) Install TestNG and write first TestNG program
3) Create multiple Test cases and run
4) TestNG Annotations
5) Execute multiple Programs / Classes using XML
6) Grouping Test Cases

7) Data Driven Testing using @DataProvider Annotation

i) Read Test Data (String type data) from an excel file and conduct Data Driven testing for Admin Login Functionality

ii) Read Test Data (Integer Type) from an excel file and conduct Data driven Testing for addition operation
………………………………….
i) Read Test Data (String type data) from an excel file and conduct Data Driven testing for Admin Login Functionality

Test Steps:
i) Launch Browser
ii) Navigate to gcrShop Admin interface (http://www.gcrit.com/build3/admin/)
iii) Enter “Username”
iv) Enter “Password”
v) Click “Login” Button

Expected URL/Result:
http://www.gcrit.com/build3/admin/index.php

Verification Point:
Capture the URL after click Login and compare with expected result

Input / Test Data:
Ref: input.xls

Selenium WebDriver Test Script:

public class DataDriven {
static WebDriver driver;

@Test (dataProvider =”testdata”)
public void adminLogin(String User, String Pwd) throws InterruptedException{
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“http://www.gcrit.com/build3/admin/”);
driver.findElement(By.name(“username”)).sendKeys(User);
driver.findElement(By.name(“password”)).sendKeys(Pwd);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(4000);
String url = driver.getCurrentUrl();

/if (url.contains(“http://www.gcrit.com/build3/admin/index.php”)){ System.out.println(“Admin Login is Successful – Passed”); } else { System.out.println(“Admin Login is Unsuccessful – Failed”); }/
Assert.assertEquals(url, “http://www.gcrit.com/build3/admin/index.php”);
}

@AfterMethod
public void closeBrowser(){
driver.close();
}
@DataProvider (name = “testdata”)
public Object[][]readExcel()throws BiffException, IOException {
File f = new File (“C:\Users\admin\Desktop\input.xls”);
Workbook w = Workbook.getWorkbook(f);
Sheet s= w.getSheet(0);
int columns = s.getColumns();
int rows = s.getRows();
//System.out.println(columns);
//System.out.println(rows);

String inputData [] [] = new String [rows][columns];

for (int i=0; i<rows; i++){
for (int j=0; j<columns; j++){
Cell c= s.getCell(j,i);
inputData[i][j]=c.getContents();
//System.out.println(inputData[i][j]);
}
}
return inputData;
}
}

ii) Read Test Data (Integer Type) from an excel file and conduct Data driven Testing for addition operation

public class DataDriven2 {
@Test(dataProvider=”abcd”)
public void addition(String num1, String num2, String num3){
int a= Integer.parseInt(num1);
int b= Integer.parseInt(num2);
int c= Integer.parseInt(num3);
int result =a+b+c;

Assert.assertEquals(result, a+b+c);
}
@DataProvider(name =”abcd”)
public Object[][]readFile() throws BiffException, IOException {
File f = new File (“C:\Users\admin\Desktop\input.xls”);
Workbook w = Workbook.getWorkbook(f);
Sheet s = w.getSheet(1);

int rows = s.getRows();
int columns = s.getColumns();
//System.out.println(rows);
//System.out.println(columns);

String inputData[] [] = new String [rows][columns];

for (int i=0; i<rows; i++){
for (int j=0; j<columns; j++){
Cell c= s.getCell(j, i);
inputData[i][j]=c.getContents();
//System.out.println(inputData[i][j]);
}
}
return inputData;
}
}

selenium tutorial
TestNG Testing Framework Tutorial