jmeter beanshell

话不多说,请看下面代码
1.取正则表达式读取的变量,通过_matchNr,取出所有匹配结果数量,载进行数据加工、断言
通过vars.get() 读取变量的值
String bank = "bank_";
String name = "name_";
String country = "country_";
String bankCode = "${bankCode}";
String bankName="${bankName}";
String countryName="${countryName}";

for(int i=1;i<=${bank_matchNr};i++){
j=i;
String getBank = bank+j;
String getName = name+j;
String getCountry = country+j;
String json = vars.get(getBank);
String json1 = vars.get(getName);
String json2 = vars.get(getCountry);
if(json.contains(bankCode)&&json1.equals(bankName)&&json2.equals(countryName))
{
}
else{
Failure=true;
FailureMessage = "error, check";
}
}
if(i==${bank_matchNr}){
FailureMessage = "ok!";
}

2.通过prev.getResponseDataAsString();读取响应,然后处理加工数据
样例1:
String response_data=prev.getResponseDataAsString();
String loanApplicationOID=vars.get("loanApplicationOID");
String response_data1=response_data.replace(""writeoffFlag":false",""writeoffFlag":false,"admin": false,"loanApplicationOID": ""+loanApplicationOID+""");
vars.put("submitResponse_data",response_data1);

样例2:
import java.util.ArrayList;
import org.json.*;
String fieldName_matchNr = vars.get("fieldName_matchNr");
int len=Integer.parseInt(fieldName_matchNr);
String response_data=prev.getResponseDataAsString();
JSONObject data_obj= new JSONObject(response_data);
ArrayList list =new ArrayList(30);

list.add("${formName}"); //事由 默认值:“表单名称”
list.add("${departmentOID}"); //部门的value
list.add("${loanAmount}"); //借款金额 默认值:800
list.add("${nextday6}"); //预计还款日期:6天以后
list.add("${supplierId},${bankId}"); //收款单位/个人
//log.info(list.toString());
StringBuilder sb = new StringBuilder();
for(int i=0;i<len;i++){
String ids= data_obj.get("customFormFields").get(i).get("id").toString();
String fieldOIDs= data_obj.get("customFormFields").get(i).get("fieldOID").toString();
String messageKeys= data_obj.get("customFormFields").get(i).get("messageKey").toString();
String fieldNames= data_obj.get("customFormFields").get(i).get("fieldName").toString();
String fieldTypes= data_obj.get("customFormFields").get(i).get("fieldType").toString();
String reportShows= data_obj.get("customFormFields").get(i).get("reportShow").toString();
String isPDFShows= data_obj.get("customFormFields").get(i).get("isPDFShow").toString();
String requireds= data_obj.get("customFormFields").get(i).get("required").toString();
String readonlys= data_obj.get("customFormFields").get(i).get("readonly").toString();
String dataSources= data_obj.get("customFormFields").get(i).get("dataSource").toString().replace(""","\\"");
String fieldConstraints= data_obj.get("customFormFields").get(i).get("fieldConstraint").toString().replace(""","\\"");
String fieldContents= data_obj.get("customFormFields").get(i).get("fieldContent").toString().replace(""value": null",""value":"${formName}"").replace(""","\\"").replace("\n","\\n");
String valueKey=list.get(i).toString();
String sd1="{"id":""+ids+"","fieldOID":""+fieldOIDs+"","sequence":"+i+","messageKey":""+messageKeys+"","formId":null,"fieldName":""+fieldNames+"","fieldType": ""+fieldTypes+"","dataSource":""+dataSources+"","valid": true,"required": "+requireds+","createdDate": null,"lastModifiedDate": null,"promptInfo": "","fieldConstraint": ""+fieldConstraints+"","readonly": "+readonlys+","isReadOnly": false,"guiWidgetId": null,"value":""+valueKey+"","fieldContent":""+fieldContents+"","fieldCode": null,"isPDFShow": "+isPDFShows+","reportShow": "+reportShows+","reportOrmKey": null,"formOID": "${formOID}","i18n": null},";
String sd2="{"id":""+ids+"","fieldOID":""+fieldOIDs+"","sequence":"+i+","messageKey":""+messageKeys+"","formId":null,"fieldName":""+fieldNames+"","fieldType": ""+fieldTypes+"","dataSource":""+dataSources+"","valid": true,"required": "+requireds+","createdDate": null,"lastModifiedDate": null,"promptInfo": "","fieldConstraint": ""+fieldConstraints+"","readonly": "+readonlys+","isReadOnly": false,"guiWidgetId": null,"value":""+valueKey+"","fieldContent":""+fieldContents+"","fieldCode": null,"isPDFShow": "+isPDFShows+","reportShow": "+reportShows+","reportOrmKey": null,"formOID": "${formOID}","i18n": null}";
if(i == len-1){
sb.append(sd2);
}else{
sb.append(sd1);
}
}
String customform=sb.toString().replaceAll(""null"","null");
String customform="["+customform+"]";
vars.put("customform",customform);
String ss=vars.get("customform");
//log.info("customform===" + ss.toString());

样例3 读取文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;

	String filePath ="${fileName}" ;
FileOutputStream out = null;
XSSFWorkbook workbook;
String expectFontName="MS Pゴシック";
short  expectFontSize=11;
short  expectFontColor_Red=5;
short  expectFontColor_Black=2;
short  expectFontColor_Black_1=6;
boolean expectDataFormat=true;
boolean expectFontBold=true;
String expectFontVerticalCenter="CENTER";
String expectFontLevelCenter="CENTER";
String expectFontLevelLeft="LEFT";
String expectFontGeneralCenter="GENERAL";
short expectCellColor=64;
try {
	File excel = new File(filePath); // 读取文件
	FileInputStream	in = new FileInputStream(excel);
	workbook = new XSSFWorkbook(in);
	XSSFSheet sheet = workbook.getSheetAt(0);
	System.out.println("第一个sheet名称"+sheet.getSheetName());					
	XSSFRow row;
	XSSFCell column;
	XSSFCellStyle  cellStyle;
	for(int i=0;i<= sheet.getLastRowNum();i++){
		row=sheet.getRow(i);
		if(row!=null){
			for(int j=0;j<=row.getLastCellNum();j++){
				column=row.getCell(j);
				if(column!=null){
					cellStyle = column.getCellStyle();
					String fontName =cellStyle.getFont().getFontName();
					short fontSize =cellStyle.getFont().getFontHeightInPoints();
					boolean fontBold =cellStyle.getFont().getBold();
					short fontColor=cellStyle.getFont().getIndex();
					String fontLevelCenter=cellStyle.getAlignmentEnum().toString();
					String fontVerticalCenter=cellStyle.getVerticalAlignmentEnum().toString();
					short cellColor=cellStyle.getFillForegroundColor();
					System.out.println("第"+i+"行第"+j+"列,字体名称:"+fontName+",字体大小:"+fontSize+",单元格填充色:"+cellColor);
					System.out.println("第"+i+"行第"+j+"列,列内容:"+column+",垂直对齐:"+fontVerticalCenter+",水平对齐:"+fontLevelCenter);
					Assert.assertEquals(fontName, expectFontName);
					Assert.assertEquals(fontSize, expectFontSize);
					Assert.assertEquals(cellColor, expectCellColor);
					Failure = false;
					FailureMessage = "字体名称、字体大小、单元格背景色校验成功";
					if(i==1){
						Assert.assertEquals(fontBold, expectFontBold);
						Assert.assertEquals(fontLevelCenter, expectFontLevelCenter);
						Assert.assertEquals(fontVerticalCenter, expectFontVerticalCenter);
						if(j<3 || j==5){
							Assert.assertEquals(fontColor, expectFontColor_Red);
						}else{
							Assert.assertEquals(fontColor, expectFontColor_Black);
						}
						Failure = false;
					     FailureMessage = "字体加粗、水平居中、垂直居中、字体颜色校验成功";
					}
				}

			}
		}
	}
	out = new FileOutputStream(filePath);
	workbook.write(out);
	out.flush();
	out.close();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

样例4 使用googleJson 读取响应,替换数据
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.JsonObject;

String Resp = prev.getResponseDataAsString();
log.info("***Resp:"+Resp);
JsonObject jsonObject=new JsonParser().parse(Resp).getAsJsonObject();

jsonObject.addProperty("companyId", vars.get("companyId"));
jsonObject.addProperty("companyOID", vars.get("companyOID"));
jsonObject.addProperty("companyName", vars.get("companyName"));

String str =jsonObject.toString();
log.info("***str:"+str);
vars.put("content",str);

样例5 获取时间戳、时间
import java.text.SimpleDateFormat;
import java.util.Date;

Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, 0);
date = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String today = formatter.format(date);
vars.put("today", today);

Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, 1);
date = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String today = formatter.format(date);
vars.put("nextday", today);

posted @ 2020-11-15 12:07  小黑ようちょう  阅读(156)  评论(0)    收藏  举报