Jmeter 正则表达式

Jmeter Regular Expression

我正在努力寻找 Jmtere 动态输入值的解决方案,其中 return 一个匹配值但带有子字符串。

</script></span><input type="hidden" name="j_id_s_1_SUBMIT" value="1" /><input type="hidden" name="javax.faces.ViewState" id="j_id__v_0:javax.faces.ViewState:1" value="07JgPgLP3e1jdtHd6s1UxDoKEaCU362MYbfU48r9paGrBDfo" autocomplete="off" /></form>

我尝试获取“07JgPgLP3e1jdtHd6s1UxDoKEaCU362MYbfU48r9paGrBDfo”这个值。我重复表达:

j_id_s_1_SUBMIT" value="(.+)"

参考名称:视图

它returns:

Match count: 1
Match[1][0]=j_id_s_1_SUBMIT" value="1" /><input type="hidden" name="javax.faces.ViewState" id="j_id__v_0:javax.faces.ViewState:1" value="07JgPgLP3e1jdtHd6s1UxDoKEaCU362MYbfU48r9paGrBDfo" autocomplete="off"
Match[1][1]=1" /><input type="hidden" name="javax.faces.ViewState" id="j_id__v_0:javax.faces.ViewState:1" value="07JgPgLP3e1jdtHd6s1UxDoKEaCU362MYbfU48r9paGrBDfo" autocomplete="off

我试过 ${__substring(${view}, 101, 149)} ,我的脚本仍然失败。还有其他解决方案吗? 谢谢

name="j_id_s_1_SUBMIT"\svalue="(.+?)"\s/>

如果你不告诉它在第一次匹配后停止(即 ?.+ 之后),它将尽可能地进行广泛匹配 - 这就是发生的事情。

PS 但那是在你的表达之后,而通过它你会得到 1,而不是你需要的那个标记。

所以,要得到你的东西,你最好

id="j_id__v_0:javax\.faces\.ViewState:1"\svalue="(.+?)"\sautocomplete

PPS 你的 html 似乎格式正确,那为什么不使用 XPath 提取器呢?那么在你的回复中你就不会依赖 and/or 的属性顺序 HTML 的整体结构。

PPPS 官方 JMeter 文档提供了更好的技巧:

Note: although the above expression works, it's more efficient to use the following expression: name="file" value="([^"]+)"> where [^"] - means match anything except " In this case, the matching engine can stop looking as soon as it sees the first ", whereas in the previous case the engine has to check that it has found "> rather than say " >.

RTFM 仍然很流行,不是吗? :-)

只是 Don't use regular expressions to parse HTML,一个小的标记更改,添加或替换一个属性,在下一行渲染一部分标签,不管怎样,你的测试都会被毁掉。

您有:

  • XPath Extractor,相关的XPath查询会像//input[@name='javax.faces.ViewState']/@value
  • 一样简单
  • CSS/JQuery Extractor,选择将是input[name=javax.faces.ViewState],使用value作为属性