使用 Gatling 检索 cookie 值

Retrieve cookie value with Gatling

我对 Gatling 有点陌生,我想从 cookie 中获取值。我尝试了很多方法,但我可能会误解一些东西。

起初我向我的身份验证 API 发出 post 请求,它创建了我想要的 cookie。

那我试过了:

   .exec {
      session => println(session)
      println(session.attributes)

      // return a Some object whose value is of type CookieJar (with apparently private access)
      println(session.attributes.get("gatling.http.cookies"))

      /*
      // Doesn't compile due to CookieJar being private
      val value: CookieJar = session.attributes.get("gatling.http.cookies") match {
        case None => None
        case Some(cj: CookieJar) => cj
      }
      print(value)
      */

      // return a GetCookieBuilder which doesn't seem really useful
      println(getCookieValue(CookieKey("COOKIE_NAME")))

      session
    }

你有什么想法吗?

getCookieValue 是 DSL 组件,不是您可以在自己的函数中调用的方法。

它用作从内部 CookieJar 中提取 cookie 值并将其作为专用属性复制到 Session 中的场景步骤。

exec(getCookieValue(CookieKey("COOKIE_NAME")))
.exec { session =>
  println(session("COOKIE_NAME").as[String])
  session
}