Data from feeder not available in scenario

2019-09-14 23:22发布

问题:

I have a problem where it looks like data from a feeder isn't added to the session. I'm testing a system where a user has a role (i.e. user or admin) and I need to perform various tests depending on the role. Following the advanced tutorial I have split my chains into different objects (each object in its own file) - one of the objects contain the login procedure which is the same regardless of the users role. I wan't re-use this, but with different feeders.

I load the username and password using the csv feeder, but it looks like it fails to add the data to the session because I get Failed to build request Submit user/password: No attribute named 'password' is defined when running the simulation.

Here is my code which is split into multiple files:

Simulation.scala

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class Simulation extends Simulation {  
  val httpProtocol = http
    .baseURL("baseURI")

  val admins = scenario("Admins")
    .feed(csv("admins.csv"))
    .exec(Login.login)
    .exec(TaskA.taskA)

  val users = scenario("Users")
  .feed(csv("users.csv"))
  .exec(
    Login.login,
    TaskB.taskB,
    TaskC.taskC
  )


  setUp(
    admins.inject(atOnceUsers(1))/*,
    users.inject(atOnceUsers(1))*/
  ).protocols(httpProtocol)
}

Login.scala

object Login {
  val login = group("Login") {
    exec(http("Get form")
    .get("/login/login.php")
    .check(form("""form[name="relay"]""").saveAs("passwordForm")))
    .pause(10 seconds)

    .exec(http("Submit user/password")
    .post("/login/authenticate.php")
    .form("${passwordForm}")
    .formParam("pass", "${password}") //TODO: get from feeder
    .formParam("user", "${username}") //TODO: get from feeder
)
}
}

For now the csv files are the same:

username,password
user,user

Strangely enough it works if I move the .feed(csv("admins.csv")) into Login.scala, but then I can't re-use it for for users.

回答1:

The solution proved to be quite simple - inject different instances of Login into each scenario like this:

Login.scala

object Login {
  def login(feeder: FeederBuilder[_]): ChainBuilder = {
     feed(feeder)
     ...
  }
}

Then in the simulation

Simulation.scala

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class Simulation extends Simulation {  
  val httpProtocol = http
    .baseURL("baseURI")

  val admins = scenario("Admins")
    .exec(Login.login(csv("admins.csv")))
    .exec(TaskA.taskA)

  val users = scenario("Users")
  .exec(
    Login.login(csv("users.csv")),
    TaskB.taskB,
    TaskC.taskC
  )


  setUp(
    admins.inject(atOnceUsers(1))/*,
    users.inject(atOnceUsers(1))*/
  ).protocols(httpProtocol)
}


标签: gatling