Gatling 需要 运行 下一个场景,如果前一个场景使用 doIf 传递

Gatling Need to run next scenario if previous scenario is passed using doIf

我是 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"))
 }

场景class是:-

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

非常感谢任何帮助。

此致, 维克拉姆

我能够使您的示例起作用,但这里有一个更好的方法...

使用

的主要问题
.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
}