• jmeterJSR223取样器和断言


    一、新建:JSR223 Sampler

      二、设置请求代码

    import org.apache.http.HttpResponse
    import org.apache.http.client.methods.HttpEntityEnclosingRequestBase
    import org.apache.http.entity.StringEntity
    import org.apache.http.impl.client.CloseableHttpClient
    import org.apache.http.impl.client.HttpClientBuilder
    import org.apache.http.util.EntityUtils
    
    
    public  class HttpGetWithBody extends HttpEntityEnclosingRequestBase {
        public final static String METHOD_NAME = "GET";
        
        @Override
        public String getMethod() {
            return METHOD_NAME;
        }
    }
    
    def client = HttpClientBuilder.create().build();
    def getRequest = new HttpGetWithBody();
    //请求地址
    getRequest.setURI(new URL("${url}${path}").toURI());
    //body
    def json ="{\"type\": \"1\", \"id\": \"123\", \"fields\": [\"TEST01\",\"TEST02\"]}\n";
    def body = new StringEntity(json, "application/json", "UTF-8");
    getRequest.addHeader("Content-Type", "application/json");
    getRequest.setEntity(body);
    log.info(json);
    def response = client.execute(getRequest);
    def result = EntityUtils.toString(response.getEntity());
    //返回结果
    vars.put("result",result)
    log.info(result,"日志");

     三、新增:JSR223 断言

     四、设置断言代码

    String resultRT = vars.get("result");
    System.out.println(resultRT);
    
    if (resultRT.contains("\"code\":\"0\"")){
        SampleResult.setSuccessful(true);
    }else{
        SampleResult.setSuccessful(false);
    }
     
    五、测试结果
  • 相关阅读:
    EntityFramework 启用迁移 EnableMigrations 报异常 "No context type was found in the assembly"
    JAVA 访问FTP服务器示例(2)
    NuGet Package Manager 更新错误解决办法
    JAVA 访问FTP服务器示例(1)
    RemoteAttribute 的使用问题
    诡异的 javascript 变量
    javascript apply用法
    Babun 中文乱码
    GSM呼叫过程
    转站博客园
  • 原文地址:https://www.cnblogs.com/biyuting/p/16366801.html
Copyright © 2020-2023  润新知