JMeter:正则表达式提取器未提取令牌 [英] JMeter: Regex Extractor not Pulling Token

查看:236
本文介绍了JMeter:正则表达式提取器未提取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从网页中提取CSRF令牌,然后通过BeanShell进行记录.由于我在

I need to extract a CSRF token from a webpage, then log it via BeanShell. The latter part is working thanks to the help I received in this thread, but now I need to figure out how to get ${token} to populate with the right data.

注意:我知道正则表达式提取器不是首选方法,但是在这种情况下,我必须保持练习的参数范围之内.

Note: I know the Regular Expression Extractor is not the preferred method, but I have to stay within the parameter of the exercise, in this case.

首先,我设置了一个HTTP请求以针对www.blazedemo.com/register执行GET.

First, I have a HTTP Request set to perform a GET against www.blazedemo.com/register.

第二,我检查了响应树中显示的响应数据以找到CSRF令牌:

Second, I checked the response data shown in the response tree to find the CSRF token:

<!-- CSRF Token -->
<meta name="csrf-token" content="4ZCKKqQgwJH5lT5dQSeAwgeyOr7plAe7IOVRGmQm">

我有一个Regex Extractor设置来抓取它:

I have a Regex Extractor setup to grab it:

万一失败了,我默认设置为"NOT_FOUND".

In case it fails to do so, I have default set as "NOT_FOUND".

最后,我有一个后处理器记录给$ {token}的任何值.

Finally, I have a post processor logging whatever value is given to ${token}.

我在日志中找到以下内容:

I find the following in my log:

2017-10-31 15:12:31,975 INFO o.a.j.u.BeanShellTestElement:令牌 是:NOT_FOUND

2017-10-31 15:12:31,975 INFO o.a.j.u.BeanShellTestElement: The token is: NOT_FOUND

推荐答案

请记住,不建议使用正则表达式来解析HTML ,我建议您使用 CSS/JQuery提取器代替.

Remember that it is not recommended to use regular expressions for parsing HTML, I would recommend going for CSS/JQuery Extractor instead.

  1. 将具有此CSRF令牌的CSS/JQuery Extractor添加为请求的子项
  2. 配置如下:

  1. Add CSS/JQuery Extractor as a child of the request which has this CSRF token
  2. Configure it as follows:

  • 参考名称:任何有意义的内容,即token
  • CSS/JQuery表达式:meta[name=csrf-token]
  • 属性:content

  • Reference Name: anything meaningful, i.e. token
  • CSS/JQuery Expression: meta[name=csrf-token]
  • Attribute: content

演示:

更多信息:如何在JMeter中使用CSS/JQuery提取器

如果您仍然想使用正则表达式-将要检查的字段"更改为Body,但是我不建议您这样做,因为在解析HTML响应时正则表达式的开发和/或支持非常麻烦.对任何标记更改非常敏感,即,如果属性顺序更改或属性转到新行,则将破坏您的测试.

If you still want to go for Regular Expressions - change "Field to check" to Body, however I wouldn't recommend this as when it comes to parsing HTML responses regular expressions are headache to develop and/or support and very sensitive to any markup change, i.e. if order of attributes changes or an attribute goes to a new line it will ruin your test.

这篇关于JMeter:正则表达式提取器未提取令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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