SaveAs with println

I’m trying to find the contents of elements in my page and print them out to the console.

This is what I’ve tried (I’m using Gatling 2 by the way) based on other posts I’ve seen here. I’m just testing this on a simple element in this example to try to get it working:

.exec(http(“request_20”)
.get("""/dj_sql_11x//Main/portal/lists/index.cfm?list=tasks""")
.check(css(".listPageTableCell").findAll.saveAs(“tasks”))
.headers(headers_3))
.exec(session => {
println(session(“tasks”));
})

I am getting a type mismatch error. I have also tried the session variable like this: println(session(tasks))

Which says that tasks isn’t a value…

Any help on finding out how to print the contents of elements out to the console would be very much appreciated.

Thank you,

–Matt Royer

type mismatch error

Which one? Where?

Ho, yes.

See Session manipulation:
https://github.com/excilys/gatling/wiki/Structure-Elements

The signature of this kind of exec is exec(Session => Session) (actually Session => Validation[Session] but the first form will work too), so you need to return a session, even if it’s the original one.

exec { session =>
println(session)
session
}

Great! Thank you! I got that working.

I do have another question though. Here is the code I am actually using:

Programmatically:

session(“tasks”) returns an Attribute object that wraps the actual value.

Ue one of the extract methods (https://github.com/excilys/gatling/wiki/Gatling%202#wiki-session) in order to get the ArrayBuffer in your case, then you can get a value like this: buffer(rank)

Gatling EL:

https://github.com/excilys/gatling/wiki/Session#wiki-el

“tasks(1)”

I’m using the EL to select the element I want with “tasks(0)”.

But I don’t understand the “extract method” in that link. Do I need to transform the “tasks” into an array buffer or something with:

println(session(“tasks(0)”).as[ArrayBuffer[String]])

I guess I don’t get how to get at the underlying values.

I'm using the EL to select the element I want with "tasks(0)".

But I don't understand the "extract method" in that link.

"extract method": as, asOption, or validate

Do I need to transform the "tasks" into an array buffer or something with:

println(session("tasks(0)").as[ArrayBuffer[String]])

println(session("tasks").as[ArrayBuffer[String]](0))

Don't try to mix EL and programming approaches, they are mutually exclusive.

Thanks again for helping me. Sorry I’m not catching on quickly.

I changed my code to your suggestion:

ArrayBuffer is not automatically imported. Just use Seq instead.

Ahhh… Thank you so much! That’s exactly what I needed.

Thanks again,

–Matt

Hi,

I’m new to Scala/Gatling and was trying to print a session variable. I’m using Gatling 2.2.2. I imported ArrayBuffer before trying the belo.

println(session(“abcd”).asArrayBuffer[String])

I was gettting the error " type mismatch;
found : Int(1)
required: io.gatling.commons.NotNothing[scala.collection.mutable.ArrayBuffer[String]]"

Can you please help?

Regards,
Prasanth.

For some dark reasons I won’t elaborate (implicit parameter), don’t use a one-liner.

val abcd = session(“abcd”).as[ArrayBuffer[String]]

println(abcd(1))

Thanks Stephane,

There’s no compilation error now, however, there’s a run time error now. But never mind, I managed to display it with session.get(“abcd”).asOption[String]

Thanks,
Prasanth.

Hi Stéphane

I wish to use below randomValue to generate email Ids for registration.

val randomSeq = exec(session => {
session.set(“randomValue”,ThreadLocalRandom.current.nextInt(2000).toString + System.currentTimeMillis.toString )
})

val RegisterFlow =
scenario(“Registration”)
.exec { session =>
println(" Random User → " + session.get(“randomValue”).asOption[Int])
session
}

.exec(http(“RegisterAccount”)
.post("/xxx/account")
.headers(headers_2) .body(StringBody("""{“preferredName”:“perftest_${randomValue}”,“familyName”:“perftest_${randomValue}”,“email”:“perftest_${randomValue}@perftest.com”,“password”:“Password1”}""")).asJSON
)

I am getting output as none. Please let me know how to print this value and use it another request.

Regards,
Bharat