Send a parameter to function that contains a session attribute

Hi,

I'm trying to send a parameter to function (runCommand) that contains a session attribute.

Once I call it by this way, the method theiaTasksUtil.runCommand is not called at all.

* var deployToCF = ""*

* val fioriS4Scenario = scenario("Fiori S4 Scenario") .feed(mainFeeder) //contains username .exec{session => deployToCF = "cf deploy /home/user/projects/mta_bp/mta_archives/mta_bp_" + session("username").as[String] + "_0.0.1.mtar" println("********************deployToCf: " + deployToCF) //prints ********************deployToCf: cf deploy /home/user/projects/mta_bp/mta_archives/mta_bp_user1_0.0.1.mtar exec(theiaTasksUtil.runCommand(deployToCF, "deploy MTAR to CF")) session }*

In case I do it in this way (using global variable), deployToCF is sent as empty string (initial value)

* var deployToCF = ""*

* val fioriS4Scenario = scenario("Fiori S4 Scenario") .feed(mainFeeder) //contains username .exec{session => deployToCF = "cf deploy /home/user/projects/mta_bp/mta_archives/mta_bp_" + session("username").as[String] + "_0.0.1.mtar" println("********************deployToCf: " + deployToCF) //prints ********************deployToCf: cf deploy /home/user/projects/mta_bp/mta_archives/mta_bp_user1_0.0.1.mtar session }*

    * .exec(theiaTasksUtil.runCommand(deployToCF, "deploy MTAR to CF"))*

Could you please tell me how to pass the session attribute as a parameter?

Thanks,

David

Hi David,

Look at the warning in the documentation.

Gatling DSL components are immutable ActionBuilder(s) that have to be chained altogether and are only built once on startup. The results is a workflow chain of Action(s). These builders don’t do anything by themselves, they don’t trigger any side effect, they are just definitions. As a result, creating such DSL components at runtime in functions is completely meaningless.

If you want to do string interpolation with the session attributes, there is the expression language for that.

Regards,
George

Hi George,

Thank you for your reply.

I’m still struggling to send a session parameter as a parameter to the custom method.

The script below calls runCommand with a string “${deployToCF}” and not the value of ${deployToCF}.

Any idea how to send the value?

exec { session =>
  session.set("deployToCF", "cf deploy /home/user/projects/mta_bp/mta_archives/mta_bp_" + session("username").as[String] + "_0.0.1.mtar")
}
.exec { session =>
  println("**********************deployToCF:" + session("deployToCF").as[String] ) --> prints the correct value: **********************deployToCF:cf deploy /home/user/projects/mta_bp/mta_archives/mta_bp_**user1**_0.0.1.mtar
  session
}
.exec(theiaTasksUtil.runCommand("${deployToCF}", "deploy MTAR to CF")) --> sends a string "${deployToCF}" and not the value of ${deployToCF}

Since the variable is in the session, first you need to get it to a local variable (Scala variable) and then pass the scala variable to your custom function.

.exec { session =>
  println("**********************deployToCF:" + session("deployToCF").as[String] ) --> prints the correct value:
val deployToCF =  session("deployToCF").as[String]

Since it is a custom method, we don’t know it’s implementation, or even the type signature unless you post it. So here is my speculation.

It probably takes a String rather than an Expression[String], and the runCommand function does not put the value from the session into the request.

Your guess is correct. The signature of runCommand is String, String.
By sending it with runCommand ("${deployToCF}", “deploy MTAR to CF”)) I expect Gatling compiler to convert it to the value, but it does not happen :frowning:


def runCommand(command: String, commandDescription: String): ChainBuilder = {
  setTaskParams(command, commandDescription)
    .exec(sendTask(command, commandDescription))
}

Unfortunately, in this case, the runCommand() method is not triggered at all (from the session block)