You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Gatling中执行条件检查?解决${code}.equals(200)恒为假的问题

Fixing Gatling Condition Check: Why ${code}.equals(200) Always Returns False

Hey there! Let's figure out why your Gatling condition check isn't working as expected. The root cause here is almost certainly a type mismatch—let's break this down and get it fixed.

The Problem Explained

When you use ${code} in Gatling, you're pulling a value stored in the session as a String. But you're comparing it directly to the integer 200. Since equals() checks both value and type, a String like "200" will never equal the integer 200—hence the always-false result.

Solution 1: Convert the String to an Integer

The most reliable way is to cast the session value to an integer before comparing. You can do this in two ways:

Using a Session Lambda

For more control, use a session lambda to explicitly convert the value:

.doIf(session => session("code").as[Int] == 200) {
  // Your logic to run when the code is 200
  exec(http("Success Request")
    .get("/api/success-endpoint"))
}

Using Gatling's Expression Language (EL)

You can also use inline EL to convert the string directly in the condition:

.doIf("${code.toInt() == 200}") {
  // Your conditional logic here
}

Solution 2: Compare as Strings (If Appropriate)

If your API returns the code as a string (e.g., the JSON response has "code": "200"), you can instead compare to a string literal:

.doIf("${code.equals('200')}") {
  // Your logic here
}

Just make sure the value stored in code is actually a string matching "200"—this won't work if the original value was a numeric type.

Quick Check: Verify Your Extraction

Before troubleshooting the condition, double-check that you're correctly extracting the code value from the response. For example, if you're using JSONPath:

.exec(http("Fetch Data")
  .get("/api/data")
  .check(jsonPath("$.response.code").saveAs("code")))

Ensure the JSONPath points to the right field, and that the field's type matches what you're expecting (numeric vs. string).

Final Tip

If you're unsure about the type of the stored value, you can log it to verify:

.exec(session => {
  println(s"Code value: ${session("code")}, type: ${session("code").getClass}")
  session
})

This will print the value and its type to your console, helping you confirm exactly what you're working with.

内容的提问来源于stack exchange,提问作者Sarath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:08:04