利用空闲之余,写了第一个接口自动化测试demo, 通过读取execl中的接口测试用例,接口自动执行。(这里跟很多网上的接口自动化有点不同的是:无需再写代码,只需要从execl中增加用例,就可执行)。
这是execl的模板:
这个模板可以很好的管理项目的各个模块,看起来也是简洁,也是颇为喜欢的~~
主要就是写了这几个类,完成了接口自动化的第一步:
在尝试写这个demo时,最大的问题困扰的我是:每个请求的参数方式(请求参数和body参数)不一致,个数不一致。怎样能用简洁的方式实现? 最终解决的办法就是:
在execl中增加参数类型判断:paramType, 如果是params,封装一个将json格式的字符串转换成map格式,如果是body, 就直接使用execl中的json格式。
好了,上代码(只是简单的实现,还没有很多处理,如果你们有幸看到,可以自己在这个基础上改进完善,我也会一直完善这个框架,嘻~~)
首先是util:
execlUtil:读取execl数据
package utils; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtils { Sheet sheet = null; Row row = null; List<Map<String,String>> list = null; String cellData = null; String filePath =System.getProperty("user.dir")+"/XapiCaseData/ScooperApiTestCase.xlsx"; String columns[] = {"caseName","caseDescription","isRun","baseURI","path","method","paramType","params"}; Workbook wb = readExcel(filePath); /** * 获取excel的sheet的数量 */ public int getSheetNum(){ return wb.getNumberOfSheets(); } /** * 获取excel的sheet[]的名字 */ public String getSheetName(int i){ return wb.getSheetName(i); } /** * 读取sheet里的接口用例参数: "caseName","caseDescription","isRun","baseURI","path","method","paramType","params" * @param sheetNum * @return List */ public List<Map<String,String>> getTestData(int sheetNum) { //获取sheet sheet = wb.getSheetAt(sheetNum); if(wb != null){ //用来存放表中数据 list = new ArrayList<Map<String,String>>(); //获取最大行数 int rownum = sheet.getPhysicalNumberOfRows(); //获取第一行 row = sheet.getRow(0); //获取最大列数 int colnum = row.getPhysicalNumberOfCells(); //循环遍历各个sheets的行和列值添加到List集合 for (int i = 1; i < rownum; i++) { Map<String, String> map = new LinkedHashMap<String, String>(); row = sheet.getRow(i); if (row != null) { for (int j = 0; j < colnum; j++) { cellData = (String) getCellFormatValue(row.getCell(j)); map.put(columns[j], cellData); } } else { break; } list.add(map); } } return list; } //读取excel public static Workbook readExcel(String filePath){ Workbook wb = null; if(filePath==null){ return null; } String extString = filePath.substring(filePath.lastIndexOf(".")); InputStream is = null; try { is = new FileInputStream(filePath); if(".xls".equals(extString)){ return wb = new HSSFWorkbook(is); }else if(".xlsx".equals(extString)){ return wb = new XSSFWorkbook(is); }else{ return wb = null; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return wb; } public static Object getCellFormatValue(Cell cell){ Object cellValue = null; if(cell!=null){ //判断cell类型 switch(cell.getCellType()){ case Cell.CELL_TYPE_NUMERIC:{ cellValue = String.valueOf(cell.getNumericCellValue()); break; } case Cell.CELL_TYPE_FORMULA:{ //判断cell是否为日期格式 if(DateUtil.isCellDateFormatted(cell)){ //转换为日期格式YYYY-mm-dd cellValue = cell.getDateCellValue(); }else{ //数字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING:{ cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = ""; } }else{ cellValue = ""; } return cellValue; } }
logUtil类:打印日志
package utils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class LogUtils { //class对象 private final Class<?> clazz; private Logger logger; public LogUtils(Class<?> clazz){ this.clazz = clazz; this.logger = LogManager.getLogger(this.clazz); } public void info(String message){ logger.info(clazz.getCanonicalName()+": "+message); } public void debug(String message){ logger.debug(clazz.getCanonicalName()+": "+message); } public void error(String message){ logger.error(clazz.getCanonicalName()+": "+message); } public void trace(String message){ logger.trace(clazz.getCanonicalName()+": "+message); } public void warn(String message){ logger.warn(clazz.getCanonicalName()+": "+message); } }
getRequestValue类:获取请求值
package baseframework; import org.json.JSONObject; import utils.ExcelUtils; import utils.LogUtils; import java.util.*; public class GetRequestValue { ExcelUtils excelUtils = new ExcelUtils(); LogUtils logUtils = new LogUtils(GetRequestValue.class); List<Map<String, String>> list =null; /** * 获取excel的数据 * @param sheetNum * @return list */ public List list(int sheetNum){ list = excelUtils.getTestData(sheetNum); return list; } //excel的sheet数量 public int getSheetNum(){ return excelUtils.getSheetNum(); } public String isRun(int i){ String isRun = list.get(i).get("isRun"); logUtils.info("此条用例的运行状态:"+isRun); return isRun; } public String method(int i){ String method = list.get(i).get("method"); logUtils.info("发送请求方法:"+method); return method; } public String paramType(int i){ String paramType = list.get(i).get("paramType"); return paramType; } //获取excel中的:"sheetName","caseName","caseDescription","isRun","baseURI","path","Method","paramType" public String sheetName(int i){ String sheetName=excelUtils.getSheetName(i) ; logUtils.info("***********************开始执行:" + sheetName + " 接口用例*********************"); return sheetName; } public String caseName(int i){ String caseName= list.get(i).get("caseName"); logUtils.info("==============caseName:"+caseName+"=================="); return caseName; } public String caseDescription(int i) { String caseDescription =list.get(i).get("caseDescription"); logUtils.info("测试"+caseDescription); return caseDescription; } public String baseURI(int i){ String baseURI = list.get(i).get("baseURI"); logUtils.info("baseURI: "+baseURI); return baseURI; } public String path(int i) { String path =list.get(i).get("path"); logUtils.info("path: "+path); return path; } /** * json格式的参数 * */ public String getRequestBodyParams(int i) { String params =list.get(i).get("params"); logUtils.info("请求参数:\n\n"+params+"\n"); return params; } /** * 将json格式的参数转换成map格式 * */ public Map getRequestParams(int i) throws Exception { Map<String, Object> paramsMap=null; //循环读取测试case String params = list.get(i).get("params"); JSONObject jsonPath = new JSONObject(params); Stack<JSONObject> stObj = new Stack<JSONObject>(); stObj.push(jsonPath); paramsMap = new HashMap<String, Object>(); JsonToMap(stObj, paramsMap); return paramsMap; } /** * 将json格式的键值对转换成map的方法 * @param stObj * @param paramsMap * @throws Exception */ public void JsonToMap(Stack<JSONObject> stObj, Map<String, Object> paramsMap) throws Exception { if(stObj == null && stObj.pop() == null){ return ; } JSONObject json = stObj.pop(); Iterator it = json.keys(); while(it.hasNext()){ String key = (String) it.next(); //得到value的值 Object value = json.get(key); //System.out.println(value); if(value instanceof JSONObject) { stObj.push((JSONObject)value); //递归遍历 JsonToMap(stObj,paramsMap); } else { paramsMap.put(key, value); } } } }
assertRequestValue类:判断execl中的请求参数
package baseframework; import io.restassured.response.Response; import java.util.Map; public class AssertRequestValue { RequestConfig requestConfig =new RequestConfig(); GetRequestValue getRequestValue = null; public void setGetRequestValue(GetRequestValue getRequestValue){ this.getRequestValue=getRequestValue; } public Response assertApiParams(int i) throws Exception { Response response=null; /** * 判断excel表字段:isRun * 1、如果是Y, * 2、否则 不运行这条case */ if(getRequestValue.isRun(i).equals("Y")){ /** * 1、先判断请求参数:请求参数是params * 2、再判断请求方式:post or get * */ if(getRequestValue.paramType(i).equals("params")){ Map<String,String> params = getRequestValue.getRequestParams(i); if(getRequestValue.method(i).equals("post")){ try { response= requestConfig.setPost(params,getRequestValue.path(i)); } catch (Exception e) { e.printStackTrace(); } }else { try { response=requestConfig.setGet(params,getRequestValue.path(i)); } catch (Exception e) { e.printStackTrace(); } } } /** * 1、先判断请求参数:请求参数是body * 2、再判断请求方式:post or get * */ if(getRequestValue.paramType(i).equals("body")){ String bodyParams=getRequestValue.getRequestBodyParams(i); if(getRequestValue.method(i).equals("get")){ try { response=requestConfig.setGet(bodyParams,getRequestValue.path(i)); } catch (Exception e) { e.printStackTrace(); } }else { try { response= requestConfig.setPost(bodyParams,getRequestValue.path(i)); } catch (Exception e) { e.printStackTrace(); } } } } return response; } }
requestConfig类:请求配置
package baseframework; import io.restassured.RestAssured; import io.restassured.response.Response; import java.util.Map; public class RequestConfig { //设置请求的地址和端口号 public void setHttpURIandPortValue(String URI, int port){ RestAssured.baseURI =URI; RestAssured.port = port; } //发送get请求,参数为params public Response setGet(Map setParams,String URI){ return RestAssured.given().params(setParams).when().get(URI); } //发送post请求,参数为params public Response setPost(Map setParams,String URI){ return RestAssured.given().params(setParams).when().post(URI); } //发送post请求,参数为body public Response setPost(String setParams,String URI){ return RestAssured.given().body(setParams).when().post(URI); } //发送get请求,参数为body public Response setGet(String setParams,String URI){ return RestAssured.given().body(setParams).when().post(URI); } }
最后就是跑用例的代码了,runApiExeclCase类:执行execl中的用例
package eagleweb; import baseframework.AssertRequestValue; import baseframework.GetRequestValue; import baseframework.RequestConfig; import io.restassured.response.Response; import utils.AssertUtils; public class RunApiExcelCase { public static void main(String[] args) throws Exception { RequestConfig requestConfig = new RequestConfig(); GetRequestValue getRequestValue = new GetRequestValue(); AssertRequestValue assertRequestValue = new AssertRequestValue(); assertRequestValue.setGetRequestValue(getRequestValue); AssertUtils assertUtils = new AssertUtils(); for(int s=0;s<getRequestValue.getSheetNum();s++){ /** * 获取excel的sheetName */ getRequestValue.sheetName(s); /** * 循坏执行excel的接口测试用例 * 传入参数sheetNum * */ for(int i=0;i<getRequestValue.list(s).size();i++){ //接口测试名i getRequestValue.caseName(i); //接口测试描述 getRequestValue.caseDescription(i); //接口URI、端口 requestConfig.setHttpURIandPortValue(getRequestValue.baseURI(i),90); //打印响应结果 Response response= assertRequestValue.assertApiParams(i); response.prettyPrint(); System.out.println("\n\n"); //判断状态码是不是200 assertUtils.statusCode200(response); } } } }
这就是我写的第一个demo了,适用于平常比较普通的接口,异常和断言以及输出报告还没有加进去,后续会一直完善的。
————————————————
版权声明:本文为CSDN博主「Kitten_336368」的原创文章,遵循 CC 4.0 BY-SA 版权协议,原文链接:https://blog.csdn.net/xiaomaoxiao336368/article/details/82838587.