I'm using Gatling to simulate user interactions on a website, and I have a scenario where I need to perform a login step and then proceed with other requests like "Add Listing," "Update Listing," etc. However, I want to perform the login step only once for all users in the simulation and reuse the authentication token for subsequent requests.
Here's my current Gatling simulation code:
public class PerfCopy extends Simulation {
String csrfToken = null;
String isLogin = "0";
// Run the scenario
public OcusellPerfCopy() {
HttpProtocolBuilder httpProtocol = http
.baseUrl("https://dev.com/")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate, br")
.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0");
// Define the scenario
ScenarioBuilder scn = scenario("Scenario")
.exec(session -> session.set("isLogin", "0"))
.exec(http("CSRF_TOKEN")
.get("/login")
.check(status().is(200))
.check(regex("<meta name=\"csrf-token\" content=\"(.*?)\"").saveAs("csrfToken")))
.exec(session -> {
csrfToken = session.getString("csrfToken");
System.out.println("CSRF Token: " + csrfToken);
return session;
})
//Login Form
.exec(http("Login")
.post("/login")
.headers(Map.of("Content-Type", "application/x-www-form-urlencoded"))
.formParam("_token", "#{csrfToken}")
.formParam("email", "[email protected]")
.formParam("password", "test@1234")
.check(status().is(200)))
//Add Listing
.exec(http("GET Add Listing")
.get("/addProperty")
.queryParam("listing_owner", "152")
.queryParam("address-autocomplete-street-number", "")
.queryParam("address-autocomplete-street-name", "")
.queryParam("address-autocomplete-street-suffix", "")
.queryParam("address-autocomplete-street-dir", "")
.queryParam("address-autocomplete-city", "")
.queryParam("address-autocomplete-state", "")
.queryParam("address-autocomplete-postal-code", "")
.queryParam("address-autocomplete-postal-code-suffix", "")
.queryParam("choose_mls[]", "Georgia")
.check(status().is(200))
.check(regex("<a class=\"\" href=\"https://dev.com/update/(\\d+)\">").saveAs("listingID")))
.exec(session -> {
String listingID = session.getString("listingID");
System.out.println("Listing ID: " + listingID);
return session;
})
//Info section
.exec(http("POST Info")
.post("/update/${listingID}")
.headers(Map.of(
"Content-Type", "application/json",
"X-CSRF-TOKEN", "#{csrfToken}"
))
.body(ElFileBody("property_info.json"))
.check(status().is(200))
.check(bodyString().saveAs("updateListingResponse")))
.exec(session -> {
String responseBody = session.getString("updateListingResponse");
System.out.println("POST updateListingResponse Listing Media Response Body: " + responseBody);
return session;
});
// Define the number of users to simulate
int numUsers = 10;
setUp(scn.injectOpen(rampUsers(numUsers).during(1)).protocols(httpProtocol));
}
}
When I run this simulation I see token generated multiple time and listing id is getting null , what wrong I am doing here