how to do data driven testing using TestNG and Selenium?

Open 1 Answers 50 Views Selenium
0 1

I have a scenario where I need to get data from Excel file and insert it into registration form and submit it. I tried below code but no luck.


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Iterator;
    import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class DatawithExcel {
        public static void DatawithExcel1() throws IOException {
            FileInputStream file = new FileInputStream(new File("C:\\TestData.xlsx"));
            //Get the workbook instance for XLS file 
            XSSFWorkbook workbook = new XSSFWorkbook (file);
            //Get first sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);
            //Get iterator to all the rows in current sheet
            Iterator<Row> rowIterator = sheet.iterator();
            while(rowIterator.hasNext()) {
                Row row = rowIterator.next();
                //For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();
                while(cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    switch(cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t\t");
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t\t");
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "\t\t");
            // TODO Auto-generated constructor stub
        public static void main(String args[]) throws IOException
            WebElement elements = driver.findElements(By.xpath(".//*[@id='steplist']/li"));
                    for(WebElement elem: elements) {
                WebDriver driver = new FirefoxDriver();

1 Answer

0 answered by

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Top Contributors