统计行号列号
1 import java.io.BufferedReader; 2 import java.io.FileReader; 3 import java.io.File; 4 5 6 print("========输出结果表格${csvPath}============ "+"${csvPath}"); 7 print("========Case 表格${csvPath}============ "+"${csvCase}"); 8 9 BufferedReader br=new BufferedReader(new FileReader("${csvCase}")); 10 //BufferedReader bufRdr = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8")); 11 String tmpStr=""; 12 int rowNum=0; 13 while(tmeStr=br.readLine()!=null){ 14 rowNum++; 15 } 16 rowNum=rowNum-1;//去掉CSV文件标题行; 17 vars.put("rowNum",String.valueOf(rowNum)); 18 print("-----------------1CSV文件行数:--------"+rowNum+"-------------------"); 19 log.info("-----------------1CSV文件行数:--------"+rowNum+"-------------------"); 20 21 print("-----------------Case源CSV表格路径:--------"+"${csvCase}"+"-------------------"); 22 //String getRowNum=vars.get("rowNum"); 23 24 //int csvNo= Integer.parseInt(vars.get("rowNum")); 25 26 27 int totalElements= Integer.parseInt(vars.get("rowNum")); 28 print("Total_csvNoCSV总行号---->> "+totalElements); 29 vars.put("totalElements", String.valueOf(totalElements)); 30 //print(${__javaScript("${totalElements}")}); 31 //print("======javaScript======"+${__javaScript(totalElements)});
prev.setDataEncoding("utf-8");
caseNo,testInput,intent,domain,expectedResult,PASS_FAIL,actualResult,response,description,tester
1 import java.util.Collection; 2 import java.util.Iterator; 3 import java.util.*; 4 import java.io.FileWriter; 5 import java.util.Arrays; 6 import java.io.Writer; 7 import java.util.List; 8 import com.alibaba.fastjson.JSONPath; 9 import com.alibaba.fastjson.JSON; 10 import com.alibaba.fastjson.JSONArray; 11 import com.alibaba.fastjson.JSONObject; 12 import Util.WriteStreamAppend; 13 import Util.Excel; 14 import java.io.BufferedReader; 15 import java.io.FileReader; 16 import java.io.File; 17 import org.apache.log4j.Logger; 18 import com.cloudminds.csv2excel; 19 import com.cloudminds.csv2excel.CsvToExcel; 20 import com.cloudminds.csv2excel.CsvToExcel.convertCsvToXls; 21 import com.cloudminds.CsvToExcel.convertCsvToXls; 22 import java.io.FileOutputStream; 23 import java.io.FileReader; 24 import java.io.IOException; 25 import org.apache.jmeter.assertions; 26 import org.apache.jmeter.samplers.SampleResult; 27 import org.apache.jmeter.assertions.AssertionResult; 28 import org.json.*; 29 30 31 import org.apache.commons.lang.math.NumberUtils; 32 import org.apache.log4j.Logger; 33 import org.apache.poi.ss.usermodel.Row; 34 import org.apache.poi.ss.usermodel.Workbook; 35 import org.apache.poi.xssf.streaming.SXSSFSheet; 36 import org.apache.poi.xssf.streaming.SXSSFWorkbook; 37 import com.opencsv.CSVReader; 38 import com.cloudminds.csv2excel.AppMain; 39 40 41 //print("========输出结果表格${csvPath}============ "+"${csvPath}"); 42 //print("========Case 表格${csvPath}============ "+"${csvCase}"); 43 // 44 //BufferedReader br=new BufferedReader(new FileReader("${csvCase}")); 45 //String tmpStr=""; 46 //int rowNum=0; 47 //while(tmeStr=br.readLine()!=null){ 48 // rowNum++; 49 //} 50 //rowNum=rowNum-1;//去掉CSV文件标题行; 51 //vars.put("rowNum",String.valueOf(rowNum)); 52 //print("-----------------CSV文件行数:--------"+rowNum+"-------------------"); 53 //log.info("-----------------CSV文件行数:--------"+rowNum+"-------------------"); 54 55 print("${caseNo}"); 56 print("${testInput}"); 57 print("${intent}"); 58 print("${expectedResult}"); 59 print("${actualResult}"); 60 61 62 String prevQuery = prev.getSamplerData(); 63 print("当前的接口请求拼接 输出---->> "+prevQuery); 64 //CookieManager manager = ctx.getCurrentSampler().getCookieManager(); 65 //print(manager); 66 String response=prev.getResponseDataAsString(); 67 print("当前的接口返回值response 输出---->> "+response); 68 String tr1 =response; 69 vars.put("rsp",response); 70 print("tr1--->>> "+tr1); 71 print("==================== "+${ResponceData}); 72 // 转换成JSON串 73 JSONObject json = JSONObject.parseObject(${ResponceData}); 74 System.out.println("1当前json--->> "+ json); 75 JSONObject result = JSON.parseObject(${ResponceData}); 76 System.out.println("2当前tr1--->> "+ result); 77 78 79 print("断言$.json==================== "+"${source}"+" "+"${text}"+" "+"${intent_json}"+" "+"${url}"); 80 Object source = JSONPath.eval(json, "${source}"); 81 Object text = JSONPath.eval(json, "${text}"); 82 Object intent_json = JSONPath.eval(json, "${intent_json}"); 83 Object url = JSONPath.eval(json, "${url}"); 84 85 String sou=JSONObject.toJSONString(source); 86 String tex=JSONObject.toJSONString(text); 87 String inte=JSONObject.toJSONString(intent_json); 88 String ur=JSONObject.toJSONString(url); 89 print(sou); 90 print(tex); 91 print(inte); 92 print(ur); 93 //String arsult="source字段: "+sou+" text字段: "+tex+" intent字段: "+inte+" url字段: "+ur; 94 String arsult="source= "+sou+";"+" text= "+tex+";"+" intent= "+inte+";"+" url= "+ur+";"+"当前AgentID= "+"${agentId}"; 95 print(arsult); 96 vars.put("arsult",arsult); 97 98 99 //默认走,分割 100 char SEPARATOR = ','; 101 102 public void writeLine(FileWriter writer, String[] params, char separator) 103 { 104 boolean firstParam = true; 105 StringBuilder stringBuilder = new StringBuilder(); 106 String param = ""; 107 for (int i = 0; i < params.length; i++) 108 { 109 param = params[i]; 110 log.info(param); 111 print("===========写入成功============"); 112 //如果行中的第一个参数,则不需要分隔符 113 if (!firstParam) 114 { 115 stringBuilder.append(separator); 116 } 117 //append 118 stringBuilder.append(param); 119 firstParam = false; 120 } 121 stringBuilder.append(" "); 122 print("换行==================== 。"); 123 log.info(stringBuilder.toString()); 124 writer.append(stringBuilder.toString()); 125 print("写入的数据 "+stringBuilder.toString()); 126 127 } 128 129 //get path of csv file (creates new one if its not exists) 130 print("========检查csv写入============ "+"${csvPath}"); 131 String csvFile = "${csvPath}"; 132 133 print("开始写入数据======= "); 134 135 print("1断言内容======= "+"${assert_content}"); 136 String ErrorValue ="${assert_content}"; 137 print("2断言内容======= "+ErrorValue); 138 139 if(prev.getResponseDataAsString().contains("${assert_content}")){ 140 //大入口统一成功 141 142 143 if (!(ur=="")) { 144 if (!(ur.contains("mp3"))) { 145 String fail="FAIL"; 146 print(arsult); 147 vars.put("passorFail",fail); 148 Failure = true;//直接判断失败,表示接口跑失败,在结果树中sample是红色的 149 FailureMessage =" 当前断言不包含 " + "${assert_content}" + "." ; 150 print ( " 未通过当前断言不包含 " + "${assert_content}"); // this goes to stdout 151 log.warn( "未通过当前断言不包含 " + "${assert_content}"); // this goes to the JMeter log file 152 } 153 154 } 155 156 157 if (!(inte=="")) { 158 if (!((inte.contains("cm_dance")) || (inte.contains("cloudminds")))) { 159 String fail="FAIL"; 160 print(arsult); 161 vars.put("passorFail",fail); 162 Failure = true;//直接判断失败,表示接口跑失败,在结果树中sample是红色的 163 FailureMessage =" 当前断言不包含 " + "${assert_content}" + "." ; 164 print ( " 未通过当前断言不包含 " + "${assert_content}"); // this goes to stdout 165 log.warn( "未通过当前断言不包含 " + "${assert_content}"); // this goes to the JMeter log file 166 } 167 168 } 169 170 171 if (!(tex==null)) { 172 if (!(tex.length()>500)) { 173 String fail="FAIL"; 174 print(arsult); 175 vars.put("passorFail",fail); 176 Failure = true;//直接判断失败,表示接口跑失败,在结果树中sample是红色的 177 FailureMessage =" 当前断言不包含 " + "${assert_content}" + "." ; 178 print ( " 未通过当前断言不包含 " + "${assert_content}"); // this goes to stdout 179 log.warn( "未通过当前断言不包含 " + "${assert_content}"); // this goes to the JMeter log file 180 } 181 182 } 183 184 185 186 System.out.println("大入口统一Pass"); 187 String ps="PASS"; 188 vars.put("passorFail",ps); 189 190 } 191 192 193 else{ 194 String fail="FAIL"; 195 print(arsult); 196 vars.put("passorFail",fail); 197 Failure = true;//直接判断失败,表示接口跑失败,在结果树中sample是红色的 198 FailureMessage =" 当前断言不包含 " + "${assert_content}" + "." ; 199 print ( " 未通过当前断言不包含 " + "${assert_content}"); // this goes to stdout 200 log.warn( "未通过当前断言不包含 " + "${assert_content}"); // this goes to the JMeter log file 201 202 } 203 204 205 String[] params ={"${caseNo}", "${testInput}", "${intent}","${domain}", "${expectedResult}",vars.get("passorFail"), vars.get("arsult") ,vars.get("rsp"),"${description}","${tester}"}; 206 207 FileWriter fileWriter = new FileWriter(csvFile, true); 208 writeLine(fileWriter, params, SEPARATOR); 209 210 fileWriter.flush(); 211 fileWriter.close(); 212 private static Logger logger = Logger.getLogger(AppMain.class); 213 fileLoc = CsvToExcel.convertCsvToXls("${csvPath}", csvFile); 214 logger.info("文件位置==>> " + fileLoc); 215
响应断言判断
//响应内容正则提取部分 String ResponseResult = vars.get("ResponseResult"); //断言 String Assertion = vars.get("Assertion"); //判断断言是否通过 if(ResponseResult.equals(Assertion) || ResponseResult.equals("Process Case")){ Failure = false; //兼容其他并存的断言 FailureMessage = "接口响应: " + prev.getResponseDataAsString() + " "; }else{ Failure = true; if (prev.getResponseDataAsString().length() < 255) { FailureMessage = "接口响应: " + prev.getResponseDataAsString() + " "; }else{ FailureMessage = "接口响应片段: " + ResponseResult + " "; } FailureMessage += "用例断言: " + Assertion; String logInfo = " "; logInfo = logInfo + prev.getThreadName() + " " + prev.getSampleLabel() + " "; logInfo = logInfo + prev.getSamplerData() + " "; logInfo = logInfo + prev.getResponseCode() + " "; logInfo = logInfo + prev.getResponseDataAsString() + " "; log.info(logInfo); }
写入csv
import com.csvreader.CsvWriter; String NewDataPath="C:\"; CsvWriter cw = new CsvWriter(NewDataPath+"\test.csv"); cw.writeRecord(new String[]{"test1","test2","test3"}); cw.writeRecord(new String[]{"a","aa","aaa"}); cw.close();
下载javacsv.jar,然后将其放入apache-jmeter-*lib目录
import com.csvreader.CsvWriter;