Parameter substitution in Jmeter send request

        Because of the needs of the project, it is necessary to send an exposure request to the background service in the stress test script, and record how to replace a parameter in the request.

       First, let's introduce the digression. The exposure request is obtained from the result of the service request in the previous step, which requires adding Json Path Processor to the sending request. As a post-processor, Json Path Processor needs to download a plugin to use it. Download jmeter-plugins-json-2.3.jar and put it in Jmeter's lib/ext to restart jmeter.
        After that, configure JSON Path PostProcessor, fill in the parameter name in variable names, such as astr, this parameter will save the string parameters intercepted by the expression in Json path expressions according to the path, $ represents all the returned strings, this plugin will return the result to Jsonization, the corresponding exposure record can be obtained through $.ads[0].monitor[0].url.
       Then, add a regular expression extractor, and select the scope of the application to be limited to the Jmeter Variables set in the previous step, such as astr, and enter the regular extraction of str in the regular expression: .+\?(.*), where in parentheses The data will be used to assign bstr in the reference name, so that bstr is the value after regular extraction of asstr.
       As above, the parameters of the exposure request are stored in bstr, because there is a character such as ${loction} in this value in the project, so the second request cannot be sent in Jmeter. At this point a replacement is required. Replace it with a string like location=10. Next, we will focus on the replacement method.
       Add a preprocessor on the second request: BeanShell PreProcessor . The built-in script is as follows:
try {
  String surl1 = vars.get("bstr");
  if(surl1 != null){
  if(surl1 != null @and surl1.length() > 0) {
  String path_replaced = surl1.split("\\$")[0]+"10";
  String other = surl1.split("\\$")[1].split("\\}")[1];
  vars.put("NEW_PATH",path_replaced+other);
  }
  }
}
catch (Throwable ex) {
  log.error("Failed to do this or that", ex);
}
      In Beanshell, vars.get() can get the parameters set in the system, and vars.put() can add parameters and values. Need to pay attention to the use of split and try catch. Some samples return empty bstr, and only if else will report an error.
      After getting NEX_PATH, the path of exposure request can be set to /s?${NEW_PATH}, which will replace ${locaiton}.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326399895&siteId=291194637