Trying to access Scala Array elements using Gatling DSL

My script has an array of static values and I need to execute HTTP POST using every value in that array

What I have done is

val ContextPaths = Array(“100.200.201”, “100.200.2002”, “100.200.2003”, “100.200.2007”, “100.200.2001”, “100.200.2004”, “100.200.2005”, “100.200.2008”, “100.200.2006”)

object LaunchSeq{

val launch =

.foreach(_(“ContextPaths”).validate[Seq[String]], “CP”){

http(“action4”)

.post("/bell-tv/app")

.headers(sentHeaders)

.formParamSeq(Seq((Const.ApiRev._1, Const.ApiRev._2), (Const.ApiVer._1, Const.ApiVer._2), (Const.AppId._1, Const.AppId._2), (Const.AppVer._1, Const.AppVer._2), (Const.BillingType._1, Const.BillingType._2), (Const.CarrierId._1, Const.CarrierId._2), (Const.ClientAppVer._1, Const.ClientAppVer._2),

(Const.ClientBuild._1, Const.ClientBuild._2), (Const.ClientVer._1, Const.ClientVer._2), ("contextPath", "${CP}"), (Const.DeviceModel._1, Const.DeviceModel._2), (Const.DeviceName._1, Const.DeviceName._2), (Const.FormFactor._1, Const.FormFactor._2), (Const.IosModel._1, Const.IosModel._2), (Const.LibVer._1, Const.LibVer._2), (Const.Locale._1, Const.Locale._2),

(Const.Mfgr._1, Const.Mfgr._2), (Const.Network._1, Const.Network._2), (Const.OsPlatfrm._1, Const.OsPlatfrm._2), (Const.OsVer._1, Const.OsVer._2), (“render”, “json”), (“subscriberId”, “${SUBSCRIBERID}”), (“uniqueId”, “${UNIQUEID}”), (Const.WebVtt._1, Const.WebVtt._2), (Const.PhoneNum._1, Const.PhoneNum._2), (“action”, “4”))).asJSON

.check(regex("“status”:1").exists)

) }

}

I get an error - [ERROR] i.g.c.s.LoopBlock$ - Condition evaluation crashed with message ‘No attribute named ‘ContextPaths’ is defined’, exiting loop

I am sure I am missing something here as I haven’t set the Array[] as a session attribute. Where and how should I handle this?

Regards,

Megha

Try including your Array variable in a session function.

EG:

exec { session =>
val **ContextPaths** =  Array("100.200.201", "100.200.2002", ..)
session.set("**ContextPaths**", **ContextPaths**)
}

Thanks Abhinav for a quick reply.
However, I cannot directly set the session attribute the way you mentioned since the method expects

  • set(key: String, value: Any): Session: add or replace an attribute
    Where infact I need to add an Array as an attribute.

Regards,
Megha

You can do it right before your for loop. Keep in mind you have to do the whole exec{=> session… function.

Got it working!!! :slight_smile: :slight_smile: :slight_smile:

This is what I do now before foreach loop

.exec(session => {

val ContextPaths = Array(“100.200.201”, “100.200.2002”, “100.200.2003”, “100.200.2007”, “100.200.2001”, “100.200.2004”, “100.200.2005”, “100.200.2008”, “100.200.2006”)

val newsession = session.set(“ContextPaths”, ContextPaths.toSeq)

println(newsession)

newsession

})

Thanks a lot Abhinav for your prompt help!

Regards,
Megha