-1

I wanted to write a session attribute to a file only if the response returned the string 'Success'.

I'm trying to do this:

.check(substring("success").onSuccess(writeThisToFile(<my variable here>))

I see that this is not valid as the IDE says. Is there any way to call a function depending on the outcome of response check?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

2 Answers2

1

To do that you can use method .transform() on bodyString. It takes function parameter of type String => T or (String, Session) => T and applies it on response body, so in Your case it would look like:

.check(
    bodyString.transform(
        (body: String, session: Session) => {
            if(body.contains("success")){
                File("/path/to/file")
                    .createFile()
                    .appendAll(session("attributeName").as[String])
            }
        }
    )
)

-- Edited --

So to save part of request body to session what you have to do is to generate that part separately, save into session attribute and use when creating body. For example lets assume that you want to send JSON containing 2 fields: some constant value + current timestamp, and then save timestamp to file if response contains "success":

val exampleScenario =  scenario("Example")
    .exec(session => {
        session.set("timestamp", System.currentTimeMillis)
    })
    .exec(http("Send data")
        .post("http://example.com")
        .body(StringBody("""{"constant":123, "timestamp": ${timestamp}}"""))
        .asJSON
        .check(
            bodyString.transform(
                (body: String, session: Session) => {
                    if(body.contains("success")){
                        File("/path/to/file")
                            .createFile()
                            .appendAll(session("timestamp").as[String])
                    }
                }
            )
        )
    )
Mateusz Gruszczynski
  • 1,421
  • 10
  • 18
  • Thank you, but in my case, the attribute that I wanted to write to the file is in the Request, not in the response.Sorry if it is confusing. To sum it up: Call a function that stores ${variable} into a file ONLY if response has the string "success" – M S Puranik Jun 13 '18 at 14:40
  • So question is not how to write session attribute to file but how to save part of request body as session attribute? If so, probably easiest way would be to generate body in session function, save it to one session attribute, then save part of request you want to save to another attribute (you may skip this part if you are using maps or case objects as body) than when building request body read data from session and at the end verify response and save attribute to file. All assuming that you are using certain implementation. It would be much easier if you could add whole request not only check – Mateusz Gruszczynski Jun 13 '18 at 14:56
  • How to save part of request body as session attribute? Yes, that's what I wanted to ask. And this looks super complicated :( Can you please point me to section of Gatling doc where it explains how to generate body in session function? – M S Puranik Jun 13 '18 at 15:36
  • It will be easier to add example, because it is not explained in single part of docs. I'll edit my response as it wont fit in comment. – Mateusz Gruszczynski Jun 14 '18 at 11:30
-1

This must work

 .asLongAs(session => ( YOUR CONDITION ) ) {
            exec(    http("Poll")  ) 
  }
user666
  • 1,104
  • 12
  • 20