在jmter測試過程中,有時候需要將測試結果寫入到excel文件,最近通過檸檬班學習到相關方法,下面將將相關方法整理,方便以后使用;
jmeter寫結果到excel文件,需要通過調用java方法實現,分為3步:
1)下載excel依賴包jxl.jar
下載地址:
鏈接:https://pan.baidu.com/s/11LMFAK4lRlh44EHECSSO2A
提取碼:414q
2)編寫java代碼,並導出為jar包,將jar包置於jmeter安裝目錄下/lib/ext目錄,代碼如下:
注:項目中需要導入jxl.jar包
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import jxl.Cell; import jxl.LabelCell; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Colour; import jxl.format.VerticalAlignment; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; public class CWOutputFile { /** * wOutputFile寫結果文件 wOutputFile(文件路徑、用例編號、用例標題、預期結果、實際結果、測試結果) * @throws IOException * @throws BiffException * @throws WriteException */ public void wOutputFile(String filepath,String caseNo,String testPoint,String testData,String preResult,String fresult) throws BiffException, IOException, WriteException{ File output=new File(filepath); String result = ""; InputStream instream = new FileInputStream(filepath); Workbook readwb = Workbook.getWorkbook(instream); WritableWorkbook wbook = Workbook.createWorkbook(output, readwb); //根據文件創建一個操作對象 WritableSheet readsheet = wbook.getSheet(0); //定位到文件的第一個sheet頁簽 int rsRows = readsheet.getRows(); //獲取sheet頁簽的總行數 /******************設置字體樣式***************************/ WritableFont font = new WritableFont(WritableFont.createFont("宋體"),10,WritableFont.NO_BOLD); WritableCellFormat wcf = new WritableCellFormat(font); /****************************************************/ Cell cell = readsheet.getCell(0,rsRows); //獲取sheet頁的單元格 if(cell.getContents().equals("")){ Label labetest1 = new Label(0,rsRows,caseNo); //第一列:用例編號 Label labetest2 = new Label(1,rsRows,testPoint);//第二列:用例標題 Label labetest3 = new Label(2,rsRows,testData); //第三列:測試數據 Label labetest4 = new Label(3,rsRows,preResult);//第四列:預期結果 Label labetest5 = new Label(4,rsRows,fresult); //第五列:實際結果 if(preResult.equals(fresult)){ result = "通過"; wcf.setBackground(Colour.BRIGHT_GREEN); //預期結果和實際結果相同,測試通過 } else{ result = "不通過"; wcf.setBackground(Colour.RED); //預期結果和實際結果不相同,測試不通過 } Label labetest6 = new Label(5,rsRows,result,wcf);//第六列:測試結果 readsheet.addCell(labetest1); readsheet.addCell(labetest2); readsheet.addCell(labetest3); readsheet.addCell(labetest4); readsheet.addCell(labetest5); readsheet.addCell(labetest6); } wbook.write(); wbook.close(); } /**cOutputFile 創建輸出Excel文件 cOutputFile,tradeType為文件名稱前綴,返回結果:文件路徑,作為wOutputFile寫入結果文件的入參 * @throws IOException * @throws WriteException */ public String cOutputFile(String tradeType) throws IOException, WriteException{ String temp_str = ""; Date dt = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); temp_str = sdf.format(dt); //獲取時間戳 String filepath = "D:\\\\"+tradeType+"_output_" + "_" + temp_str + ".xls"; File output = new File(filepath); if(!output.isFile()){ //文件不存在,創建新文件 output.createNewFile(); //寫文件 WritableWorkbook writeBook = Workbook.createWorkbook(output); WritableSheet sheet = writeBook.createSheet("輸出結果", 0); //命名sheet WritableFont headfont = new WritableFont(WritableFont.createFont("宋體"),11,WritableFont.BOLD);//設置首行字體為宋體,11號,加粗 WritableCellFormat headwcf = new WritableCellFormat(headfont); headwcf.setBackground(Colour.GRAY_25); sheet.setColumnView(0, 11); //設置列寬 sheet.setColumnView(1, 20); sheet.setColumnView(2, 40); sheet.setColumnView(3, 10); sheet.setColumnView(4, 10); sheet.setColumnView(5, 10); headwcf.setAlignment(Alignment.CENTRE); //文字居中 headwcf.setVerticalAlignment(VerticalAlignment.CENTRE); Label labe00 = new Label(0,0,"用例編號",headwcf); //寫入內容:Label(列號,行號,內容) Label labe10 = new Label(1,0,"用例標題",headwcf); Label labe20 = new Label(2,0,"測試數據",headwcf); Label labe30 = new Label(3,0,"預期結果",headwcf); Label labe40 = new Label(4,0,"實際結果",headwcf); Label labe50 = new Label(5,0,"執行結果",headwcf); sheet.addCell(labe00); sheet.addCell(labe10); sheet.addCell(labe20); sheet.addCell(labe30); sheet.addCell(labe40); sheet.addCell(labe50); writeBook.write(); writeBook.close(); } return filepath; } }
導出jar包方法:選擇項目->右擊鼠標->選擇export ->選擇JAR File
jar包(CWOutputFile.jar)下載地址:
鏈接:https://pan.baidu.com/s/1LsOCPMD9hUfea8j-rPy4Xg
提取碼:7erg
3)jmeter通過beanshell取樣器調用java方法實現創建excel文件和寫入結果到Excel文件
創建Excel文件:
//新建對象 t = new CWOutputFile(); //新建excel文件 String filepath = t.cOutputFile("測試"); //保存excel文件路徑保存為jmeter變量 vars.put("filepath",filepath);
保存結果到Excel文件:
//創建寫回結果的對象 s = new CWOutputFile(); //參數准備 String preResult = vars.get("preResult"); //獲取參數化文件中的變量,返回值為字符串類型,否則直接引用為json類型 String fresult = vars.get("fresult");//獲取正則表達式中的結果文件,返回值為字符串類型,否則直接引用為json類型 //寫結果到excel文件,調用方法wOutputFile方法 s.wOutputFile("${filepath}","${caseNo}","${testPoint}","${testData}",preResult,fresult);
執行結果:
注:
1)當寫入文件的參數不是字符串類型,例如json格式時,需要通過vars.get(參數名)方法,將參數轉化為字符串類型,然后寫入結果文件;
2)寫入文件中的參數如果包含中文,csv數據文件編碼格式要置為空,否則會出現字符亂碼;