jmeter-JSR223取样器和断言

一、新建: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);
}

 

 
五、测试结果

 

 

posted @ 2022-06-11 21:01  避雨亭  阅读(966)  评论(0编辑  收藏  举报