Parsing Value from HTML Response without Failing if They Don't Exist

Hello! :slight_smile:

I was reading around the forum and got thinking about the “Gatling way” to parse an HTML response when we simply want to get data to decide what request to fire next (and not to specifically check for success).

In all the tests I have ever written I am doing the following when I want to parse a value from an HTML response and store it in the session:

css("<CSS Selectors Here>", "value").saveAs("sessionVariableName")

This way when the CSS selector doesn’t find the elements I get a failed check and, if my request is using exitHereIfFailed, Gatling will log this request as an error and stop the scenario for that user.

Suppose I have the following case:

val someChecks: Seq[HttpCheck] = Seq(,
  css("<CSS Selectors Here>").saveAs("sessionVariableName")

... .check(someChecks).exitHereIfFailed

In this case if I want to store the value in the session if it exists and not fail if it doesn’t exist, I would have to remove the exitHereIfFailed part above. Since I have more than one checks, is there a way to fail if for example my someCustomCheck fails but not when the CSS check does?

As I understand it so far, my best bet would be to do something like this instead:

val someChecks: Seq[HttpCheck] = Seq(,
  bodyString.transform(htmlResponse => {
    "<input name=\"commonInputName\" value=\"([0-9]+)\"".r
      .map(matchedItem => ("commonInputName",

This way I can do all my custom stuff within the transform function parameter. The problem with that is that I am loosing the CSS selector features which are probably more efficient. In addition, how would I store multiple session variables from within the transform block?

I guess digging up the classes/functions used by css() and using them, if possible, in my transform() wouldn’t be a very safe way to do it, right?

To give a more concrete example of what I am thinking, here is a list of steps an imaginary scenario would do:

  1. Fire a request to a page.
  2. Check for specific set of fields in the HTML.
    3a. If the fields are found, store them in the session along some with random values (maybe coming from a feeder). Is a CSS check enough to store a list of HTML input fields that share the same name (i.e., being passed as an array of values to a servlet for example)?
    4a. If the list is found in the session fire a request with those parameters (i.e., doIf). Also, how would you fire a request with params found in the session that share the same name? I’m guessing you would use formParamSeq instead formParamMap as you have duplicate keys in that case.
    5a. If the list is not found do something else.
    3b. If the fields are not found, leave the site.

I might be overthinking this. In any case, thanks in advance for any information on this.

Can’t you use optional for this? Gatling - Checks

1 Like

Thanks! I will try this one. :slight_smile: