Martin Blore
Martin Blore

Reputation: 2195

Gatling - Split Value In Check

I have a HTML element from a HTTP request like so:

<input type="radio" data-trip="0" id="fareRadioId_" name="pricefare" value="H15CNGE~SHATSA~220420150955~3332|H15CNGE~TSASHA~270420150715~338" class="pricefare" data-toggle="radio" checked="checked">

I'm used to pulling a value from a CSS select like so:

.check(css("input#fareRadioId_0.select_departure", "value").saveAs("departSellKey"))

But after I've selected the value in the element above which is "H15CNGE~SHATSA~220420150955~3332|H15CNGE~TSASHA~270420150715~338", I want to split it in to parts, with the split character being "|", and save the two parts in to session with 2 different names. Is this possible?

I've pretty new to Gatling and Scala so this is a little above my head at the moment. Any help would be greatly appreciated.

Upvotes: 2

Views: 2343

Answers (1)

millhouse
millhouse

Reputation: 10007

I'm not sure if you'll be able to save the two parts with different names, but it's fairly easy to perform the split and store the result as a Seq, after which you can access it with indexes etc.

What you need to do is insert a suitable transformer into your check:

.check(css("...").transform(_.split('|').toSeq).saveAs("sellKeys"))

This takes the String from the css() expression, does a split() on it (which creates an Array[String]) and then converts it to a Seq because they're nicer to work with :-)

The Seq is then saved into sellKeys, so later on you can do things like (silly example):

 .exec( session => {
    val keys = session("sellKeys").as[Seq[String]]
    println(s"keys are ${keys.mkString(" and ")}")
    println(s"the first key is ${keys.head}") 
    session 
    }
)

Output:

keys are H15CNGE~SHATSA~220420150955~3332 and H15CNGE~TSASHA~270420150715~338
the first key is H15CNGE~SHATSA~220420150955~3332

Upvotes: 2

Related Questions