LaVOZs

The World’s Largest Online Community for Developers

'; scala - Gatling: Dynamically assemble HttpCheck for multiple Css selectors - LavOzs.Com

I am working on a Gatling test framework that can be parameterized through external config objects. One use case I have is that there may be zero or more CSS selector checks that need to be saved to variables. In my config object, I've implemented that as a Map[String,(String, String)], where the key is the variable name, and the value is the 2-part css selector.

I am struggling with how to dynamically assemble the check. Here's what I got so far:

val captureMap: Map[String, (String, String)] = config.capture
httpRequestBuilder.check(
  captureMap.map((mapping) => {
    val varName = mapping._1
    val cssSel = mapping._2
    css(cssSel._1, cssSel._2).saveAs(varName)
  }).toArray: _* // compilation error here
)

The error I'm getting is:

Error:(41, 10) type mismatch;
 found   : Array[io.gatling.core.check.CheckBuilder[io.gatling.core.check.css.CssCheckType,jodd.lagarto.dom.NodeSelector,String]]
 required: Array[_ <: io.gatling.http.check.HttpCheck]
      }).toArray: _*

apparently, I need to turn my CheckBuilder into a HttpCheck, so how do I do that?

Update:
I managed to get it to work by introducing a variable of type HttpCheck and returning it in the next line:

httpRequestBuilder.check(
  captureMap.map((mapping) => {
    val varName = mapping._1
    val cssSel = mapping._2
    val check:HttpCheck= css(cssSel._1, cssSel._2).saveAs(varName)
    check
  }).toArray: _*
)

While this works, it's ugly as hell. Can this be improved?

Related
Question about Scala variable Mutability
Gatling request based on Webservice response
How to dynamically generate JSon in Gatling?
scala type mismatch osmosis
How to flatten tuples in Spark?
How to pass in a map into UDF in spark
publish and subscribe problems with jeanadrien/gatling-mqtt-protocol plugin for gatling
Gatling - Set Request name dynamically
Gatling: dynamically setup one scenario multiple times
Cannot call use class name directly to map type after adding a companion project