1
votes

There may be mistranslations because i'm using a translator.

i want to create response assertion in beanshell

script content is postrequest site and get responsedata

but this request usually fail so i want to check everytime with response assertion

i want to check make response assertion in jmeter beanshell

Whether the request is successful or not, I want to create a response assertion in beanshell.

can i help?

for(int i=0; i<10; i++){
HttpClient client = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(requestURL);

postRequest.setEntity(new StringEntity(jsonMessage));
HttpResponse response = client.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String inputLine;
StringBuffer sbRes = new StringBuffer();
    while((inputLine = reader.readLine()) != null) {
        sbRes.append(inputLine);
                }
        reader.close();

String trans = sbRes.toString();
Boolean checkResult = trans.contains("result\":0");
if(checkResult==true){
         //** i want create response assertion here **
}
else{
        // **i want create response assertion here  **
}
}
1

1 Answers

0
votes

Looking into your code you're just sending 10 consecutive HTTP requests so it can be easily substituted with normal HTTP Request sampler and Response Assertion, there is no need to go for scripting there

Whatever:

  1. Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting.

  2. You have SampleResult pre-defined variable which can be used for controlling:

    enter image description here

Check out Top 8 JMeter Java Classes You Should Be Using with Groovy to learn more about above and other JMeter API shorthands available for JSR223 Test Elements