Jmeter Getting started with Web performance testing (vi): Jmeter parsing response and passing value

Source: Internet
Author: User

Parsing the contents of the response and passing the obtained value to the subsequent request, the common method is to add the post processor on the request that wants to parse response
This chapter describes two common components

    • BeanShell postprocessor
    • JSON Extractor
Add Post Processor: BeanShell postprocessor
    • Gets the string in the response and determines the content, when response contains "the wait operation timed out" or "Oops." Something went wrong ... sorry, the response of the request is considered incorrect
      String response = prev.getresponsedataasstring ();
      String code = Prev.getresponsecode ();
      Log.info ("Respnse is" + response);
      Log.info (code);
      int result1 = Response.indexof ("The wait operation timed out");
      int result2 = Response.indexof ("Oops. Something went wrong ... sorry ");
      if (Code = = "$" && result1>=0 | | result2>=0) {
      Failuremessage = "Ok,check current";
      }
      else{
      Failure = true;
      Failuremessage = "Error,check ERROR";
      }

    • Parse the returned Jason data, get the value of the Name field assigned to the variable result

{"Body": {"apps": [{"Name": "111"},{"name": "222"}]}}

Import org.json.*;
The imported JSON package in the script needs to be downloaded to the network and put under \lib\ext

String response_data = prev.getresponsedataasstring ();
Jsonobject data_obj = new Jsonobject (response_data);
String apps_str = Data_obj.get ("Body"). Get ("apps"). ToString ();
Jsonarray Apps_array = new Jsonarray (APPS_STR);
String[] result = new string[apps_array.length ()];
for (int i=0;i<apps_array.length (); i++) {
Jsonobject app_obj = new Jsonobject (Apps_array.get (i). toString ());
String name = App_obj.get ("name"). ToString ();
Result[i] = name;
}
Vars.put ("Result", arrays.tostring (result));

Add Post Processor: JSON Extractor

Parse the returned Jason data, get the value of the Name field into the parameter to the request in the back

{"Body": {"apps": [{"Name": "111"},{"name": "222"}]}}

The value of the variable T1 is 111, and the value of the variable t2 is 222

Jmeter Getting started with Web performance testing (vi): Jmeter parsing response and passing value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.