Create a custom validator. Check, if the past of some HTML file contains a substring.

*The sophisticated Scala of Gatling drives me crazy. It sound simple, but after two days I did not find a glue, how to reach it.*

*I have:*

*val scn = scenario("test")
  .exec(http("request_0")
    .get("/")
    .check(status.is(200))
    .check(css("h1").is("Full header")
)*

*I want:*

*val scn = scenario("test")
  .exec(http("request_0")
    .get("/")
    .check(status.is(200))
    .check(css("h1").contains("Part of the header")
)*

*Does somebody have an example to do it?*

Have you given a thought that something that basic is of course already built in?
http://gatling.io/docs/2.1.7/http/http_check.html#http-response-body

2Stéphane: Is it build-in?

I did found a possibility to get a substring of the whole response body.

**check(substring("Something").exists))**

But how to get a substring of the result of the css call? Something like this is needed:

__check(css("*").substring("Something").exists))__

Call specified in the documentation

check(css("*").validate(name, validator))

does not exist.

Use transform to perform the substring on the css selector result.

**It works as**

**check(css("h1").transform((s: String) => s.indexOf("Part of the header")).greaterThan(-1)))**

or (shorter):
check(css(“h1”).transform(_.indexOf(“Part of the header”)).greaterThan(-1)))

or:check(css(“h1”).transformOption(_.flatMap(string => if (string.contains(“Part of the header”)) Some(string) else None)))

or:
check(css(“h1”).transformOption(_.collect { case string if string.contains(“Part of the header”) => string }))