1
votes

Trying to check if a string exists on the body. Similar to checking status which is .check(status.is(200)). I want to check for string as well. tried .check(bodyString.is("greeting")) but got an error:

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)

}

---- Errors --------------------------------------------------------------------

bodyString.find.is(Greeting), but actually found {"message":"G 9 (47.37%) reeting"} bodyString.find.is(Greeting1), but actually found {"message":" 5 (26.32%) Greeting1"} bodyString.find.is(Greeting2), but actually found {"message":" 5 (26.32%) Greeting2"}

1
Have you checked this answer? https://stackguides.com/questions/35596668/gatling-check-if-a-html-result-contains-some-string Something like this should work: .check(substring("greeting").exists)anasmi

1 Answers

0
votes

The reason is the bodyString return the full response body, as it described in the documentation.

You could use in matcher (you can see the implementation here - look for InMatcher[A], but it won't work because you need to switch expected.contains(actualValue) with expected.contains(expected)

I suggest you implement your own 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
  }
}

and use it:

.check(jsonPath("$.message").validate(customIn("Greeting")))), which checks if "Greeting" exists in the message attribute of the json response body.