Home > Net >  Karate-Gatling: Not able to use object fields inside Karate features
Karate-Gatling: Not able to use object fields inside Karate features

Time:04-12

For the following Gatling simulation

class DeviceSimulation extends Simulation {

  var devices: List[Device] = List[Device]()

  before {
    // Preparing data.
    devices = DataFetch.getDevices()
  }

   // Feed device
  val devicesFeederCont: Iterator[Map[String, Device]] = Iterator.continually(devices.map(d => {
    Map("device" -> d)
  })).flatten
  val devicesFeederToKarate: ScenarioBuilder = scenario("feederDeviceToKarate").exec(karateSet("device", session => session("device").as[Device]))


  val Devices: ScenarioBuilder = scenario("Device")
    .feed(devicesFeederCont)
    .exec(devicesFeederToKarate)
    .exec(karateFeature("classpath:features/device/Devices.feature"))

  setUp(
    Devices.inject(rampUsers(5).during(5 seconds))
  ).protocols()
}

I would like to be able to inject Device object inside my feature:

Feature: Device actions

  Background:
    * url 'https://server-host'
    * print 'Device obj: ', device


  Scenario: Device actions

    Given path '/api/device/name/', device.name
    When method GET
    Then status 200

But, although for the Background print I get: c.intuit.karate - [print] Device obj: Device(1234,989898989), for the GET request I have: GET /api/device/name/com.intuit.karate.graal.JsExecutable@333d7..

I mention that Device is just a case class with two fields: case class Device(id: Int, name: String).

Is there a way to properly use objects passed from feeder inside Karate features?

CodePudding user response:

Right now we've tested only with primitive values passed into the Gatling session. It may work if you convert the data into a java.util.Map. So maybe your best bet is to write some toMap() function on your data-object. Or if you manage to emit a JSON string, there is a karate.fromString() helper that can be useful.

So please read the docs here and figure out what works: https://github.com/karatelabs/karate/tree/master/karate-gatling#gatling-session

You are most welcome to contribute code to improve the state of things.

  • Related