繁体   English   中英

如何在selenium Web驱动程序和testNG中使用数据提供程序读取Excel并获取多个值并将其连接

[英]How to read an excel and fetch multiple values and concatenate them using data-provider in selenium web driver and testNG

我想读取一个excel并一次获取10条记录,并使用Selenium Web驱动程序和testNG中的数据提供程序将它们连接起来。 例如,Excel工作表具有两列,即col 1,col 2和10行。 现在,我想使用Selenium Web驱动程序(即“ A,B,C,D,E,F,G,H,J,I”作为值)从excel作为值将获取的结果传递到文本框中。 在这里,第1列col1包含“ A”,第1列col 2包含“,”。 同样,对于其余的行和列也是如此。 能否请您提供此逻辑的代码。

使用JDBC驱动程序连接到excel文件,并通过SQL将其用作数据库:

如何对连接到Excel电子表格

read and write data from excel   
 import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    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;

    public class ExcelReader {
        public FileInputStream fis = null;
        public FileOutputStream fout = null;
        private XSSFWorkbook workbook = null;
        private XSSFSheet sheet = null;
        private XSSFRow row = null;
        private XSSFCell cell = null;
        String path = null;

        public ExcelReader() throws IOException {
            path = System.getProperty("user.dir") + "\\testdata\\testdata.xlsx";
            fis = new FileInputStream(path);//FileInputStream is used to read excel.
            workbook = new XSSFWorkbook(fis);//workbook is excel file include multiple sheet
            sheet = workbook.getSheetAt(0);//focusing on particular sheet in workbook.
        }
    //get number of rows in selected sheet.
        public int getSheetRows(String sheetName) {
            int index = workbook.getSheetIndex(sheetName);
            workbook.getSheetAt(index);
            return (sheet.getLastRowNum() + 1);
        }
    //get number of columns in selected sheet
        public int getSheetColumn(String sheetName) {
            int index = workbook.getSheetIndex(sheetName);
            workbook.getSheetAt(index);
            row = sheet.getRow(0);
            return (row.getLastCellNum());
        }
    //get the values in cell by using sheet name and row number and column number
        public String getCellData(String sheetName, int rowNum, int colNum) {
            int index = workbook.getSheetIndex(sheetName);
            sheet = workbook.getSheetAt(index);
            row = sheet.getRow(rowNum);
            cell = row.getCell(colNum);
            return (cell.getStringCellValue());
        }
    //get the values form cell by using the sheetname and row number and column name 
        public String getCellData(String sheetName, int rowNum, String ColName) {
            int CellNum = -1;
            int index = workbook.getSheetIndex(sheetName);
            sheet = workbook.getSheetAt(index);

            for (int i = 0; i < getSheetColumn(sheetName); i++) {
                row = sheet.getRow(0);
                cell = row.getCell(i);
                if (cell.getStringCellValue().equals(ColName)) {
                    CellNum = cell.getColumnIndex();
                    break;
                }
            }
            row = sheet.getRow(rowNum);
            cell = row.getCell(CellNum);
            return (cell.getStringCellValue());

        }
    //write the data into sheet.
        public void setData(String sheetName, int ColNum, int rowNum, String str) {
            int index = workbook.getSheetIndex(sheetName);
            sheet = workbook.getSheetAt(index);

            row = sheet.getRow(rowNum);
            cell = row.createCell(ColNum);
            cell.setCellValue(str);

            try {
                fout = new FileOutputStream(path);

                try {
                    workbook.write(fout);
                    fout.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }

        public static void main(String[] args) throws IOException {
            ExcelReader reader = new ExcelReader();

              System.out.println(reader.getSheetRows("LoginTest"));
              System.out.println(reader.getSheetColumn("LoginTest"));
              System.out.println(reader.getSheetRows("SignUPTest"));
              System.out.println(reader.getSheetColumn("SignUpTest"));
              System.out.println(reader.getCellData("LoginTest", 1, 0));
             System.out.println(reader.getCellData("LoginTest", 2, "password"));
            reader.setData("LoginTest", 1, 1, "Hello");
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM