Programming‎ > ‎Scala‎ > ‎

1. Sample Code




How to call APIs and pass parameters


Example API:
Do evaluate whether you need to use optional Vs exists

/step1 -> returns { "key": 888 }
/step2/{key} -> returns { "key": 999 } if {key} passed in is == 888
Scala Code:

var step1 = exec(http("step1")
    .get("/step1")
    .check(jsonPath("$..key").optional.saveAs("key")))

var step2 = exec(http("step2")
    .get("/step2/${key}"))

val scn: ScenarioBuilder = scenario("SimulationName").exec(step1,
    doIf(session => session.contains("key")) {
        exec(step2)
    })

Set session 

    .exec(session => {
      val newSession = session
        .set("var-name", "var-value")
      newSession
    })

Print statement

println("Hello, world!") 
println(userCredentials)

Print statement for session

.exec({ session => println(session("resource_id").as[String])
      session
      })

Reference:
- https://stackoverflow.com/questions/39748780/gatling-how-to-pass-value-in-next-request-when-previous-request-doesnt-have-js
- https://stackoverflow.com/a/40736282/9371636



Comments