Jmeter-global variables are used across thread groups

Table of contents

I. Introduction  

2. Example demonstration

 Summarize:


I. Introduction  

  I talked about how to use regular expressions to extract values. Generally, the extracted values ​​are in the same thread and can be referenced by any request. So what if other thread groups also want to reference it? At this time, it involves a knowledge point about global variables. Without further ado, let’s start with examples.

1. BeanShell creates global variables (cross-thread groups): you can use Beanshell sampler or Beanshell post-processor

1) Create a local variable (within the thread group): token

2) Use Beanshell to call the created local variable token and name the global variable newtoken, example ${__setProperty(newtoken,${token},)}

3) Call global variables across thread groups

Method 1: ${__P(newtoken)}; //Call global variables

Method 2: ${__property(newtoken)}; //You can also call global variables like this, which is equivalent to the above writing method

Method 3: Beanshell pre-processor - take newtoken and assign it to token1, which can then be referenced by ${token1}

String a1=props.get("newtoken");

vars.put("token1",a1)

2. Example demonstration

1. Under the [Unified Login] request of the [Login] thread group, right-click > Add > Post-Processor > Regular Expression, and use regular expressions to extract the token in the response result after the unified login is successful. The reference name is "token".

The expression rule is written as: "token":"(.*?)"

 2. In the [Login] thread group, right-click > Add > Sampler > BeanShell Sampler, and set the extracted token as the global variable newtoken.

Script is written as: ${__setProperty(newtoken,${token},)}

3. In the [xx system] thread group, right-click > Add > Preprocessor > BeanShell PreProcessor, take the global variable newtoken and assign it to token

Script is written as:

String a1=props.get("newtoken");
vars.put("token",a1)

4. Okay, the above is finally finished. You can safely reference the token in the [xx system] thread group, as follows:

5. The final operation and debugging is as follows: (Because it involves information security issues, the following picture is roughly taken)

1) Use Debug Sampler to see the extracted token

2) [xx system] thread group can normally reference the token of [login] thread group, as shown in the figure

3) The unified login successfully jumps to [xx system], and the login is successful, as shown in the figure:

Note: Some people may be curious as to why there are several more sub-requests under this request. Haha, this involves the knowledge of redirection. You can download it on Baidu yourself.

Add a URL assertion:

 Summarize:

Thank you to everyone who reads my article carefully! ! !

I have personally compiled some technical materials that I have compiled in my software testing career over the past few years, including: e-books, resume modules, various work templates, interview guides, self-study projects, etc. Everyone is welcome to click on the business card below to get it for free, don’t miss it.

   Python automated testing learning exchange group: a full set of automated testing, interview, resume and learning materials. Click the link to join the group chat [Python automated testing exchange]: http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=DhOSZDNS -qzT5QKbFQMsfJ7DsrFfKpOF&authKey=eBt%2BF%2FBK81lVLcsLKaFqnvDAVA8IdNsGC7J0YV73w8V%2FJpdbby66r7vJ1rsPIifg&noverify=0&group_code=198408628

Guess you like

Origin blog.csdn.net/MXB_1220/article/details/131840359