如何在JMeter中基于前一个Sampler响应值控制采样器执行?
如何在JMeter中根据响应属性值分支执行不同请求?
Hey there! I’ve tackled this exact conditional flow in JMeter before, so let’s break down the steps to make this work for you:
1. 先从响应里提取要判断的属性值
First up, you need to pull the target property from your first Sampler’s response. The JSON Extractor is perfect for this:
- Right-click your first Sampler → Add → Post Processors → JSON Extractor
- In the
JSON Path Expressionfield, enter the path to your target property. For example, if your response has an array property like"tags": [], use$.tags - Give it a
Reference Name(something memorable liketargetArrayProperty) so you can reference it later
2. 用If Controller实现分支判断
Next, add an If Controller to handle the "IF" case:
- Right-click your Test Plan/Thread Group → Add → Logic Controller → If Controller
- In the
Conditionbox, paste this JEXL3 expression to check if the extracted value is exactly[]:${__jexl3("${targetArrayProperty}" == "[]",)}- JEXL3 is great for precise string comparisons here—make sure the quoted
[]matches exactly what your response returns (no extra spaces!)
- JEXL3 is great for precise string comparisons here—make sure the quoted
3. 添加IF分支的请求
Drag the Sampler you want to run only when the property is [] into this If Controller. It’ll only execute when the condition is true.
4. 设置ELSE分支的请求
For the "ELSE" case, add another If Controller with the inverse condition:
- Create a second If Controller, and use this condition:
${__jexl3("${targetArrayProperty}" != "[]",)} - Drop your alternate Sampler into this second If Controller—it’ll run whenever the first condition fails.
Quick Notes to Avoid Headaches:
- Always verify your extracted variable works first! Use the View Results Tree listener to check if
${targetArrayProperty}is correctly pulling[](or the non-empty value) from the response - If your empty array might have whitespace (like
[ ]), adjust the condition to match that exact string - If you’re dealing with a property that could be
nullinstead of[], you’ll need to tweak the condition to account for that (e.g., combine with${__isBlank(${targetArrayProperty},)})
内容的提问来源于stack exchange,提问作者Siddish
相关产品推荐
相关产品推荐

