Skip to content
slandelle edited this page Mar 23, 2012 · 23 revisions

Concepts #

The Check API is used for verifying that the response to a request matches expectations and capturing some elements in it.

Checks are performed on a request thanks to the method check. For example, on an HTTP request :

http("My Request").get("myUrl").check(status.is(200))

One can of course perform multiple checks:

http("My Request").get("myUrl").check(status.not(404), status.not(500)))

This API provides a dedicated DSL for chaining the following steps:

  1. defining the check
  2. extracting
  3. transforming
  4. verifying
  5. saving

Defining the check type #

This part is specific to a given Check implementation.

The HTTP Check implementation provides the following built-ins:

HTTP status: #

status

Targets the HTTP response status code.

Note: A status check is automatically added to a request when you don't specify one. It checks that the HTTP response has a 2XX status code.

HTTP header: #

header(headerName)

Targets the HTTP response header of the given name. headerName can be a simple String, an evaluatable String containing expression, or a (Session => String) function.

Note: The header names are available as constants in the DSL. They all are written in upper case and words are separated with underscores, eg: CONTENT_TYPE

HTTP response body:

  • regex(expression) #

Defines a Java regular expression to be applied on any text response body.

expression can be a simple String, an evaluatable String containing expression, or a (Session => String) function.

It can contain 0 or 1 capture group.

regex("""<td class="number">""")
regex("""<td class="number">ACC${account_id}</td>""")
regex("""/private/bank/account/(ACC[0-9]*)/operations.html""")

Note: In scala, you can use escaped strings with this notation: """my "non-escaped" string""". This simplifies the writing and reading of regular expressions.

  • xpath(expression, namespaces) #

Defines an XPath 1.0 expression to be applied on an XML response body.

expression can be a simple String, an evaluatable String containing expression, or a (Session => String) function.

namespaces is an optional List of couples of (prefix, uri)

xpath("//input[@id='text1']/@value")

Note: You can also use vtdXpath(xpathExpression: String), this check uses VTD as the XPath engine, it is available as a separate module.

  • jsonPath(expression) #

Similar to XPath 1.0, but to be applied on a JSON response body. It is intended for capturing leaves of the JSON tree, so only a subset of XPath is supported.

expression can be a simple String, a String containing an EL expression, or a (Session => String) function.

jsonPath("//foo/bar[2]/baz")

Note: HTTP checks are performed in the order of HTTP element precedence: first status, then headers, then response body.

Extracting #

  • find: return the first occurrence

  • find(occurrence): return the occurrence of the given rank

Note: Ranks start at 0.

  • findAll: return a List of all the occurrences

  • count: return the number of occurrences

find(occurrence), findAll and count are only available on check types that might produce multiple results. For example, status only has find.

Note: In case of no extracting step is defined, a find is added implicitly.

Transforming #

'transform(transformationFunction)' Transforming is an optional step for transforming the result of the extraction before trying to match or save it.

transformationFunction is a function whose input is the extraction result and output is the result of your transformation.

transform((s: String) => s + "foo")'

Verifying #

  • is(expected) #

Checks that the value is equal to the expected one.

expected is a function that returns a value of the same type of the previous step (extraction or transformation). In case of a String, it can also be a static String or a String with an EL expression.

  • not(expected) #

Checks that the value is different from the expected one.

expected is a function that returns a value of the same type of the previous step (extraction or transformation). In case of a String, it can also be a static String or a String with an EL expression.

  • exists #

Checks that the value exists and is not empty in case of multiple results.

  • notExists #

Checks that the value doesn't exist and or is empty in case of multiple results.

  • in(sequence) #

Checks that the value belongs to a given sequence.

expected is a function that returns a sequence of values of the same type of the previous step (extraction or transformation).

Note: In case of no verifying step is defined, a exists is added implicitly.

Saving #

saveAs(key)

Saving is an optional step for storing the result of the previous step (extraction or transformation) into the virtual user Session, so that it can be reused later.

key is a String

Putting it all together #

To help you understand the checks, here is a list of examples:

check(regex("""https://(.*)""").count.is(5))

Verifies that there are exactly 5 HTTPS links in the response

check(regex("""https://(.*)/.*""")
      .findAll
      .is(List("www.google.com", "www.mysecuredsite.com"))

Verifies that there are two secured links pointing at the specified websites.

check(status.is(200))

Verifies that the status is equal to 200

check(status.in(200 to 210))

Verifies that the status is one of: 200, 201, 202, ..., 209, 210

check(regex("aWord").find(1).exists))

Verifies that there are at least two occurrences of "aWord"

check(regex("aWord").notExists)

Verifies that the response doesn't contain "aWord"

04/12/2012 Gatling 1.1.3 is out

Contents

General Information

User Documentation

Developing Gatling

Clone this wiki locally