# SaveAs with println

**URL:** <https://community.gatling.io/t/saveas-with-println/733>\
**Category:** Gatling (Open-Source)\
**Created:** [December 18, 2013, 12:14am UTC](https://community.gatling.io/t/saveas-with-println/733 "2013-12-18T00:14:30Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Royer](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Matt\_Royer](https://community.gatling.io/u/Matt_Royer)\
**Post date:** [December 18, 2013, 12:14am UTC](https://community.gatling.io/t/saveas-with-println/733/1 "2013-12-18T00:14:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [December 18, 2013, 6:59am UTC](https://community.gatling.io/t/saveas-with-println/733/2 "2013-12-18T06:59:20Z")

</div>

> type mismatch error

Which one? Where?

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [December 18, 2013, 7:01am UTC](https://community.gatling.io/t/saveas-with-println/733/3 "2013-12-18T07:01:58Z")

</div>

Ho, yes.

See Session manipulation:  
[https://github.com/excilys/gatling/wiki/Structure-Elements](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  
}

---

<div class="post-metadata">

**Author:** ![Matt\_Royer](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Matt\_Royer](https://community.gatling.io/u/Matt_Royer)\
**Post date:** [December 19, 2013, 11:42pm UTC](https://community.gatling.io/t/saveas-with-println/733/4 "2013-12-19T23:42:33Z")

</div>

Great! Thank you! I got that working.

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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [December 20, 2013, 7:03am UTC](https://community.gatling.io/t/saveas-with-println/733/5 "2013-12-20T07:03:23Z")

</div>

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](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](https://github.com/excilys/gatling/wiki/Session#wiki-el)

“tasks(1)”

---

<div class="post-metadata">

**Author:** ![Matt\_Royer](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Matt\_Royer](https://community.gatling.io/u/Matt_Royer)\
**Post date:** [December 20, 2013, 6:56pm UTC](https://community.gatling.io/t/saveas-with-println/733/6 "2013-12-20T18:56:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [December 20, 2013, 10:03pm UTC](https://community.gatling.io/t/saveas-with-println/733/7 "2013-12-20T22:03:07Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Matt\_Royer](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Matt\_Royer](https://community.gatling.io/u/Matt_Royer)\
**Post date:** [December 30, 2013, 3:09pm UTC](https://community.gatling.io/t/saveas-with-println/733/8 "2013-12-30T15:09:19Z")

</div>

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

I changed my code to your suggestion:

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [December 30, 2013, 3:42pm UTC](https://community.gatling.io/t/saveas-with-println/733/9 "2013-12-30T15:42:25Z")

</div>

ArrayBuffer is not automatically imported. Just use Seq instead.

---

<div class="post-metadata">

**Author:** ![Matt\_Royer](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Matt\_Royer](https://community.gatling.io/u/Matt_Royer)\
**Post date:** [December 30, 2013, 4:16pm UTC](https://community.gatling.io/t/saveas-with-println/733/10 "2013-12-30T16:16:24Z")

</div>

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

Thanks again,

–Matt

---

<div class="post-metadata">

**Author:** ![Prasanth\_Joy](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Prasanth\_Joy](https://community.gatling.io/u/Prasanth_Joy)\
**Post date:** [July 12, 2016, 4:18am UTC](https://community.gatling.io/t/saveas-with-println/733/11 "2016-07-12T04:18:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [July 13, 2016, 6:36am UTC](https://community.gatling.io/t/saveas-with-println/733/12 "2016-07-13T06:36:16Z")

</div>

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))

---

<div class="post-metadata">

**Author:** ![Prasanth\_Joy](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Prasanth\_Joy](https://community.gatling.io/u/Prasanth_Joy)\
**Post date:** [July 13, 2016, 7:31am UTC](https://community.gatling.io/t/saveas-with-println/733/13 "2016-07-13T07:31:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bharat\_Goyal](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Bharat\_Goyal](https://community.gatling.io/u/Bharat_Goyal)\
**Post date:** [November 3, 2016, 9:45am UTC](https://community.gatling.io/t/saveas-with-println/733/14 "2016-11-03T09:45:47Z")

</div>

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
