I’m working on adding some syntactic sugar to the Gatling DSL to handle the kinds of things that I find myself doing often. Some examples:
`
scenario( name )
.exec(
http( desc )
.get( path )
.check( jsonPath( “$.result[*]” ).saveAs( OBJECT_LIST ) )
)
// pick one of the objects at random
.set( OBJECT_JSON ).from( OBJECT_LIST ) // I may change the syntax to make it more obvious
.extract( “$.id” ).from( $(OBJECT_JSON) ).into( OBJECT_ID )
`
I’m following the “pimp my library pattern” that worked before. But now it is not working correctly, and I’m stumped as to why.
`
import scala.util.Random
import io.gatling.core.session.{ Expression, Session }
import io.gatling.core.Predef._
import io.gatling.core.validation._
import io.gatling.core.structure.ChainBuilder
import io.gatling.core.json.Boon
import io.gatling.core.check.extractor.jsonpath._
object SessionManagement {
implicit class SessionManagementExtensions( val c : ChainBuilder ) {
trait SetSessionVariableAPI {
def from( src: String ) : ChainBuilder
def to[T]( value: Expression[T] ) : ChainBuilder
}
def set( dest: String ) = new SetSessionVariableAPI {
def from( src: String ) =
c.exec( session => {
val list = session( src ).as[Vector[String]]
val i = if ( list.size == 0 ) -1 else Random.nextInt( list.size )
val value = if ( i > 0 ) list(i) else “INVALID_” + dest
session.set( dest, value )
})
def to[T]( value: Expression[T] ) =
c.exec( session => session.set( dest, value(session) ) )
}
trait ExtractFromInto { def into( name: String ) : ChainBuilder }
trait ExtractFrom { def from( json: Expression[String] ) : ExtractFromInto }
def extract( path: String ) = new ExtractFrom {
def from( json: Expression[String] ) = new ExtractFromInto {
def into( name: String ) =
c.exec( session => {
val parsed = Boon.parse( json(session).toString )
session.set( name, JsonPathExtractor.extractAll[String]( parsed, path ).get )
})
}
}
}
}
`
This seems to compile (which doesn’t mean I did it right, but at least it compiles), but when I try to use it, I get an error:
11:56:52.169 [ERROR] i.g.a.ZincCompiler$ - /src/rtde-testing/performance/rtde/simulations/com/cigna/rtde/scenarios/sandbox.scala:17: value set is not a member of io.gatling.core.structure.ChainBuilder possible cause: maybe a semicolon is missing before
value set’?
11:56:52.172 [ERROR] i.g.a.ZincCompiler$ - .set( “FOO” ).to(“bar”)
11:56:52.173 [ERROR] i.g.a.ZincCompiler$ - ^
`
Can you see what I’m doing wrong that might cause me to get this kind of an error?