jmeter之beanshell断言实例

匿名 (未验证) 提交于 2019-12-02 22:56:40

.首先储存一个接口的响应结果,比如在http请求的后面添加beanshell后置处理器(BeanShell PostProcessor)来储存http请求的响应结果:

 1 import org.json.*;  2   3 //获取上一个请求的返回值  4 String response = prev.getResponseDataAsString();  5 //将返回值转换为json  6 JSONObject responseJson = new JSONObject(response);  7 //获取responseMessage  8 String message = responseJson.getString("responseMessage");  9 log.info("message的值:" + message); 10  11 //使用vars.put()方法储存变量message 12 vars.put("message",message); 13 //获取titleLink 14 String titleLink = responseJson.getJSONObject("data").getString("titleLink"); 15 log.info("titleLink的值:" + titleLink); 16 //使用vars.put()方法储存变量message 17 vars.put("titleLink",titleLink);

在后面的其他接口中如何需要使用变量message和titleLink,可以使用${message}和${titleLink}来获取变量的值;

变量储存好后,在需要断言的接口后面添加BeanShell断言,使用Failrue来标识断言失败,FailureMessage标示断言失败的原因,如:

1 //使用vars.get()方法获取变量的值 2 String message= vars.get("message"); 3   4 if(!message.equals("success")) { 5     Failure = true;  6     FailureMessage = "规则解析失败"; 7 }else{ 8     FailureMessage = "规则解析成功"; 9     }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2.直接在需要断言的接口后面使用beanshell断言,使用Failrue来标识断言失败,FailureMessage标示断言失败的原因,如:

 1 import org.json.*;  2   3 //获取上一个请求的返回值  4 String response = prev.getResponseDataAsString();  5 //将返回值转换为json  6 JSONObject responseJson = new JSONObject(response);  7 //获取responseMessage  8 String message = responseJson.getString("responseMessage");  9 log.info("message的值:" + message); 10  11 if(!message.equals("success")){ 12     Failure = true;  13     FailureMessage = "规则解析失败,message不等于success"; 14     return; 15 } 16  17 //获取titleLink 18 Object titleLink = responseJson.getJSONObject("data").get("titleLink"); 19 log.info("titleLink的值:" + titleLink.toString()); 20  21  22 if(titleLink.toString().equals("null") || "".equals(titleLink)){ 23     Failure = true;  24     FailureMessage = "规则解析失败,titleLink为空"; 25 }else if(!titleLink.toString().startsWith("http") && !titleLink.toString().startsWith("https")){ 26     Failure = true; 27     FailureMessage = "规则解析失败,titleLink不为空,但是不是以http或者https开头的"; 28 }

接口的响应数据为:

 1 {  2 :   "responseCode":"1",  3 :   "responseMessage":"success",  4 :   "responseType":null,  5 :   "data":  6 :   {  7 :   :   "city":"上海",  8 :   :   "rentUnit":"月",  9 :   :   "source":"个人房源网", 10 :   :   "title":"徐盈路1188弄徐泾青浦徐泾租房", 11 :   :   "belonger":"个人", 12 :   :   "housingType":"סլ", 13 :   :   "floor":"高层", 14 :   :   "rentPrice":"17000", 15 :   :   "titleLink":"http://sh.grfy.net/rent/d-34612565.html", 16 :   :   "decoration":null, 17 :   :   "direction":null, 18 :   :   "isSplit":"否", 19 :   :   "imgs":null, 20 :   :   "publishTime":"2018-07-25T23:20:33.471", 21 :   :   "contactMobile":null, 22 :   :   "website":"http://sh.grfy.net/rent/list_2_0_0_0-0_0_0-0_0_2_0_{}_.html", 23 :   :   "address":"徐泾", 24 :   :   "contactName":"王女士", 25 :   :   "houseType":"4室2厅2卫", 26 :   :   "estate":"徐泾", 27 :   :   "roomArea":"177", 28 :   :   "collectHouseType":"סլ", 29 :   :   "collectType":"出租", 30 :   :   "district":"青浦", 31 :   :   "totalFloor":"共20层", 32 :   :   "region":"上海", 33 :   :   "isRegister":"否", 34 :   :   "desc":"仁恒西郊花园 4室2厅2卫 房屋亮点 新上 配套齐全 有阳台 首次出租 随时看房 出租要求 一家人 一年起租 租户稳定 作息正常 房源描述小区环境好,物业管理成熟,私人会所实施配套齐全,临近地铁17号徐盈站,周边多所国际学校,仁恒的房子品质有保障。无中介费。" 35 :   } 36 }

原文:https://www.cnblogs.com/lwjnicole/p/9383872.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!