目录
做了简单分离 initExcel(),WriteToExcel(),CloseExcel(),主要目的提高写 excel 性能
前言:
基于 Excel 数据驱动的单接口自动化接口框架是一种使用 Excel 数据作为输入,自动化执行接口测试的框架。
很多时候大家处理 Excel 时,会采用格式固定,确定好某行,某列的位置,才能正常取数或写数据,这对于整个框架而言,限制会非常大,因此跟大家说下我这边的思路;大家也可以作为通用方法进行调用;
Excel 读方法
实现思路:1.确定好参数行,根据参数名称自动查找到该参数对应的数据,这里要求参数名称不能变动,参数所在位置可任意变换
/**
* 优势: 参数和参数值的位置不需要格式限制
* 按行读取所有数据,并把param与value以Map形式输出
* BasicExcelData以map形式存储数据
* @param excelPath excel路径
* @param sheetName sheet页名称
* @param paramRow 参数所在行
* @param iRow 参数值所在行
* @return BasicExcelData
*/
public static BasicExcelData ExcelRead(String excelPath,String sheetName,int paramRow,int iRow) throws Exception{
File directory = new File(".");
String path = null;
path = directory .getCanonicalPath();
path += excelPath;
File file = new File(path);
BasicExcelData basicExcelData = new BasicExcelData(); //new basicExcelData
if(file.exists()){
WorkbookSettings ws = new WorkbookSettings();
ws.setCellValidationDisabled(true);
Workbook wwb = Workbook.getWorkbook(file,ws);
Sheet sheet = wwb.getSheet(sheetName);
int rows = sheet.getRows();
int columns = sheet.getColumns();
// log.info(rows + "and" + columns);
Map<String, Object> paramWithValue= new HashMap<String, Object>();
Map<String, Object> paramWithCol= new HashMap<String, Object>();
Map<String, Object> paramValueWithCol= new HashMap<String, Object>();
//遍历行数据,并写入map中
for(int i = 0; i < columns; i ++){
Cell paramCell = sheet.getCell(i,paramRow);//获取excel中参数所在行数据
Cell valueCell = sheet.getCell(i,iRow);//获取excel中参数值所在行数据
//若参数值的末尾为#,则删除#
// log.info(valueCell.getContents());
if(valueCell.getContents().length() != 0 && "#".equals(valueCell.getContents().substring(valueCell.getContents().length()-1))){
//去除#后的字符串
paramWithValue.put(paramCell.getContents(), valueCell.getContents().substring(0,valueCell.getContents().length()-1));
paramValueWithCol.put(valueCell.getContents().substring(0,valueCell.getContents().length()-1), i);
}
else {
paramWithValue.put(paramCell.getContents(), valueCell.getContents());
p