saveAs("var")

9,098 views
Skip to first unread message

David T

unread,
May 7, 2012, 8:39:32 PM5/7/12
to Gatling User Group
Hi,

Is there a way to use a variable that was captured with RegEx and
saved using saveAs?
Here is my module:
object GetInfo {
val scnOne = scenario("Get Book Size and Id")

.exec(
http("book info")
.get("/books/custom")
.check(status.is(200))
.check(
regex("""/custom/stylecatalog(.+)=USER_CUSTOM""")
.findAll
.saveAs("bookSelectionUrl"))
)
}

There are multiple matches and they are being saved as List values
into variable bookSelectionUrl.
How can I print this values out? Or how can I choose one of them?
What I need, I need to be able to select 4th selection?

Thank You,
David

Stéphane Landelle

unread,
May 8, 2012, 3:17:15 AM5/8/12
to gat...@googlegroups.com
Gosh, how could we not document this?

Here is the doc, thanks for reporting:

Steph

2012/5/8 David T <dtis...@gmail.com>

David T

unread,
May 9, 2012, 5:37:48 PM5/9/12
to Gatling User Group
That helps!
Thank You!

On May 8, 12:17 am, Stéphane Landelle <slande...@excilys.com> wrote:
> Gosh, how could we not document this?
>
> Here is the doc, thanks for reporting:https://github.com/excilys/gatling/wiki/Session#wiki-el
>
> Cheers,
>
> Steph
>
> 2012/5/8 David T <dtishk...@gmail.com>

Remi

unread,
Aug 2, 2012, 11:48:11 AM8/2/12
to gat...@googlegroups.com
Hello David,

i tried several attempts to make it work, i have a similar problem.

can you post your complete code? with the explanation of the document

thanks,
Remi

Stéphane Landelle

unread,
Aug 2, 2012, 12:02:11 PM8/2/12
to gat...@googlegroups.com
You can also post a gist of what you're trying to do and we can help you fix it.

2012/8/2 Remi <reemste...@gmail.com>

Remi

unread,
Aug 3, 2012, 6:56:46 AM8/3/12
to gat...@googlegroups.com
ok here is my post:

.exec(
                http("randomPage")
                    .get("/zoek/dossier?offset="+ test2)
                    .check(regex("""a href="/video/(.*)">Video""")
                    .findAll
                    .saveAs("idnr"))
                )


i now want to readout idnr. this should contain multiple values, because the regex matches multiple times.
according to the explanation, this should be a session. but i cannot read it out or display it via println.
hopefully you can help me.

Stéphane Landelle

unread,
Aug 3, 2012, 7:14:36 AM8/3/12
to gat...@googlegroups.com
The easiest way is to use some ELs:

for example, you can write:
.exec(
  http("foo").get("bar${idnr(0)}baz")
)

you can even use Session functions:

.exec((session: Session) =>
  println(session.getAttribute("idnr"))
)

Cheers,

Stéphane

2012/8/3 Remi <reemste...@gmail.com>

Remi

unread,
Aug 3, 2012, 7:23:26 AM8/3/12
to gat...@googlegroups.com
I don't get it, how can i use it now in my code?

how do i store the outcome of my regex in a variable?

i used in my code .saveAs("idnr")

Stéphane Landelle

unread,
Aug 3, 2012, 7:43:56 AM8/3/12
to gat...@googlegroups.com
Yep, you've used saveAs("idnr"), which means that you've saved the outcome of your regex (in your case, a List[String] as you used findAll) into the Session as an attribute named "idnr".

You can then retrieve this attribute either with an EL inside a String parameter, or programmatically with a Session function.

Do you understand now?

Steph

2012/8/3 Remi <reemste...@gmail.com>

Nicolas Rémond

unread,
Aug 3, 2012, 8:05:41 AM8/3/12
to gat...@googlegroups.com
Not related but I don't think your regex will work, here is my suggestion : 
    .check(regex("""a href="/video/([^"]*)">Video""")

Remi

unread,
Aug 3, 2012, 9:07:58 AM8/3/12
to gat...@googlegroups.com
i am not very familiar with gatlin (yet). don't know what EL stands for.
maybe you can help me by writing the solution for my problem.

i now have:

.exec(
                http("randomPage")
                    .get("/zoek/dossier?offset="+ test2 + "&sorteerVolgorde=relevantie&trefwoord=leraar")

                    .check(regex("""a href="/video/(.*)">Video""")
                    .findAll
                    .saveAs("idnr"))
                )


and then there must be something like:


.exec((session: Session) =>
  println(session.getAttribute("idnr"))
)

but this doesnt work

Stéphane Landelle

unread,
Aug 3, 2012, 9:12:34 AM8/3/12
to gat...@googlegroups.com
EL means Expression Language. Gatling EL syntax is very similar to the Java one.

Could you explain what you mean by "it doesn't work", please? Do you get an exception? Except this EL problem, does your simulation work fine?
Could you post a Github gist with your whole Simulation?

Steph

2012/8/3 Remi <reemste...@gmail.com>

Remi

unread,
Aug 3, 2012, 9:16:46 AM8/3/12
to gat...@googlegroups.com
check pastebin:

http://pastebin.com/fzkAbnWX

there is the complete code

Remi

unread,
Aug 3, 2012, 9:18:00 AM8/3/12
to gat...@googlegroups.com
simulation works fine except this EL problem.

Remi

unread,
Aug 3, 2012, 9:28:33 AM8/3/12
to gat...@googlegroups.com
this is the error

C:\Projecten\testtools\gatling-charts-highcharts-1.2.5\user-files\simulations\Simulation.scala:84: error: not found: value session
                .exec((session: Session)
                       ^
one error found

        at com.excilys.ebi.gatling.app.Gatling$$anonfun$compile$1.apply(Gatling.scala:182)
        at com.excilys.ebi.gatling.app.Gatling$$anonfun$compile$1.apply(Gatling.scala:172)
        at com.excilys.ebi.gatling.core.util.IOHelper$.use(IOHelper.scala:24)
        at com.excilys.ebi.gatling.app.Gatling.compile(Gatling.scala:172)
        at com.excilys.ebi.gatling.app.Gatling.start(Gatling.scala:103)
        at com.excilys.ebi.gatling.app.Gatling$.main(Gatling.scala:69)
        at com.excilys.ebi.gatling.app.Gatling.main(Gatling.scala)
Druk op een toets om door te gaan. . .

Stéphane Landelle

unread,
Aug 3, 2012, 9:50:09 AM8/3/12
to gat...@googlegroups.com
My fault, here is the correct Session function:

.exec((session: Session) => {

println(session.getAttribute("idnr"))

session

})


2012/8/3 Remi <reemste...@gmail.com>

Remi

unread,
Aug 6, 2012, 8:16:22 AM8/6/12
to gat...@googlegroups.com
it's still not working.

do u know how i can print the result of a http request?

Stéphane Landelle

unread,
Aug 6, 2012, 8:23:13 AM8/6/12
to gat...@googlegroups.com
Have a look at the logback.xml file (located in the conf directory).

You can lower to root level to DEBUG.

Cheers,

Stéphane

2012/8/6 Remi <reemste...@gmail.com>

Remi

unread,
Aug 12, 2012, 7:06:13 AM8/12/12
to gat...@googlegroups.com
Hello Stéphane,

lowering the root level to DEBUG doesn't print the value of the session attribute idnr
actually my regex works, only problem it's retrieving the first match.

to make the performance test more realistic i want it to return a random match.

so i am using this session attribute idnr now as ${idnr} in a getrequest

but when i use it like ${idnr[3]}
it gives an error back.

what am i doing wrong?




Op maandag 6 augustus 2012 14:23:13 UTC+2 schreef Stéphane Landelle het volgende:

Remi

unread,
Aug 12, 2012, 7:22:13 AM8/12/12
to gat...@googlegroups.com
information below is not complete correct. i now see that my session attribute is printed.

but now it's printing the first match. how can i make that random?

Stéphane Landelle

unread,
Aug 12, 2012, 7:39:28 AM8/12/12
to gat...@googlegroups.com
Hello Remi,

Lowering the root level to DEBUG will only print the HTTP requests and responses, not the Session content.
Maybe we should add some DEBUG and TRACE logs to help debugging.

Until then, you can debug your Simulation with a Session function:

Regarding your wanting to select a random entry among a check result, you have to:
  1. ensure you capture ALL the results of your regex, not only the first one (which is the default): use findAll: https://github.com/excilys/gatling/wiki/Checks#wiki-extracting
  2. use a transform step in your check in order to save only one of the results: https://github.com/excilys/gatling/wiki/Checks#wiki-transforming
So, you'll have something like this:

val randomData = new org.apache.commons.math3.random.RandomDataImpl

val selectRandomElementInList = (list: List[String]) => {
  val randomIndex = randomData.nextInt(0, list.size - 1)
  list(randomIndex)
}

...
...
check(regex("XXXXXX").findAll.transform(selectRandomElementInList).saveAs("idnr"))

And voila, you can use ${idnr}.

Cheers,

Stéphane

2012/8/12 Remi <reemste...@gmail.com>

Remi

unread,
Aug 12, 2012, 2:49:09 PM8/12/12
to gat...@googlegroups.com
Hello Stephane,

thanks for your reply.
unfortunately i am getting an error:

Collecting simulations...
Exception in thread "main" java.lang.RuntimeException: Compilation failed:
C:\projecten\tools\gatlin\gatling-charts-highcharts-1.2.5\user-files\simulations\Simulation1.scala:54: error: type mismatch;
 found   : List[String] => String
 required: Seq[String] => ?
                .check(regex("""<a href="/video/([^"]*)""").findAll.transform(selectRandomElementInList).saveAs("idnr"))
                                                                              ^
one error found

do you know what's going wrong?




        at com.excilys.ebi.gatling.app.Gatling$$anonfun$compile$1.apply(Gatling.scala:182)
        at com.excilys.ebi.gatling.app.Gatling$$anonfun$compile$1.apply(Gatling.scala:172)
        at com.excilys.ebi.gatling.core.util.IOHelper$.use(IOHelper.scala:24)
        at com.excilys.ebi.gatling.app.Gatling.compile(Gatling.scala:172)
        at com.excilys.ebi.gatling.app.Gatling.start(Gatling.scala:103)
        at com.excilys.ebi.gatling.app.Gatling$.main(Gatling.scala:69)
        at com.excilys.ebi.gatling.app.Gatling.main(Gatling.scala)
Press any key to continue . . .

Stéphane Landelle

unread,
Aug 12, 2012, 3:16:34 PM8/12/12
to gat...@googlegroups.com
My fault.
As the compiler states, findAll returns a Seq[String], not a List[String].
You just have to change the selectRandomElementInList signature:

val selectRandomElementInList = (list: Seq[String]) => {

  val randomIndex = randomData.nextInt(0, list.size - 1)

  list(randomIndex)

}


Cheers,

Steph


2012/8/12 Remi <reemste...@gmail.com>

Remi

unread,
Aug 12, 2012, 5:28:51 PM8/12/12
to gat...@googlegroups.com
It looks like it works.

thanks a lot!
i will check it in the morning again.

Sathiyamoorthy G N

unread,
Nov 9, 2017, 5:21:01 AM11/9/17
to Gatling User Group
Can you please help...? 
I used below code.

check(bodyString.saveAs("BODY"))

how can i print BODY in console window or how can i verify the value that stored in BODY?

Thanks & Regards,
Sathya
Reply all
Reply to author
Forward
0 new messages