【问题标题】:Using gatling session variable in triple qouted string在三引号字符串中使用加特林会话变量
【发布时间】:2018-01-12 05:17:22
【问题描述】:

如何在 gatling 的 StringBody 中使用 session 变量?

我已经定义了我的exec 喜欢,

val migrateAsset = exec(_.set("assetId", AssetIdGenerator.generateRandomAssetId()))
      .exec(http("Migrate Asset")
      .post(s"$url/asset/metadata")
      .header("Content-Type", "application/json")
      .header("Authorization", s"Bearer ${authToken}")
      .body(StringBody(
          s"""
            |{
            |    "objectType" : "DocumentType",
            |    "fileName" : "main.xml",
            |    "locations" : [
            |        {
            |            "region" : "eu-west-1",
            |            "url" : "https://s3-eu-west-1.amazonaws.com/${bucketName}/${assetId}"
            |        },
            |        {
            |            "region" : "us-east-1",
            |            "url" : s"https://s3.amazonaws.com/${bucketName}/${assetId}"
            |        }
            |    ],
            |    "format" : "MAIN",
            |    "mimeType" : "text/plain"
            |}
          """.stripMargin
      ))
      .check(status.is(200)))

在正文中,我希望为 eu-west 和 us-east 地区传递相同的 assetId。由于assetId 是随机生成的,因此我将其存储在会话变量中,以确保我对两个位置都使用相同的assetId。

但我不能以 StringBody 格式传递 assetId。它不断给我错误,例如,

AssetsMigrationLoadSimulation.scala:31:未找到:值 资产 ID | "url" : "https://s3-eu-west-1.amazonaws.com/${bucketName}/${assetId}"

【问题讨论】:

  • 您是否尝试过使用这样的表达式函数:.body(StringBody(session => """{ "myContent": """" + someGenerator(session) + """" }""")).asJSON from the docs
  • 不太确定,但你有很多 s"..." 在那里。这意味着 Scala 将评估 EL 构造本身,而不是 Gatling。如果您删除 s 前缀,它可能会起作用。

标签: scala performance-testing gatling scala-gatling gatling-plugin


【解决方案1】:

正如 Hans-Peter 所提到的 - 您的 IDE 已经看到您用来引用 gatling 会话参数的 ${...} 并决定您正在尝试进行常规 scala 字符串插值 - 所以它把 's ' 在字符串前面。

删除's',这应该可以工作

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多