【问题标题】:Gatling - No attribute named 'CSRF' is definedGatling - 未定义名为“CSRF”的属性
【发布时间】:2021-03-03 19:24:50
【问题描述】:

我得到这个错误,我的属性没有定义,我不知道为什么。

我首先将它作为 CSRF 变量保存在我的第一个请求中。
稍后我尝试在发送请求以填写表单时访问它。

这是我的主要模拟课程:

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)
}

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

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$$Lambda$434/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 
================================================================================

难道我有这种行为是因为输入字段被标记为隐藏?
或者有什么我看不到的。

请帮忙。
谢谢!

【问题讨论】:

    标签: scala gatling


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。不幸的是,这并没有帮助。我仍然得到错误。这次我编辑了帖子以包含点,并从控制台上传了日志。你还有什么想法吗? :D 再次感谢您与我们联系:-*
    • 这很奇怪。我注释掉了一堆代码,只留下了“加载登录页面”的部分,然后我在日志中得到了这个:“j.n.ConnectException:连接被拒绝:没有更多信息”。我认为我的设置有问题,但我不知道是什么。不知道往哪个方向走。如何解决?
    • 我还更新了我的帖子以强调这种奇怪的行为
    • 事情并非如此。问一个问题,得到一个答案,接受它,如果你有不同的问题,创建一个新问题。不要编辑您的原始问题。
    • 关于 ConnectException 的问题,我再次从头开始我的项目,就像在 gatling acafmy 的第一个模块中建议的那样。之后,问题不再出现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2016-05-01
    • 2016-05-18
    • 2012-09-16
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多