jmeter beanshell 之常用的代码

fastjson下载和org.json下载,在本文最后面附上下载连接。自行下载。

 

 

jmeter beanshell 获取浮点、整数型、字符串型变量

代码如下:

float test= Float.parseFloat(vars.get("test")); //获取浮点数test变量的值

int test= Integer.parseInt(vars.get("test"));//获取整数型test变量的值

String test= vars.get("test");//获取字符串型test变量的值

 

 

 

精确保留小数点后两位

1、jmeter beanshell 求精确到最后两位数,要求四舍五入

代码如下:

import java.text.DecimalFormat;
//四舍五入
DecimalFormat mFormat = new DecimalFormat("#.00");

String test = mFormat.format(test); //将test变量四舍五入

2、jmeter beanshell 求精确到最后两位数,直接截取不做四舍五入

代码如下:

import java.text.DecimalFormat;
DecimalFormat mFormat = new DecimalFormat("0.00");

String test = mFormat.format(test); //将test变量直接截取保留小数点后两位,不做四舍五入

 

beanshell--替换数组中的某个字段/替换json的某key的value值,直接用jsonobject.put("isShow", true);

举例如下:

/*  返回的response data 信息参考如下
{
	"code":200,
	"data":{
		"auditor":null,
		"status":1,
		"storageGoodsList":[
			{
				"abstracts":null,
				"allocateQuantity":null,
				"allocateReason":null,
				"businessType":"DGRK",
				"createdTime":"2020-08-06 17:31:08",
				"createdUser":null,
				"expireTime":"2020-08-06 00:00:00",
				"goodsId":"1290829975123123",
				"goodsName":"商品测试",
				"goodsNo":"10011003100400020",
				"goodsStock":0,
				"id":"1291305800404307971",
				"inSurplusQuantity":null,
				"isShow":0,
				"meteringUnit":"个(1箱)",
				"outQuantity":13,
				"outSurplusQuantity":null,
				"perPrice":100.00,
				"productNo":"H012020062900000144",
				"productTime":"2020-08-06 00:00:00",
				"profit":39.00,
				"promotionId":null,
				"purchasePrice":100.00,
				"realInQuantity":13,
				"relationId":"1291305800404307970",
				"remark":"物品信息备注---自动化测试脚本",
				"saleAmount":1300.00,
				"sellingPrice":103.00,
				"skuValue":"重量:200g;包装:10小包;",
				"stockBatchNo":"200806173100001",
				"stockUnit":"个(1箱)",
				"storageCode":"DG320506001",
				"storageName":"仓库测试)",
				"subtotalAmount":1300.00,
				"surplusQuantity":null,
				"taxRate":1.00,
				"totalAmount":1287.13,
				"typeCode":null,
				"unitRate":1.00,
				"updatePurPrice":null,
				"updateStock":null,
				"updatedTime":null,
				"updatedUser":null
			}
		],
		"supplierId":0,
		"supplierName":"供应商A",
		"typeCode":"DG_IN_STORAGE_1"
	},
	"message":"请求成功",
	"success":true
}
*/

import java.util.*;
import com.alibaba.fastjson.*;


String response_data = prev.getResponseDataAsString();
JSONObject data_obj = JSONObject.parseObject(response_data);
//返回response中获取数组storageGoodsList
JSONArray dataArray = data_obj.get("data").getJSONArray("storageGoodsList");
 

//数组转成JSONObject。第一组
JSONObject storageGoodsList=(JSONObject)dataArray.getJSONObject(0);
//log.info("打印====================================="+storageGoodsList);
//替换JSONObject中的isShow为true
storageGoodsList.put("isShow", true);
//log.info("打印====================================="+storageGoodsList);
//JSONObject再换成字符串
String str = JSON.toJSONString(storageGoodsList);
String goodsList = "["+str+"]";
log.info("打印====================================="+goodsList);


vars.put("goodsList",goodsList);

 

jmeter beanshell 求范围内随机数

方式1、

//取一定范围内的随机数,前闭后开
public static int getRandNo(int min,int max){
Random random = new Random();
int randNo = random.nextInt(max)%(max-min+1) + min;
return randNo;
}

方式2、

//取一定范围内的随机数,前闭后闭

public static int getRandNo(int min,int max){        
        Random random = new Random();
        int randNo = random.nextInt(max-min+1) + min;
        return randNo;
}

 

 

 

 

 

 

 

 

有两个json包,大同小异。请自己选择。一个是阿里巴巴的json包,一个是普通的json包。

链接:https://pan.baidu.com/s/1qnH1Nf0zFnDRtFb8gw0uag 
提取码:txym 
复制这段内容后打开百度网盘手机App,操作更方便哦

 

jmeter beanshell 之常用的代码_第1张图片

你可能感兴趣的:(jmeter,beanshell)