【问题标题】:Check string in gatling simulation在加特林模拟中检查字符串
【发布时间】:2019-10-11 14:45:14
【问题描述】:

尝试检查正文中是否存在字符串。类似于检查.check(status.is(200)) 的状态。我也想检查字符串。试过.check(bodyString.is("greeting")),但报错:

val scn = scenario("GreetingPages")
.during(testTimeSecs) {
  exec(
    http ("greeting")
      .get("/greeting")
      .check(status.is(200))
      .check(bodyString.is("Greeting"))
  ).pause(minWaitMs,maxWaitMs)
  .exec(
    http("greeting1")
      .get("/greeting1")
      .check(status.is(200))
      .check(bodyString.is("Greeting1"))
  ).pause(minWaitMs,maxWaitMs)
  .exec(
    http("Third page")
      .get("/greeting2")
      .check(status.is(200))
      .check(bodyString.is("Greeting2"))
  ).pause(minWaitMs,maxWaitMs)

}

---- 错误 ------------------------------------------ --------------------------

bodyString.find.is(Greeting),但实际找到 {"message":"G 9 (47.37%) 重来"} bodyString.find.is(Greeting1),但实际找到 {"message":" 5 (26.32%) 问候1"} bodyString.find.is(Greeting2),但实际找到 {"message":" 5 (26.32%) 问候2"}

【问题讨论】:

标签: scala gatling


【解决方案1】:

原因是bodyString 返回完整的响应正文,如documentation 中所述。

你可以使用in匹配器(你可以看到here - look for InMatcher[A]的实现,但它不起作用,因为你需要将expected.contains(actualValue)切换到expected.contains(expected)

我建议你实现自己的MyOwnInMatcher:

class MyOwnInMatcher[A](expected: Seq[A]) extends Matcher[A] {

  def name: String = "customIn"

  protected def doMatch(actual: Option[A]): Validation[Option[A]] = actual match {
    case Some(actualValue) =>
      if (expected.contains(actualValue))
        actual.success
      else
        s"found $actualValue".failure
    case _ => Validator.FoundNothingFailure
  }
}

并使用它:

.check(jsonPath("$.message").validate(customIn("Greeting")))),检查json响应体的message属性中是否存在“Greeting”。

【讨论】:

    猜你喜欢
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多