【问题标题】:Gatling Need to run next scenario if previous scenario is passed using doIf如果使用 doIf 传递上一个场景,则 Gatling 需要运行下一个场景
【发布时间】:2019-06-21 04:11:40
【问题描述】:

我是 Scala 和 gatling 的新手。如果之前的场景是使用 doIf 传递的,我需要运行场景。

我的代码是:

HttpRequest

object CompanyProfileRequest {

val check_company_profile: HttpRequestBuilder = http("Create Company 
 Profile")
.get(onboarding_url_perf + "/profile")
.headers(basic_headers)
.headers(auth_headers)
.check(status.is(404).saveAs("NOT_FOUND"))


val create_company_profile: HttpRequestBuilder = http("Create Company 
 Profile")
.post(onboarding_url_perf + "/profile")
.headers(basic_headers)
.headers(auth_headers)
.body(RawFileBody("data/company/company_profile_corporation.json")).asJson
.check(status.is(200))
.check(jsonPath("$.id").saveAs("id"))
 }

场景类是:-

 object ProfileScenarios {

  val createProfileScenarios: ScenarioBuilder = scenario("Create profile 
  Scenario")
  .exec(TokenScenario.getCompanyUsersGwtToken)
  .exec(CompanyProfileRequest.check_company_profile)
  .doIf(session => session.attributes.contains("NOT_FOUND")) {
   exec(CompanyProfileRequest.create_company_profile).exitHereIfFailed
   }
 }

模拟是:-

      private val createProfile = ProfileScenarios
     .createProfileScenarios
     .inject(constantUsersPerSec(1) during (Integer.getInteger("ramp", 1) 
     second))

     setUp(createProfile.protocols(httpConf))

每当我运行此模拟时,我都无法检查此情况:-

.doIf(session => session.attributes.contains("NOT_FOUND"))

非常感谢任何帮助。

问候, 维克拉姆

【问题讨论】:

  • 你能澄清一下“我无法检查这种情况”是什么意思吗?你的 doIf 块总是执行吗?它永远不会执行吗?
  • 我假设您正在尝试调用 check_company_profile,如果返回 404(表示 office 不存在)然后执行 create_company_profile?

标签: scala gatling scala-gatling


【解决方案1】:

我能够让您的示例正常工作,但这里有一个更好的方法...

使用的主要问题

.check(status.is(404).saveAs("NOT_FOUND"))

.doIf(session => session.attributes.contains("NOT_FOUND"))

要实现条件切换,您现在有一个检查会导致 check_company_profile 在它确实不应该失败时失败(例如,当您获得 200 时)。

更好的方法是使用检查转换将布尔值插入“NOT_FOUND”变量。这样,当 office 存在时,您的 check_company_profile 操作仍然可以通过,并且 doIf 构造可以只使用 EL 语法并且更清楚地了解它执行的原因。

val check_company_profile: HttpRequestBuilder = http("Create Company Profile")
  .get(onboarding_url_perf + "/profile")
  .headers(basic_headers)
  .headers(auth_headers)
  .check(
    status.in(200, 404), //both statuses are valid for this request
    status.transform( status => 404.equals(status) ).saveAs("OFFICE_NOT_FOUND") //if the office does not exist, set a boolean flag in the session
  )

现在您已经有了一个布尔会话变量(“OFFICE_NOT_FOUND”),您可以在 doIf 中使用它...

.doIf("${OFFICE_NOT_FOUND}") {
   exec(CompanyProfileRequest.create_company_profile).exitHereIfFailed
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多