最近在写自动化脚本,需要通过配置文件来定义Excel表格的存放路径,并且很多个测试类,故此为了测试数据的方便管理,于是每个测试类对应的测试数据是通过存放在Excel工作簿中,每个工作簿的命名规则和对应的测试类名一致。固有以下代码段:
//获取Excel数据文件路径
public static String getPropertiesPath() throws FileNotFoundException, IOException{
Properties pperties = new Properties();
pperties.load(new FileInputStream(""));
return pperties.getProperty("sysmDataPath");
}
//读取配置文件.properties里面的内容
public static String getPropertiesFromKeyToValue(String key) throws FileNotFoundException, IOException{
Properties pp = new Properties();
pp.load(new FileInputStream("D:\\bysu\\interface\\webselenium\\src\\webselenium\\resources\\config\\conf.properties"));
String properValue = pp.getProperty(key, "http://10.25.174.25:30082/admin/admin/login.html");
return properValue;
}
//获取配置文件指定的目录下所有的xls文件名
public static Map getDataAllFiles() throws FileNotFoundException, IOException{
//System.getProperty("user.dir")获取当前项目所在的根目录+通过获取配置文件中数据文件的存放目录
String path = System.getProperty("user.dir")+getPropertiesFromKeyToValue("sysmDataPath");
File[] fe = new File(path).listFiles();
HashMap<String,String> strPath = new HashMap<>();
for(File f:fe){
strPath.put(f.getName(),path);
}
return strPath;
}
//获取测试类名的Excel的文件名
public static String getExcelName(String className){
String[] sp = className.split("\\.");//把获取到调用类的类名以.为分割号分割
String excelName = sp[sp.length-1]; //取类名
return excelName;
}
//获取对应的Excel文件
public static File getExcelFile(String className) throws FileNotFoundException, IOException{
String excelName = getExcelName(className); //取类名
File file = null;
for(Object obj:Common.getDataAllFiles().keySet()){
if(String.valueOf(obj).contains(excelName)){//在配置文件的目录下如果含有与类名一样的Excel文件,那么就对该文件进行操作
file = new File((String)Common.getDataAllFiles().get((String.valueOf(obj)))+obj);
}
}
return file;
}
//获取文件名为测试类名的Excel文件中的第1行所有列的内容
public static Map getExcelFirstRows(String className) throws FileNotFoundException, IOException, BiffException{
String excelName = getExcelName(className); //取类名
HashMap<Integer,String> colName = new HashMap<>();
for(Object obj:Common.getDataAllFiles().keySet()){
if(String.valueOf(obj).contains(excelName)){//在配置文件的目录下如果含有与类名一样的Excel文件,那么就对该文件进行操作
Workbook wk = Workbook.getWorkbook(getExcelFile(className));
Sheet st = wk.getSheet(excelName);//获取与类名一致的sheet工作表
int cols = st.getColumns();
for(int i=0;i<cols;i++){
Cell cell = st.getCell(i, 0);//根据指定的列号和行号获取单元格
colName.put(i,cell.getContents());
}
}
}
return colName;
}
//获取文件名为测试类名的Excel文件中的第i行所有列的内容,str为调用类的类名
public static List getExcelVarRows(String className,int rowNo) throws FileNotFoundException, IOException, BiffException{
String excelName = getExcelName(className);
ArrayList<String> colName = new ArrayList<>();
for(Object obj:Common.getDataAllFiles().keySet()){
if(String.valueOf(obj).contains(excelName)){//在配置文件的目录下如果含有与类名一样的Excel文件,那么就对该文件进行操作
Workbook wk = Workbook.getWorkbook(getExcelFile(className));
Sheet st = wk.getSheet(excelName);//获取与类名一致的sheet工作表
int cols = st.getColumns();
for(int i=0;i<cols;i++){
Cell cell = st.getCell(i, rowNo);//根据指定的列号和行号获取单元格
colName.add(cell.getContents());
}
}
}
return colName;
}
//根据列名,返回所在列的所有行的所有内容
public static Map getColsFromColumnName(String className,String colName) throws FileNotFoundException, BiffException, IOException{
HashMap colNames = (HashMap)Common.getExcelFirstRows(className);
String excelName = getExcelName(className);
HashMap<Integer,String> colContents = new HashMap<>();
for(Object obj:colNames.keySet()){
String colValue = (String)colNames.get(obj);
if(((String)colValue).equals(colName)){
Workbook wk = Workbook.getWorkbook(getExcelFile(className));
Sheet st = wk.getSheet(excelName);//获取与类名一致的sheet工作表
int rowNo = st.getRows();
for(int i=0;i<rowNo;i++){
colContents.put(i, st.getCell((int)obj, i).getContents());
}
}
}
return colContents;
}
由于本人初学,代码写得粗糙,望各位高人多多指点,在此不胜感激。