Unable to fetch current session value inside Repeat loop

Hi All,

I have to write external log file based on the certain condition.
I need to execute the same request multiple times and if it matches the condition set it should write to external log file or else nothing should be done.

.repeat(11) {
.get(Url + “/captured/S/2020-12-10/A/${Json_url}”)

.doIf(session => session(“C_check”).as[String].length() == 4)(exec(session => {
import java.io._

val log = new File(“A.csv”)
val fw = new FileWriter(log, true)
val bw = new BufferedWriter(fw)
bw.write(session(“PromotionId”).as[String] + “,”)



But the C_check session value in doIf is fixed with the value from the first iteration and the same is used for all the 11 times. But the overall execution is correctly happening. With the OK and KO.

When I debugged and printed the value in the console , the first value of C_check is used for all the iterations and hence log is all the time.

session => session(“C_check”).as[String] - I hope it should give the current session value only. Sorry If I am wrong

Kindly help on this

Aravind G

Check if request A has caching headers that would cause it to be skipped.

Hi Stephane,

Thanks for the solution.
To eliminate that doubt, I just added .disableCaching to the http protocol config definition and re-run the script.
I enabled log back and I could see the response is coming correctly.
But still the session value from first iteration is set for all the remaining iteration.

.exec(session => if (session.contains(" C_check ")) session else session.markAsFailed)

I tried using the above one but it’s not successful since it completely stops the scenario when the condition is true.
Is there any way in Gatling to continue to the next iteration inside a loop even if the condition fails.

PS: I am using the latest gatling 3.5 version


Oh wait, what are you doing exactly? Is feed(file) actually pointing to the same file you’re writing to? This can’t work.

Hi Stephane,

No , I am not writing to the same file.

val file = csv(“D:/WOW_Gatling_POC/WOW_Gatling_POC/src/test/resources/CSV_FILES/Json_file.csv”) // this is the feed file location. I declared this at the start of the Object file.

The above code worked fine for me for some other request.

The difference in both the response are

  1. For working request .check(xpath("//Integrity").find.saveAs(“C_check”)) the two responses are one with the Integrity as true and other as false
    Log written successfully when I give this condition .doIf(session => session(“C_check”).as[String].length==4

  2. For not working request .check(xpath("//Integrity").find.saveAs(“C_check”)) the response are the one Integrity as true and for the second response variant the field itself is absent.

Log written for all the iterations when I give this condition .doIf(session => session(“C_check”).as[String].length==4

I guess the absence of integrity field in one of the response variant is reason for this.

Could you please tell me how to handle it?

I tried with
.doIf(session => session(“C_check”).as[String].NotNull – Not working

Aravind G

Hi Stephane,

Just wanted to wish you and whole Gatling community Advance Happy Christmas and new Year wishes. Hope in 2021 the usage of Gatling tool and its growth will be a huge one than 2020.