jmeter之beanshell断言实例

.首先储存一个接口的响应结果,比如在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

时间: 2024-08-01 21:57:34

jmeter之beanshell断言实例的相关文章

JMeter学习-007-JMeter 断言实例之一 - 响应断言

之前的文章中已经对如何录制 web 的请求进行了详细的描述,敬请参阅:JMeter学习-004-WEB脚本入门实战 同时,我们的手机应用(例如:京东.天猫.唯品会.携程.易迅 等等 App)所发出的请求,也可进行录制,只需要将手机连接的无线网络代理到本机(以 iPhone 为例,依次点击 [设置]/[无线局域网]/[连接网络信息,即网络右侧详细信息符号]/[HTTP代理]/手动],填写 服务器IP.端口 对应填写 JMeter 机器的 IP.JMeter HTTP 代理端口 即可),并将对应的端

JMeter使用BeanShell断言

BeanShell简介 BeanShell是使用Java语法的一套脚本语言,在JMeter的多种组件中都有BeanShell的身影,如: 定时器:BeanShell Timer 前置处理器:BeanShell PreProcessor 采样器:BeanShell Sampler 后置处理器:BeanShell PostProcessor 断言:BeanShell Assert 监听器:BeanShell Listener 通过BeanShell可以对请求数据.响应数据或环境变量进行更加灵活的处理

Jmeter之BeanShell断言使用

1.Bean Shell常用内置变量 JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下: log:用来记录日志文件,写入到jmeber.log文件,使用方法:log.info(“This is log info!”); ctx(JmeterContext)通过它来访问context,使用方法可参考:org.apache.jmeter.threads.JMeterContext. vars - (JMeterVariabl

jmeter之beanshell断言---数据处理

在做接口测试时,对响应数据的校验是非常重要的部分:在使用Jmeter进行接口测试时,有多种respone校验方式,比如响应断言.BeanShell断言等等,BeanShell断言可以自定义断言,自由灵活的用脚本实现断言. 1.什么是BeanShell ? 小型嵌入式Java源代码解释器,具有对象脚本语言特性,能够动态地执行标准JAVA语法 运行其内部的脚本处理Java应用程序,还可以在运行过程中动态执行你java应用程序执行java代码,因为BeanShell是用java写的,运行在同一个虚拟机

Jmeter的BeanShell断言

1.什么是BeanShell断言? BeanShell断言可以使用beanshell脚本来执行断言检查,可以用于更复杂的个性化需求,使用更灵活,功能更强大,但是要能够熟练使用beanshell脚本. 在这里除了可以使用beanshell的内置变量外,主要通过Failure和FailureMessage来设置断言结果. Failure = false;-----表示断言成功, FailureMessage = "……"; ----自定义的成功信息 Failure = true;-----

jmeter中beanshell断言的使用

简单使用beanshell的内容,进行测试内容的判断 这里通过断言内容,修改if的条件,达到发送警报邮件的功能 beanshell 代码如下:     SampleResult 等效于 prev log.error(vars.get("ReqStatus")); int count=Integer.parseInt(vars.get("errorcount").trim()); vars.put("result", SampleResult.ge

JMeter学习-015-JMeter 断言之-Bean Shell Assertion

前面的博文中有对 JMeter 中的 响应断言 进行了讲解并实例演示,详情敬请参阅博文:JMeter学习-007-JMeter 断言实例之一 - 响应断言. 在 JMeter 中总计提供了如下几种 BeanShell 组件: Beanshell Sampler:单独的采样器. Beanshell PreProcessor:针对其他采样器的前置处理器.可以在采样器执行之前进行预处理操作,生成相关测试数据供采样器使用,相当于 TestNG 中的 before annotation. Beanshel

Jmeter BeanShell断言

这篇文章用来记录编写beanshell断言遇到得问题. 问题1:JSONObject not found in namespace 流程:在beanshell下写代码如下: 1 String response_data = prev.getResponseDataAsString(); 2 JSONObject data_obj = new JSONObject(response_data); 3 4 String current_page = data_obj.get("data")

Jmeter接口测试使用beanshell断言json返回

一般情况下响应断言就能解决很多问题,但是返回复杂的json时就需要用到beanshell断言. 举个例子 免费的接口API www.sojson.com/api/beian/sojson.com host:    www.sojson.com source:   api/beian/sojson.com (api/beian后面只能跟一级域名如:baidu.com或者sojson.com) 创建好线程组HTTP请求,给该请求加beanshell断言,代码如下,其中必须加prev.setSucce