美文网首页我爱编程
JMETER接口测试

JMETER接口测试

作者: 远航天下 | 来源:发表于2018-05-22 15:07 被阅读0次

    一、jmeter接口测试原理

    1、HTTP请求常用的post、get请求,包括请求头信息参数、请求体参数、返回信息参数,及jmeter断言处理等 

    二、Jmeter处理机制

    1、请求头及请求体进行处理

        ----通过前置处理器提供的方法进行处理,常用的有:用户参数、BeanShell PostProcessor

        ----通过用户参数的方法对请求体和头信息参数进行处理

        ----通过BeanShell PostProcessor

    代码如下:

    import com.zcsmart.aes.en.AESModule;

    import com.zcsmart.aes.IAES;

    import com.zcsmart.aes.impl.AES192Impl;

    import com.zcsmart.aes.impl.AES256Impl;

    importorg.apache.commons.codec.binary.Base64;

    import com.zcsmart.aes.SHA256Util;

    public String test(){

        log.info("---------start---------");

        String key = "zcsmart.key";

         String iv = "zcsmart.iv";

         IAES iaes = new AES192Impl();

         String plain = "{\"cmd\":\"auth-token\"}";

         String result = iaes.encDataBase64(plain, AESModule.AES_192_CBC_PKCS7, key, iv);

         byte[] sha256Byte = SHA256Util.getServerHash_C(result);

         String sha256_str = Base64.encodeBase64String(sha256Byte);

        String body = "{\"encData\":\""+result+"\"}";

        log.info("body体的hash_body值(): " + sha256_str);

        String auth = "{\"companyid\":\"1104\",\"clientid\":\"1\",\"timestamp\":\"20180503000000\",\"hashbody\":\"" + sha256_str + "\"}";

         String signData = iaes.signData(auth);

        log.info("=== REQUEST body :" + body);

        log.info("=== REQUEST auth :" + auth);

        log.info("=== REQUEST sign :" + signData);

        vars.put("body",body);

        vars.put("auth",auth);

        vars.put("sign",signData);

        return "ok";

    }

    test();

    2、返回信息处理

    ----通过后置处理器对返回结果进行处理

    ----常用的JSON Extractor把返回结果拿到,定义变量方便后面接口使用

    ----使用BeanShell PostProcessor进行结果处理

    代码如下

    import com.alibaba.fastjson.JSON;

    import java.util.Map;

    import java.lang.String;

    import com.jmeter.functions.sign.DataSign;

    importorg.apache.jmeter.engine.util.CompoundVariable;

    import org.apache.jmeter.functions.InvalidVariableException;

    import java.util.ArrayList;

    import java.util.Collection;

    public static String test(){

        String item = prev.getResponseDataAsString();

        log.info("item=============="+item);

        Map map = JSON.parseObject(item, Map.class);

        String encData = map.get("encData").toString();

         DataSign dataSign = new DataSign();

         Collection params = new ArrayList();

         params.add(new CompoundVariable("C:\\jmetet_project\\test\\tsm_bjtbiz.pack"));

         params.add(new CompoundVariable("4"));

         params.add(new CompoundVariable("tsm.csc.so"));

         params.add(new CompoundVariable("192"));

         params.add(new CompoundVariable("111"));

         params.add(new CompoundVariable(encData));

         dataSign.setParameters(params);

         String result = dataSign.execute();

         log.info("result: "+result);

         prev.setResponseData(result,"UTF-8");

         return result;

    }

    test();

    3、断言

        ----jmeter断言有很多种,常用的有响应断言、Bean shell断言

        ----响应断言

        ----Bean shell断言

    代码如下:

    String jsonString =  vars.get("uid");

    String id =  vars.get("autoId");

    log.info ("The example answer is " + jsonString);    

        if (    jsonString != "1"){

            AssertionResult.setFailureMessage("The User Id is wrong");

                AssertionResult.setFailure(true);

        }

        if (    id != "1"){

            AssertionResult.setFailureMessage("The id is wrong");

                AssertionResult.setFailure(true);

        }

    4、参数化

    ----用户自定义参数来进行参数化

    ----通过前置处理器的BeanShell定义变量

    代码如下:

    //登录获取userId

    String userId=props.get("userId");

    vars.put("userId",userId);

    //登录获取token

    String token=props.get("token");

    vars.put("token",token);

    //注册获取grantToken

    String grantToken=props.get("grantToken");

    vars.put("grantToken",grantToken);

    ----通过数据库进行参数化

    相关文章

      网友评论

        本文标题:JMETER接口测试

        本文链接:https://www.haomeiwen.com/subject/vjnhjftx.html