JMeter:在beanshell后处理器中使用正则表达式提取器值在另一个线程中使用它提取值 [英] JMeter: Using a regex extractor value in beanshell post processor to extract a value using it in another threads

查看:1111
本文介绍了JMeter:在beanshell后处理器中使用正则表达式提取器值在另一个线程中使用它提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法设置了一个脚本来从令牌中提取值,并将其全局地用于其他线程,但是不幸的是,默认情况下,我的脚本正在将值提取为1.

I managed to set a script to extract value from token and using it globally for other thread but unfortunately, my script is extracting value as 1 by default.

BeanShell预处理器

正则表达式提取器+打印的日志

我的期望是打印从令牌请求中提取的值.

My expectation is to print the value extracted from the token request .

推荐答案

很难回答为什么您的脚本总是提取值1,直到我们知道返回的响应是什么,以及您的Regex Extractor在此响应中的表现如何.另外,最好在新版本的JMeter中使用Groovy代替BeanShell.

It is difficult to answer why your script always extracts the value 1 till we know what is the response returned and how your Regex Extractor is behaving on this response. Also it is better to use Groovy in newer versions of JMeter instead of BeanShell.

作为一个帮助您的示例,我在下面为我的一个旧项目发布了Regex ExtractorBeanshell Preprocessor的用法,该项目从上一个请求的响应中提取一个cookie & CSRF值并传递到下一个请求.

As an example to help you, I have posted below the usage of Regex Extractor and Beanshell Preprocessor for one of my old projects which extracts a cookie & CSRF values from the response of the previous request and passes on to the next request.

HTTP响应:

{"responseMessage":{"messageObjects":[{"Authorization":"Bearer eyJhbGcxxxxxxxxxx.hsagdshvvdcwfdhwegdwdvbsqmshljkdhlqwkvnAJHFDBVGHWFHJDWBMNEHDJBSMNWBWJLHDKLJWHKJHDHWFVWDJGJKEWWBDNXVWGHDFWJHXKLBSNXVWFXWJEHXKWBXWDVXHGHWCDGXWDXCB0YXNrdcbwdgwegvcbwdvxbscxwfdvcwehgdwebwbvdcwdgckkhecdADGGHWFCVWBDVCGWCWVXNBSVXKWHCWVCBWCJWFGHECWECwgdhwvcwehhebdmnwwjhgnwvdcnbvwgvcwrehcwefnwecwghkevwe.abcdefghijklmnopqrst-poushfgabjwgjhwdvvttrudyqgd-CSGQGBkjgdjwhged","csrfToken":"adganm7ik39i6oclabce54154","ConnectedIP":"127.0.0.1","lastLoginIp":"null"}],"messageString":""},"responseStatus":200}

正则表达式提取器(使用的变量名称为"authcsrfkey")

Regular Expression Extractor (Name of the variable used is "authcsrfkey")

{"responseMessage":{"messageObjects":\[{"Authorization":(.+?),"csrfToken":"(.+?)"

BeanShell PostProcessor (因为我需要将两个提取的值连接在一起.该后处理器紧跟在Regex Extractor之后)

BeanShell PostProcessor (as I need to join the two extracted values. This postprocessor is just after the Regex Extractor)

String authkey=vars.get("authcsrfkey_g1");
String csrf=vars.get("authcsrfkey_g2");
String cookie="Authorization=" + authkey + "; csrfToken=" + csrf;
vars.put("cookie",cookie);

BeanShell PreProcessor (要添加到后续请求的标头中)

BeanShell PreProcessor (to be added in the header of the subsequent request)

import org.apache.jmeter.protocol.http.control.Header;
sampler.getHeaderManager().add(new Header("Cookie",vars.get("cookie")));

这篇关于JMeter:在beanshell后处理器中使用正则表达式提取器值在另一个线程中使用它提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆