JMeter commonly used built-in objects: vars, ctx, prev

In the previous article Beanshell Sampler and Beanshell Assertion , the use of JMeter beanshell was initially explained. Next, the commonly used built-in objects in JMeter beanshell and their use were collected and organized.

Note: The example uses JMeter version 5.1

1. vars

As the API documentation says, this is the class that defines variables that are properties local to a single thread. It sounds a bit difficult to understand. In fact, we only need to know that a series of methods of this class are basically for obtaining and assigning values ​​​​to variables, and this value obtaining and assignment behavior only takes effect on the variables of the current thread, not all threads.

Among all methods, there are two methods that deserve special attention. If you are interested in other methods, you can read the documentation for further understanding. These two methods are get() and put().

String get(String key) method, as the name suggests, is a value method. Its elements are as follows:

  • Parameter: String key, which is a key in string format.
  • Return value: The value corresponding to the parameter key, provided that this key-value pair exists, otherwise null will be returned.
  • Function: Get the corresponding value from the memory through key

The void put(String key, String value) method is an assignment method, and its elements are as follows:

  • Parameters: String key, which is a key in string format, String value, which is a value in string format.
  • Return value: void, that is, no return value
  • Function: Store a pair of key and value in memory in key-value pair format

Let’s use an example to experience these two methods. First, establish the script structure as follows:

Insert image description here

 The function of [user-defined variable] is to store a variable in the memory. The parameter name is "name" and the parameter value is "mu". It is a key-value pair like { "name" : "mu"} (only Example, JMeter does not store variables this way)

Next, write the code in [BeanShell Sampler]:

Insert image description here

  • vars.get(“name”): The variable name and its corresponding value have been stored in the previous [User-defined variable], and here we can get its value.
  • vars.put(“nameNew”,name + “gz”): After concatenating the value of name with “gz”, assign it to the variable nameNew and store it in memory.

Run the script and view the result tree:

Insert image description here

 

2. ctx and prev

For more information see: API documentation

This built-in object class provides many methods, but one method commonly used in interface testing is SampleResult getPreviousResult(), whose elements are as follows:

  • Parameters: none
  • Return value: SampleResult, the sampler result
  • Function: Get the result of the previous sampler

The SampleResult class [ API documentation ] provides many methods to provide various elements of the sampler result, such as:

  • String getRequestHeaders(): Get the request headers of the sampler results
  • String getResponseCode(): Get the response code of the sampler result
  • String getResponseHeaders(): Get the response headers of the sampler results
  • String getResponseDataAsString(): Get the response data of the sampler result

For other methods, please consult the API documentation

Here is an example to demonstrate. First create the following script:Insert image description here

Write the following statement in [beanShell sampler]:

import org.apache.jmeter.samplers.SampleResult;

// 获取取样器结果
SampleResult result = ctx.getPreviousResult();
// 获取请求头
String RequestHeaders = result.getRequestHeaders();
// 获取响应头
String ResponseHeaders = result.getResponseHeaders();
// 获取响应码
String responseCode = result.getResponseCode();
// 获取响应数据
String responseData = result.getResponseDataAsString();
// 获取url
URL url = result.getURL();
//
//log.info(RequestHeaders);
//log.info(ResponseHeaders);
log.info(responseData);

 Run the script and you can see the corresponding log output in the console:

Insert image description here

JMeter also provides a built-in object  prev . This built-in object can directly use the method in the SampleResult class to modify the [beanShell sampler] statement in the above example to the following:

// 获取请求头
String RequestHeaders = prev.getRequestHeaders();
// 获取响应头
String ResponseHeaders = prev.getResponseHeaders();
// 获取响应码
String responseCode = prev.getResponseCode();
// 获取响应数据
String responseData = prev.getResponseDataAsString();

log.info(RequestHeaders);
log.info(ResponseHeaders);
log.info(responseData);

 After running it, I found that the effect was the same.

Guess you like

Origin blog.csdn.net/lovedingd/article/details/131981472