作者:季沐测试笔记

原文地址https://www.cnblogs.com/testero/p/15436864.html

博客主页https://www.cnblogs.com/testero

1 BeanShell 断言之实例介绍

1.1 假设当前有个判断该用户是否为新用户的接口,返回的数据格式为以下内容:

{
"success": true,
"code": 0,
"msg": "success",
"data": {
"newUser": true,
"invitationCode": null
},
"total": 0
}
  • 当用户为新用户时,newUser的值为true

  • 当用户已是老用户时,newUser的值为false

1.2 假设有两为用户:15293561001为老用户,15293561002为新用户,现需要对改接口返回的数据进行验证,看是否达到预期效果,在Jmeter断言中选择BeanShell 断言

BeanShell中的脚本内容的验证就是根据用户判断newUser的值是否正确,这里会用到BeanShell断言的一些语法,只需要判断下后定义下断言失败或是成功即可,断言内容为以下内容

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON; prev.setDataEncoding("UTF-8"); String result = prev.getResponseDataAsString(); String result = prev.getResponseDataAsString(); System.out.println("------------------------------------------------------"); JSONObject response = JSON.parseObject(result); System.out.println("response为"+response); JSONObject data = (JSONObject) response.get("data"); System.out.println("data为"+data); System.out.println("mobile为"+"${mobile}"); System.out.println("newUser为"+data.get("newUser")); if ( "${mobile}" == "15293561001") {
if (data.get("newUser").toString().equals("false")){
Failure = false;
FailureMessage = "断言成功";
}else {
Failure = true;
FailureMessage = "断言失败,newUser为"+data.get("newUser");
}
}else if ("${mobile}" == "15293561002"){
if (data.get("newUser").toString().equals("true")){
Failure = false;
FailureMessage = "断言成功";
}else {
Failure = true;
FailureMessage = "断言失败,newUser为"+data.get("newUser");
}
}

断言中15293561001判断的newUser为false,15293561002判断的newUser为true,该断言应该完全通过,循环请求该接口,察看结果树

两条数据都是断言通过

下面我们将15293561002的newUser断言处改为false,执行效果如下

可以看到显示出了之前定义的断言失败的信息和接口返回的该字段的实际结果。

2 断言语法介绍

示例:

if (data.get("newUser").toString().equals("true")){
Failure = false;
FailureMessage = "断言成功";
}else {
Failure = true;
FailureMessage = "断言失败,newUser为"+data.get("newUser");
}

以上断言信息中Failrue用来标识断言失败,Failure = false;时表示断言成功

FailureMessage标识断言失败的原因

Jmeter之BeanShell 断言的更多相关文章

  1. 记录jmeter使用beanshell断言获取复杂的json字符串参数值

    实战示例 测试场景 电商系统经常会涉及到商品的库存数量的压测,在用户下单前需要先做库存余量的判断,当余量不足时用户无法下单,保证商品的有效售卖 库存余量查询响应结果 响应结果一般是json字符串的形式 ...

  2. JMeter中BeanShell断言使用一

    Jmeter Ant Task如何让beanshell断言失败的详细信息展示在report里面 首先必须给beanshell断言添加FailureMessage if(${TotalClient_SS ...

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

    在做接口测试时,对响应数据的校验是非常重要的部分:在使用Jmeter进行接口测试时,有多种respone校验方式,比如响应断言.BeanShell断言等等,BeanShell断言可以自定义断言,自由灵 ...

  4. jmeter使用BeanShell断言

    1. 首先存储一个接口的响应结果,如在http请求的BeanShell PostProcessor: import java.io.UnsupportedEncodingException; Syst ...

  5. Jmeter之BeanShell断言使用

    1.Bean Shell常用内置变量 JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下: log:用来记录日志文件,写入到j ...

  6. 『动善时』JMeter基础 — 31、JMeter中BeanShell断言详解

    目录 1.BeanShell简介 2.Beanshell的内置变量和方法 3.BeanShell断言界面详解 4.BeanShell断言的使用 (1)测试计划内包含的元件 (2)登陆接口请求界面内容 ...

  7. jmeter之beanshell断言实例

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

  8. jmeter中beanshell断言的使用

    简单使用beanshell的内容,进行测试内容的判断 这里通过断言内容,修改if的条件,达到发送警报邮件的功能 beanshell 代码如下:     SampleResult 等效于 prev lo ...

  9. JMeter之BeanShell断言---获取时间戳

    1.创建线程组,创建一个BeanShell Sampler,在其中编写BeanShell脚本. 2.在Jmeter中,可以利用${__time(,)}时间戳函数来获取十位的时间戳,如: vars.pu ...

随机推荐

  1. 前后端数据交互(六)——ajax 、fetch 和 axios 优缺点及比较

    一.ajax.fetch 和 axios 简介 1.1.ajax ajax是最早出现发送后端请求的技术,属于原生 js .ajax使用源码,请点击<原生 ajax 请求详解>查看.一般使用 ...

  2. 《通过刷leetcode学习Go语言》之(1):序言

    Author       : Email         : vip_13031075266@163.com Date          : 2021.03.07 Version     : 北京 C ...

  3. linux清空文件

    https://www.cnblogs.com/mrwang1101/p/6166326.html

  4. jquery实现强制刷新

    $('iframe.active').attr('src', $('iframe.active').attr('src'));

  5. Java字符串常量池及字符串判等解析

    一.理解"=="的含义 "=="常用于两个对象的判等操作,在Java中,"=="主要有以下两种用法: 1.基础数据类型:比较的是他们的值是否 ...

  6. AWVS13批量添加目标脚本

    # -*-coding:utf-8-*- # @Author:malphite.tang import json import requests from queue import Queue req ...

  7. (9)java Spring Cloud+Spring boot+mybatis企业快速开发架构之SpringCloud-搭建Eureka服务注册中心

    ​ 首先创建一个 Maven 项目,取名为 eureka-server,在 pom.xml 中配置 Eureka 的依赖信息,代码如下所示. <!-- Spring Boot --> &l ...

  8. 使用Java api对HBase 2.4.5进行增删改查

    1.运行hbase 2.新建maven项目 2.将hbase-site.xml放在项目的resources文件夹下 3.修改pom.xml文件,引入hbase相关资源 <repositories ...

  9. 学习laravel总结中...

    1,安装homestead :mac环境 https://learnku.com/docs/laravel-development-environment/5.5/development-enviro ...

  10. git 操作 :从远程仓库gitLab上拉取指定分支到本地仓库;git如何利用分支进行多人开发 ;多人合作代码提交实践

    例如:将gitLab 上的dev分支拉取到本地 git checkout -b dev origin/dev 在本地创建分支dev并切换到该分支 git pull origin dev 就可以把git ...