首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gatling -没有定义名为“CSRF”的属性

Gatling -没有定义名为“CSRF”的属性
EN

Stack Overflow用户
提问于 2021-03-03 19:24:50
回答 1查看 543关注 0票数 3

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

我首先将它作为CSRF变量保存在我的第一个请求中。

稍后,我尝试访问它,同时进行帖子请求,以填写该表格。

这是我的主要模拟课:

代码语言:javascript
复制
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)
}

这是我的steps对象,我试图保存它,然后在post请求中使用它:

代码语言:javascript
复制
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中,我们有:

代码语言:javascript
复制
<input type="hidden" name="CSRFToken" value="6ac89c39-ee25-4cdc-9553-cc8a7824f43b" />

这是日志:

代码语言:javascript
复制
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 
================================================================================

可能是因为输入字段被标记为隐藏,所以我有此行为吗?

或者有些东西我看不见。

请帮帮忙。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-03 21:58:21

代码中有一个错误:在feed(accounts)之前遗漏了一个点。因为这一点,它没有连接到头部,只有尾巴被考虑。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66463587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档