【问题标题】:How to capture changing "authenticity_token" at run time using Jmeter's "Regular Expression Extractor"?如何使用 Jmeter 的“正则表达式提取器”在运行时捕获变化的“authenticity_token”?
【发布时间】:2014-06-12 09:51:09
【问题描述】:

我曾尝试将input\s+name=”authenticity_token”\s+type=”hidden”\s+value=”(.*?)”\s*\ 放入Jmeter's Regular Expression Extractor,但这无济于事,并且测试失败。对于Template,我一直保留$1$

在查看页面源代码时,它是这样写的: <input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="OzzoQsvruAetQAiAMj5Mh4L730w0PUxzoALcgT3dI+o=" />
基于以上,我应该如何为Regualr Expression Extractor写内容

请看下图:

它的 Ruby on Rails 应用程序

【问题讨论】:

  • 有什么区别?但是我尝试使用",但仍然失败。
  • 第一个是右双引号。您可以在this demo 中看到这些字符的不同之处(注意右侧的捕获组)。
  • 第一个失败了,然后我把它改成了",然后又失败了。
  • 试试removing the trailing \s*\.,如果这不起作用..那么这是我不熟悉的JMeter的东西。
  • 这样的`input+name="authenticity_token"+type="hidden"+value="(.*?)"*\ `

标签: regex jmeter authenticity-token


【解决方案1】:

根据https://stackoverflow.com/a/1732454/2897748

您无法使用正则表达式解析 [X]HTML。因为正则表达式无法解析 HTML。正则表达式不是可用于正确解析 HTML 的工具。

我强烈建议使用以下方法之一:

  1. CSS/JQuery Extractor
  2. XPath Extractor

上面的示例配置以匹配您的隐藏输入值:

CSS/JQuery

  • 参考名称:token
  • CSS/JQuery 表达式:input[name=authenticity_token]
  • 属性:value

XPath

  • 使用 Tidy - 勾选(如果您的响应不符合 XML/XHTML)
  • 参考名称:token
  • XPath 查询://input[@name='authenticity_token']/@value

如果您仍然需要坚持使用正则表达式提取器,以下配置可能会有所帮助:

  • 参考名称:token
  • 正则表达式:<input name="authenticity_token" type="hidden" value="(.+?)" />
  • 模板:$1$

但它会非常敏感和脆弱,即在属性不同的顺序、多行布局等情况下。我建议考虑使用上述提取器。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多