在加特林模拟中检查字符串

问题描述 投票:0回答:1

尝试检查主体上是否存在字符串。类似于检查状态为.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%)et”bodyString.find.is(Greeting1),但实际上找到了{“ message”:“ 5(26.32%)Greeting1“}bodyString.find.is(Greeting2),但实际上发现了{“ message”:“ 5(26.32%)Greeting2“}

scala gatling
1个回答
0
投票

原因是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属性中是否存在“问候语”。

© www.soinside.com 2019 - 2024. All rights reserved.