【问题标题】:Want to use same data provider but different excel path for different functions to provide data in selenium想要使用相同的数据提供者,但不同的功能使用不同的 excel 路径以在 selenium 中提供数据
【发布时间】:2020-10-12 09:23:38
【问题描述】:

想使用同一个 DataProvider 为多个方法提供数据。使用 Apache POI 创建了实用程序来读取 excel 文件并对其进行参数化,并使用数据提供程序向应用程序发送数据。

数据提供者向其中一种方法提供数据,但使用相同的数据提供者我想向另一种方法提供数据,但使用不同的 excel 路径。简而言之,参数化了路径。怎么做?

    public Object [][] logindetails() {
        configexcel datas= new configexcel("C:\\Users\\xyz.xlsx"); //configexcel class
        int rows= datas.rowsncol(0);
        int col= datas.column(0);
        Object [][] data= new Object[rows-1][col];
        for(int i=1;i<rows;i++) {
            for (int j=0;j<col;j++) {
                 data[i-1][j]= datas.readdata(0,i,j);
        }
        return data;
    }
  

--configexcel datas= new configexcel("C:\\Users\\xyz.xlsx"); //want to parameterize this.

//Constructor

package excelconfig;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class configexcel {
    
    XSSFWorkbook loadwb;
    XSSFSheet sheet;
    
    public configexcel(String excelpath) {
        
        try {
            File src=new File(excelpath);
            FileInputStream file= new FileInputStream(src);
            loadwb= new XSSFWorkbook(file);
            
        } catch (Exception e) {
            System.out.print("The error is"+e.getMessage());
        }
    }
        public String readdata(int sheets,int row,int col) {
            
            sheet=loadwb.getSheetAt(sheets);
            String inputs=sheet.getRow(row).getCell(col).getStringCellValue();
            return inputs;  
        }
        
        public int rowsncol(int rows) {
            int count=loadwb.getSheetAt(rows).getLastRowNum();
            count=count+1;
            return count;
        }
        
        public int column(int col) {
            sheet= loadwb.getSheetAt(0);
            int counts= sheet.getRow(col).getLastCellNum();
            // counts=counts+1;
            return counts;
        }```

}

【问题讨论】:

    标签: selenium selenium-webdriver apache-poi dataprovider data-driven


    【解决方案1】:

    在 selenium 中使用 excel 阅读器并在代码中存储数据字段并进行参数化

        import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.Calendar;
    import org.apache.poi.hssf.usermodel.HSSFDateUtil;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
    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 String path;
        public FileInputStream fis = null;
        public FileOutputStream fileOut = null;
        private XSSFWorkbook workbook = null;
        private XSSFSheet sheet = null;
        private XSSFRow row = null;
        private XSSFCell cell = null;
    
        public ExcelReader(String path) {
    
            this.path = path;
            try {
                fis = new FileInputStream(path);
                workbook = new XSSFWorkbook(fis);
                sheet = workbook.getSheetAt(0);
                fis.close();
            } catch (Exception e) {
    
                e.printStackTrace();
            }
        }
        // returns the row count in a sheet
    
        public int getRowCount(String sheetName) {
            int index = workbook.getSheetIndex(sheetName);
            if (index == -1)
                return 0;
            else {
                sheet = workbook.getSheetAt(index);
                int number = sheet.getLastRowNum() + 1;
                return number;
            }
    
        }
        // returns the data from a cell
        public String getCellData(String sheetName, String colName, int rowNum) {
            try {
                if (rowNum <= 0)
                    return "";
    
                int index = workbook.getSheetIndex(sheetName);
                int col_Num = -1;
                if (index == -1)
                    return "";
    
                sheet = workbook.getSheetAt(index);
                row = sheet.getRow(0);
                for (int i = 0; i < row.getLastCellNum(); i++) {
                    // System.out.println(row.getCell(i).getStringCellValue().trim());
                    if (row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
                        col_Num = i;
                }
                if (col_Num == -1)
                    return "";
    
                sheet = workbook.getSheetAt(index);
                row = sheet.getRow(rowNum - 1);
                if (row == null)
                    return "";
                cell = row.getCell(col_Num);
    
                if (cell == null)
                    return "";
    
                //System.out.println(cell.getCellType().name());
                //
                if (cell.getCellType().name().equals("STRING"))
                    return cell.getStringCellValue();
    
                    // if (cell.getCellType().STRING != null)
    
                    // if(cell.getCellType()==Xls_Reader.CELL_TYPE_STRING)
                    // return cell.getStringCellValue();
                else if ((cell.getCellType().name().equals("NUMERIC")) || (cell.getCellType().name().equals("FORMULA"))) {
    
                    String cellText = String.valueOf(cell.getNumericCellValue());
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        // format in form of M/D/YY
                        double d = cell.getNumericCellValue();
    
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(HSSFDateUtil.getJavaDate(d));
                        cellText = (String.valueOf(cal.get(Calendar.YEAR))).substring(2);
                        cellText = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + 1 + "/" + cellText;
    
                        // System.out.println(cellText);
    
                    }
    
                    return cellText;
                } else if (cell.getCellType().BLANK != null)
                    return "";
                else
                    return String.valueOf(cell.getBooleanCellValue());
    
            } catch (Exception e) {
    
                e.printStackTrace();
                return "row " + rowNum + " or column " + colName + " does not exist in xls";
            }
        }
    
    
        // returns the data from a cell
        public String getCellData(String sheetName, int colNum, int rowNum) {
            try {
                if (rowNum <= 0)
                    return "";
    
                int index = workbook.getSheetIndex(sheetName);
    
                if (index == -1)
                    return "";
    
                sheet = workbook.getSheetAt(index);
                row = sheet.getRow(rowNum - 1);
                if (row == null)
                    return "";
                cell = row.getCell(colNum);
                if (cell == null)
                    return "";
    
                //
                if (cell.getCellType().name().equals("STRING"))
                    return cell.getStringCellValue();
    
                    //
                    // if (cell.getCellType().STRING != null)
                    // return cell.getStringCellValue();
                else if ((cell.getCellType().name().equals("NUMERIC")) || (cell.getCellType().name().equals("FORMULA"))) {
    
                    String cellText = String.valueOf(cell.getNumericCellValue());
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        // format in form of M/D/YY
                        double d = cell.getNumericCellValue();
    
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(HSSFDateUtil.getJavaDate(d));
                        cellText = (String.valueOf(cal.get(Calendar.YEAR))).substring(2);
                        cellText = cal.get(Calendar.MONTH) + 1 + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cellText;
    
                        // System.out.println(cellText);
    
                    }
    
                    return cellText;
                } else if (cell.getCellType().BLANK != null)
                    return "";
                else
                    return String.valueOf(cell.getBooleanCellValue());
            } catch (Exception e) {
    
                e.printStackTrace();
                return "row " + rowNum + " or column " + colNum + " does not exist  in xls";
            }
        }
    
        // returns true if data is set successfully else false
        public boolean setCellData(String sheetName, String colName, int rowNum, String data) {
            try {
                fis = new FileInputStream(path);
                workbook = new XSSFWorkbook(fis);
    
                if (rowNum <= 0)
                    return false;
    
                int index = workbook.getSheetIndex(sheetName);
                int colNum = -1;
                if (index == -1)
                    return false;
    
                sheet = workbook.getSheetAt(index);
    
                row = sheet.getRow(0);
                for (int i = 0; i < row.getLastCellNum(); i++) {
                    // System.out.println(row.getCell(i).getStringCellValue().trim());
                    if (row.getCell(i).getStringCellValue().trim().equals(colName))
                        colNum = i;
                }
                if (colNum == -1)
                    return false;
    
                sheet.autoSizeColumn(colNum);
                row = sheet.getRow(rowNum - 1);
                if (row == null)
                    row = sheet.createRow(rowNum - 1);
    
                cell = row.getCell(colNum);
                if (cell == null)
                    cell = row.createCell(colNum);
    
                // cell style
                // CellStyle cs = workbook.createCellStyle();
                // cs.setWrapText(true);
                // cell.setCellStyle(cs);
                cell.setCellValue(data);
    
                fileOut = new FileOutputStream(path);
    
                workbook.write(fileOut);
    
                fileOut.close();
    
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    
        // returns true if sheet is created successfully else false
        public boolean addSheet(String sheetname) {
    
            FileOutputStream fileOut;
            try {
                workbook.createSheet(sheetname);
                fileOut = new FileOutputStream(path);
                workbook.write(fileOut);
                fileOut.close();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    
        // returns true if sheet is removed successfully else false if sheet does
        // not exist
        public boolean removeSheet(String sheetName) {
            int index = workbook.getSheetIndex(sheetName);
            if (index == -1)
                return false;
    
            FileOutputStream fileOut;
            try {
                workbook.removeSheetAt(index);
                fileOut = new FileOutputStream(path);
                workbook.write(fileOut);
                fileOut.close();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    
        // returns true if column is created successfully
        public boolean addColumn(String sheetName, String colName) {
            // System.out.println("**************addColumn*********************");
    
            try {
                fis = new FileInputStream(path);
                workbook = new XSSFWorkbook(fis);
                int index = workbook.getSheetIndex(sheetName);
                if (index == -1)
                    return false;
    
                XSSFCellStyle style = workbook.createCellStyle();
                // style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
                // style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    
                sheet = workbook.getSheetAt(index);
    
                row = sheet.getRow(0);
                if (row == null)
                    row = sheet.createRow(0);
    
                // cell = row.getCell();
                // if (cell == null)
                // System.out.println(row.getLastCellNum());
                if (row.getLastCellNum() == -1)
                    cell = row.createCell(0);
                else
                    cell = row.createCell(row.getLastCellNum());
    
                cell.setCellValue(colName);
                cell.setCellStyle(style);
    
                fileOut = new FileOutputStream(path);
                workbook.write(fileOut);
                fileOut.close();
    
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
    
            return true;
    
        }
    
        // removes a column and all the contents
        public boolean removeColumn(String sheetName, int colNum) {
            try {
                if (!isSheetExist(sheetName))
                    return false;
                fis = new FileInputStream(path);
                workbook = new XSSFWorkbook(fis);
                sheet = workbook.getSheet(sheetName);
                XSSFCellStyle style = workbook.createCellStyle();
                // style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
                XSSFCreationHelper createHelper = workbook.getCreationHelper();
                // style.setFillPattern(XSSFCellStyle.NO_FILL);
                for (int i = 0; i < getRowCount(sheetName); i++) {
                    row = sheet.getRow(i);
                    if (row != null) {
                        cell = row.getCell(colNum);
                        if (cell != null) {
                            cell.setCellStyle(style);
                            row.removeCell(cell);
                        }
                    }
                }
                fileOut = new FileOutputStream(path);
                workbook.write(fileOut);
                fileOut.close();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
    
        }
    
        // find whether sheets exists
        public boolean isSheetExist(String sheetName) {
            int index = workbook.getSheetIndex(sheetName);
            if (index == -1) {
                index = workbook.getSheetIndex(sheetName.toUpperCase());
                if (index == -1)
                    return false;
                else
                    return true;
            } else
                return true;
        }
    
        // returns number of columns in a sheet
        public int getColumnCount(String sheetName) {
            // check if sheet exists
            if (!isSheetExist(sheetName))
                return -1;
    
            sheet = workbook.getSheet(sheetName);
            row = sheet.getRow(0);
    
            if (row == null)
                return -1;
    
            return row.getLastCellNum();
    
        }
    
        public int getCellRowNum(String sheetName, String colName, String cellValue) {
    
            for (int i = 2; i <= getRowCount(sheetName); i++) {
                if (getCellData(sheetName, colName, i).equalsIgnoreCase(cellValue)) {
                    return i;
                }
            }
            return -1;
    
        }
    
    }
    

    【讨论】:

    • ExcelReader reader = new ExcelReader("path excel sheet");
    • 感谢您的快速响应。此代码读取不同的工作表并写入不同的文件(工作表)和验证。实际上,我已经从 excel 文件创建了 getRow、getCell 等的构造函数,并创建了一个数组(提供的代码)来存储数据并向函数提供数据。问题是当我在构造函数中参数化路径并在 dataprovider 中传递参数(路径)时,我只能使用 dataprovider 一次,而我想通过提供不同的 excel WORKBOOK 路径多次使用 dataprovider 来实现不同的功能,取决于功能数据要求。
    • 你试过上面的 util 类吗?
    • 我只使用了部分代码来读取,而不是写入工作表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    相关资源
    最近更新 更多