Gatling - 未定义名为 'CSRF' 的属性

Gatling - No attribute named 'CSRF' is defined

我收到此错误消息,指出我的属性未定义,但我不明白为什么。

我把它保存在我的第一个请求中作为 CSRF 变量。
稍后我尝试在 post 请求填写表格时访问它。

这是我的主要模拟class:

package simulations.stage

import io.ecx.steps.{Config, Login}
import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder

import scala.concurrent.duration._

class RampUsersLoadSimulations extends Simulation{

  val httpConf: HttpProtocolBuilder = http.baseUrl(Config.baseUrl)
    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
    .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36")
    .inferHtmlResources()
    .acceptEncodingHeader("gzip, deflate, br")
    .proxy(Proxy("localhost", 8866))

  before {
    println(s"Running for: ${Config.baseUrl}")
  }

  val login: ScenarioBuilder = scenario("Scenario: Login to the storefront")
    .exec(
      Login.login(Config.accounts),
      Login.navigateToMyAccountPage())

  setUp(login.inject(atOnceUsers(1))).protocols(httpConf)
}

这是我的步骤对象,我尝试在其中保存它,然后在 post 请求中使用它:

package io.ecx.steps

import io.gatling.core.Predef._
import io.gatling.http.Predef._

object Login {

  def login(accountsPath: String) = {
    val accounts = csv(accountsPath).random

      exec(http("Load Login Page")
      .get("/login")
      .check(regex("<title>My Title</title>").exists)
      .check(css("[name=CSRFToken]", "value").saveAs("CSRF"))
      .check(status.is(200)))
      .pause(2)
//      .exec{session => println(session); session}
    feed(accounts)
      .exec(http("Log in with credentials to the storefront")
        .post("/j_spring_security_check")
        .formParam("username", "${username}")
        .formParam("password", "${password}")
        .formParam("rememberMe", "${rememberMe}")
        .formParam("CSRFToken", "${CSRF}")
        .check(css("#loginForm").notExists)
      .check(status.in(200)))
      .pause(5)
  }

  def navigateToMyAccountPage() = {
    exec(http("Open My account Page")
    .get("accountSummary")
    .check(css(".page-AccountSummaryPage").exists)
    .check(status.in(200)))
      .pause(2)
  }
}

在加载登录页面时加载的 HTML 中,我们有:

<input type="hidden" name="CSRFToken" value="6ac89c39-ee25-4cdc-9553-cc8a7824f43b" />

这是日志:

Simulation simulations.stage.RampUsersLoadSimulations started...
Session(Scenario: Login to the storefront,1,Map(gatling.http.ssl.sslContexts -> io.gatling.http.util.SslContexts@6e62fe0c, gatling.http.cache.dns -> io.gatling.http.resolver.ShufflingNameResolver@56fe67f3, gatling.http.cache.baseUrl -> https://my-site.com),KO,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda4/1485485458@7dcefc4,io.netty.channel.nio.NioEventLoop@70f02c32)
23:29:59.611 [ERROR] i.g.h.a.HttpRequestAction - 'httpRequest-4' failed to execute: No attribute named 'CSRF' is defined

================================================================================
2021-03-03 23:30:00                                           5s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=0      KO=1     )
> Load Login Page                                          (OK=0      KO=1     )
---- Errors --------------------------------------------------------------------
> j.n.ConnectException: Connection refused: no further informati      1 (50.00%)
on
> Log in with credentials to the storefront: Failed to build req      1 (50.00%)
uest: No attribute named 'CSRF' is defined 

---- Scenario: Login to the storefront -----------------------------------------
[--------------------------------------------------------------------------]  0%
          waiting: 0      / active: 1      / done: 0     
================================================================================


================================================================================
2021-03-03 23:30:05                                          10s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=0      KO=1     )
> Load Login Page                                          (OK=0      KO=1     )
---- Errors --------------------------------------------------------------------
> j.n.ConnectException: Connection refused: no further informati      1 (50.00%)
on
> Log in with credentials to the storefront: Failed to build req      1 (50.00%)
uest: No attribute named 'CSRF' is defined 

---- Scenario: Login to the storefront -----------------------------------------
[--------------------------------------------------------------------------]  0%
          waiting: 0      / active: 1      / done: 0     
================================================================================


================================================================================
2021-03-03 23:30:08                                          13s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=0      KO=2     )
> Load Login Page                                          (OK=0      KO=1     )
> Open My account Page                                     (OK=0      KO=1     )
---- Errors --------------------------------------------------------------------
> j.n.ConnectException: Connection refused: no further informati      2 (66.67%)
on
> Log in with credentials to the storefront: Failed to build req      1 (33.33%)
uest: No attribute named 'CSRF' is defined 

---- Scenario: Login to the storefront -----------------------------------------
[##########################################################################]100%
          waiting: 0      / active: 0      / done: 1     
================================================================================

Simulation simulations.stage.RampUsersLoadSimulations completed in 13 seconds
Parsing log file(s)...
Parsing log file(s) done
Generating reports...

================================================================================
---- Global Information --------------------------------------------------------
> request count                                          2 (OK=0      KO=2     )
> min response time                                   2015 (OK=-      KO=2015  )
> max response time                                   2019 (OK=-      KO=2019  )
> mean response time                                  2017 (OK=-      KO=2017  )
> std deviation                                          2 (OK=-      KO=2     )
> response time 50th percentile                       2017 (OK=-      KO=2017  )
> response time 75th percentile                       2018 (OK=-      KO=2018  )
> response time 95th percentile                       2019 (OK=-      KO=2019  )
> response time 99th percentile                       2019 (OK=-      KO=2019  )
> mean requests/sec                                  0.143 (OK=-      KO=0.143 )
---- Response Time Distribution ------------------------------------------------
> t < 800 ms                                             0 (  0%)
> 800 ms < t < 1200 ms                                   0 (  0%)
> t > 1200 ms                                            0 (  0%)
> failed                                                 2 (100%)
---- Errors --------------------------------------------------------------------
> j.n.ConnectException: Connection refused: no further informati      2 (66.67%)
on
> Log in with credentials to the storefront: Failed to build req      1 (33.33%)
uest: No attribute named 'CSRF' is defined 
================================================================================

会不会是因为输入字段被标记为隐藏,所以我有这种行为?
或者有什么东西我看不到。

请帮忙。
谢谢!

您的代码有一个错误:您在 feed(accounts) 之前漏掉了一个点。因此,它不附加到头部,只考虑尾巴。