加特林使用 session.set 并同时喂食

Gatling use session.set and feed simultaneously

如果我使用 .get("/***/quotes-${endPoint}/quotes?source=rtbp&userid=test&symbol=${pTypeSymbol}${authM}${pEqSymbol}") 那么 ${pEqSymbol} 可以工作但是 ${pTypeSymbol} 将是 ${pEqSymbol} 获取它的错误示例应该在下面的代码中

val getApiKeyScenario = scenario("getApiKey")
    .feed(getApiKeyData)
    .feed(pEqSymbolFeed)
    .feed(pOptionSymbol)
    .feed(pOtherSymbol)
    .exec(session => session
      .set("endPoint", "v1")
      .set("pTypeSymbol", "${pEqSymbol}")
      .set("authM", "&apikey=***********"))
    .exec(http("getApiKeyRequest")
      .get("/******/quotes-${endPoint}/quotes?source=rtbp&userid=test&symbol=${pTypeSymbol}${authM}")
      .check(status.is(200))
      .check(checkIf(doLogResponse) {
        bodyString.saveAs("pResponse")
      })
    )
    .doIf(doLogResponse) {
      logResponse()
    }

如果我尝试 .set("pTypeSymbol", pEqSymbolFeed.readRecords.head("pEqSymbol")) 将会循环 如果我尝试 .set("pTypeSymbol", s"${pEqSymbol.isDefined}") 未找到:值 pEqSymbol 如果我尝试 s"${pEqSymbol}" 未找到:值 pEqSymbol

我现在的日志是GET *******/quotes-v1/quotes?source=rtbp&userid=test&symbol=${pEqSymbol}&apikey=****** 不过应该是GET *******/quotes-v1/quotes?source=rtbp&userid=test&symbol="Here my value from feed"&apikey=******

请阅读official documentation:

This Expression Language only works on String values being passed to Gatling DSL methods. Such Strings are parsed only once, when the Gatling simulation is being instantiated.

For example queryParam("latitude", session => "${latitude}") wouldn’t work because the parameter is not a String, but a function that returns a String.

在你的例子中,你不需要将pEqSymbol复制到pTypeSymbol,你可以直接写:

.exec(session => session
      .set("endPoint", "v1")
      .set("authM", "&apikey=***********"))
    .exec(http("getApiKeyRequest")
      .get("/******/quotes-${endPoint}/quotes?source=rtbp&userid=test&symbol=${pEqSymbol}${authM}")
      )
    )

但如果硬要复制,就得用Session API:

.set("pTypeSymbol", session("pEqSymbol").as[String])