httpclient框架实现接口自动化的思路(二)

1.编写testcase,并将执行结果写入excel

(1)编写testcase

public class Login extends BaseTest {    //继承可以不写该test ,但是不想通过testng.xml执行    @Test(dataProvider ="loginPasswordCase")    public void loginPassword(String caseId, String apiId,String parameters){        String url= RestfulUtil.getUrlByApiId(apiId);        String requestMdode= RestfulUtil.getRequestModeByApiId(apiId);        //解析json格式        JSONObject jsonObject = JSONObject.parseObject(parameters);        HashMap<String,String> hashMap=new HashMap<String, String>();        for (String key: jsonObject.keySet()) {            hashMap.put(key, (String) jsonObject.get(key));        }        String result= HttpUtil.doService(url,requestMdode, hashMap);        System.out.println(result);        //新建一个类,将要写入的内容封装至类,并统一放在集合内,等套件执行完毕后再批量写入至excel        WriteBackData writeBackData=new WriteBackData("测试用例",caseId,"ActualResponseData",result);        ExcelUtil.list.add(writeBackData);    }    @DataProvider(name = "loginPasswordCase")    public Object[][] testCase(){        Object[]value={"CaseId","ApiId","Parameter"};        Object[][] datas = CaseUtil.getCaseByApiId("1", value);        return datas;    }    @AfterSuite    public void batchWriteBack(){        ExcelUtil.batchWriteBackToExcel("D:\\Documents\\Desktop\\tester\\Api_auto\\src\\test\\resources\\testcase_v2.xls");    }}

(2)将执行结果结果批量写入excel 

I.写入excel 之前,首先要获取写入位置的行索引和列索引

public class ExcelUtil {
public static HashMap<String,Integer> cellMapping=new HashMap<>();public static HashMap<String,Integer> rowMapping=new HashMap<>();static {    loadRowNumAndCellNumMapping("D:\\Documents\\Desktop\\tester\\Api_auto\\src\\test\\resources\\testcase_v2.xls","测试用例");}
public static void loadRowNumAndCellNumMapping(String path, String sheetName) {        InputStream inputStream=null;        try {            inputStream = new FileInputStream(new File(path));            Workbook workbook = WorkbookFactory.create(inputStream);            Sheet sheet = workbook.getSheet(sheetName);//            通过标题获取标题所在的cellNum            Row titleRow = sheet.getRow(0);            int lastCellNum = titleRow.getLastCellNum();            if (titleRow != null && !isEmptyRow(titleRow)) {                for (int j = 0; j < lastCellNum; j++) {                    Cell cell = titleRow.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);                    //获取行号                    int column = cell.getAddress().getColumn();                    //获取行号对应的标题                    String title=new DataFormatter().formatCellValue(cell);                    if(title.contains("(")) {                    title=title.substring(0,title.indexOf("("));                    }                    cellMapping.put(title,column);                }            }            //获取每一行的数据            int lastRowNum = sheet.getLastRowNum();            for (int i = 1; i <=lastRowNum ; i++) {                Row data=sheet.getRow(i);                Cell FirstCellOfRow = data.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);                String caseId=new DataFormatter().formatCellValue(FirstCellOfRow);                int rowNum=data.getRowNum();                rowMapping.put(caseId,rowNum);            }        } catch (Exception e) {            e.printStackTrace();        }finally {            if (inputStream != null) {                try {                    inputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }
}II.创建类数据回写的类,将数据即将要回写到excel的数据回写到类,再从类中获取数据批量写到excel
public class WriteBackData {    private String sheetName;    private String caseId;    private String cellName;    private String result;}III.批量回写excel 操作
public static void batchWriteBackToExcel(String path){    InputStream inp= null;    OutputStream outputStream=null;    try {        inp = new FileInputStream(new File(path));        Workbook workbook = WorkbookFactory.create(inp);        for (WriteBackData writeBackData:list) {            //获取表单            Sheet sheet = workbook.getSheet(writeBackData.getSheetName());            //获取writeBackData中getcaseid的值,才能通过rowMapping中获取行号            String caseId = writeBackData.getCaseId();            Row row = sheet.getRow(rowMapping.get(caseId));            //获取类中cellName的值,才能获取对应的列号            Cell cell = row.getCell(cellMapping.get(writeBackData.getCellName()), Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);            cell.setCellValue(writeBackData.getResult());        }        outputStream=new FileOutputStream(new File(path));        workbook.write(outputStream);    } catch (Exception e) {        e.printStackTrace();    }finally {        try {            if(inp!=null){                inp.close();            }            if(outputStream!=null){                outputStream.close();            }        } catch (IOException e) {            e.printStackTrace();        }    }
 
 

相关推荐