still struggling with cookies...

29 views
Skip to first unread message

Stef Joosten

unread,
Jan 1, 2022, 9:59:42 AM1/1/22
to Gatling User Group
I want to use a cookie from my session in my scenario. For this purpose I get the cookie (using `getCookieValue`) and use it further down the line as `session(PHPSESSID)`.
Here is a fragment of my scenario.
```
.pause(2)
.exec(getCookieValue(CookieKey("PHPSESSID")))
.exec { session =>
http("Fill Userid/Password")
.patch("/api/v1/resource/SESSION/1")
// .headers(headers_sessID)
.body(StringBody(""" [{ "op":"replace",
"path":"/Login/"""+session("PHPSESSID")+"""/Login/"""+session("PHPSESSID")+"""/Userid",
"value":"peter"
},
{ "op":"replace",
"path":"/Login/"""+session("PHPSESSID")`
+"""/Login/"""+session("PHPSESSID")+"""/Password",
"value":"stupidpassword"
},
{ "op":"replace",
"path":"/Login/"""+session("PHPSESSID")+"""/Login/"""+session("PHPSESSID")+"""/Login/property",
"value":true
}] """)).asJson
session}
.pause(2)
```
My problem is that this does not work. the step "Fill Userid/Password" is not being executed.
Maybe that is because it is a function?
However, If I do not define the exec step as a function, it cannot find `session("PHPSESSID")` because `session` is not defined.

I'd get grateful for a good explanation that drives me toward a solution.

Stef Joosten

unread,
Jan 2, 2022, 7:38:08 AM1/2/22
to Gatling User Group
I found an answer, but it does not use the solution suggested by the Gatling documentation, which is `exec(getCookieValue(CookieKey("PHPSESSID")))`.
So it is not really satisfactory, but it works...  Here is the code fragment that works:
```
.pause(2)
.exec(
http("Display the login screen")
.get("/api/v1/resource/SESSION/1/Login")
.check(jsonPath("$._id_").find.saveAs("sessId"))
)
.pause(2)
.exec (
http("Fill Userid/Password")
.patch("/api/v1/resource/SESSION/1")
.body(StringBody(""" [{ "op":"replace",
"path":"/Login/${sessId}/Login/${sessId}/Userid",
"value":"peter"
},
{ "op":"replace",
"path":"/Login/${sessId}/Login/${sessId}/Password",
"value":"stupidpassword"
},
{ "op":"replace",
"path":"/Login/${sessId}/Login/${sessId}/Login/property",
"value":true
}] """)).asJson
)
.pause(2)
```
So here I did not get the session id from the cookie PHPSESSID, but I got it from filtering it out from a response message in the step "Display the login screen".
Op zaterdag 1 januari 2022 om 15:59:42 UTC+1 schreef Stef Joosten:

Stéphane LANDELLE

unread,
Jan 2, 2022, 4:26:15 PM1/2/22
to gat...@googlegroups.com
.pause(2)
.exec(getCookieValue(CookieKey("PHPSESSID")))
.exec { session =>
http("Fill Userid/Password")
.patch("/api/v1/resource/SESSION/1")
// .headers(headers_sessID)
.body(StringBody(""" [{ "op":"replace",
"path":"/Login/#{PHPSESSID}/Login/#{PHPSESSID}/Userid",
"value":"peter"
},
{ "op":"replace",
"path":"/Login/#{PHPSESSID}/Login/#{PHPSESSID}/Password",
"value":"stupidpassword"
},
{ "op":"replace",
"path":"/Login/#{PHPSESSID}/Login/#{PHPSESSID}/Login/property",
"value":true
}] """)).asJson
session}
.pause(2)



--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/59d7f772-ae94-4508-8f45-590e3f8f1784n%40googlegroups.com.

Stef Joosten

unread,
Jan 6, 2022, 6:51:57 AM1/6/22
to Gatling User Group
Thank you, Stéphane. I tried your solution because it looks like "the right way to do things". However, I got back the following results:
```
================================================================================
2022-01-06 12:19:15                                          26s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=12     KO=3     )
> Home                                                     (OK=1      KO=0     )
... (skipping some superfluous lines)
> Display the login screen                                 (OK=1      KO=0     )
> Display MyScripts                                        (OK=0      KO=1     )
> Ask for a New Script                                     (OK=0      KO=1     )
> Show the Atlas                                           (OK=0      KO=1     )
---- Errors --------------------------------------------------------------------
> status.find.in(200,201,202,203,204,205,206,207,208,209,304), f      3 (37,50%)
ound 401
> Display New Script: Failed to build request: No attribute name      1 (12,50%)
d 'scriptId' is defined
> Type New Script: Failed to build request: No attribute named '      1 (12,50%)
scriptId' is defined
> Compile the Script: Failed to build request: No attribute name      1 (12,50%)
d 'scriptId' is defined
> Functional Specification: Failed to build request: No attribut      1 (12,50%)
e named 'scriptId' is defined
> Prototype: Failed to build request: No attribute named 'script      1 (12,50%)
Id' is defined

---- RAPtest -------------------------------------------------------------------
[##########################################################################]100%
          waiting: 0      / active: 0      / done: 1    
================================================================================

Simulation RAPtest completed in 26 seconds
```
With the solution that works for me (see above in the mail trail), I get the expected results, which are:
```
================================================================================
2022-01-06 12:25:11                                         56s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=21     KO=0     )
> Home                                                     (OK=1      KO=0     )
... (skipping some superfluous lines)
> Display the login screen                                 (OK=1      KO=0     )
> Fill Userid/Password                                     (OK=1      KO=0     )
> Display MyScripts                                        (OK=1      KO=0     )
> Ask for a New Script                                     (OK=1      KO=0     )
> Display New Script                                       (OK=1      KO=0     )
> Type New Script                                          (OK=1      KO=0     )
> Compile the Script                                       (OK=1      KO=0     )
> Functional Specification                                 (OK=1      KO=0     )
> Prototype                                                (OK=1      KO=0     )
> Show the Atlas                                           (OK=1      KO=0     )

---- RAPtest -------------------------------------------------------------------
[##########################################################################]100%
          waiting: 0      / active: 0      / done: 1    
================================================================================

Simulation RAPtest completed in 56 seconds
```
The only difference is the change you are suggesting.
Although it seems the right thing to do, somehow it skips the step "Fill Userid/Password". As a consequence, the login procedure fails and all subsequent steps fail. Can you explain this? If you want I can make this reproducible for you.
Op zondag 2 januari 2022 om 22:26:15 UTC+1 schreef Stéphane Landelle:
Reply all
Reply to author
Forward
0 new messages