用户定义的变量(User_Defined_Variables)机制:
Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.
Once the Test Plan and all UDVs have been processed, the resulting set of variables is copied to each thread to provide the initial set of variables.
If a runtime element such as a User Parameters Pre-Processor or Regular Expression Extractor defines a variable with the same name as one of the UDV variables, then this will replace the initial value, and all other test elements in the thread will see the updated value.不管该配置元件放在哪里,都会在一开始就进行处理,所有的UDV都处理完成后,结果变量集就被复制到每个线程组中,以提供初始变量集。
如果运行时元素(如用户参数预处理器或正则表达式提取器)定义了一个与UDV变量之一同名的变量,那么它将替换初始值,线程组中的所有其他测试元素将看到更新后的值(仅限该线程组)所以,即使将UDV放在最外层,UDV设置的变量也是属于局部变量,不属于全局变量,只不过在最开始将UDV的变量集复制了一份初始值到每个线程组中,属于每个线程组的局部变量
例子:患者登录,获取token,使用__setProperty设置为全局变量,供其他线程组调用
1、获取 usertoken
2、设置全局变量 usertoken
${__setProperty(usertoken,${usertoken},)}
3、其他线程组使用 usertoken参数
方法一:直接使用 ${__P(usertoken)} 调用
执行结果
方法二:添加全局的配置元件:用户定义的变量
变量的值填写 ${__P(usertoken)} ,使用时调用变量名即可
执行结果
注意事项
使用该方法,需要先禁用其他线程组,单独执行前置(获取token)线程组,然后禁用前置线程组,执行需要压测的线程组
方法三:BeanShell_PreProcessor (BeanShell 预处理程序)
已知:在最外层有设置UDV变量usertoken,该变量属于当前线程组的局部变量
在非前置线程组中,使用BeanShell 前置处理器,将全局变量usertoken保存到局部变量usertoken中
var s = props.get("usertoken"); //获取全局变量 usertoken
log.info(s);
vars.put("usertoken", s); //把全局变量保存到UDV局部变量usertoken中