javascripttestingperformance-testinggatling

How to insert the request body?


Help, guys, I have been assigned to research gatling with javascript for performance testing, but I am quite junior and struggling to even make a login request from a project.

This is the documentation: https://docs.gatling.io/reference/script/protocols/http/request/#request-name
There is also a nice video by a guy here, that I used for a double check, if I missed some prerequisite: https://www.youtube.com/watch?v=s4WeRrCAk-8

So far this is the solution I am coming up with:

import { atOnceUsers, scenario, simulation } from "@gatling.io/core";
import { http, status, StringBody } from "@gatling.io/http";

export default simulation((setUp) => {
    const httpProtocol = 
    http.baseUrl('https://example.test.com')
      .acceptHeader('*/*')
      .contentTypeHeader('application/json');

    const loginAndLogout = scenario('Login and logout')
      .exec(http('Login').post('/login')
      .body(StringBody("{ \"username\": \"testUser\", \"password\": \"testPass\", \"channel\": \"web\" }"))
      .check(status().is(200))  // Check for a 200 OK response
);

      setUp(loginAndLogout.injectOpen(atOnceUsers(1))).protocols(httpProtocol);
});

A simple get request is working, but a post request with a body is just not doing the job for me. I just get:

"[ERROR] i.g.a.Gatling$ - Run crashed org.graalvm.polyglot.PolyglotException: TypeError: undefined is not a function."

The error itself does not speak too much to me.

I have done previously a full project with "k6", but I am clueless here. Thanks.


Solution

  • The error lies in the imports:

    import { atOnceUsers, scenario, simulation } from "@gatling.io/core";
    import { http, status, StringBody } from "@gatling.io/http";
    

    StringBody needs to be imported from core, not http:

    import { StringBody, atOnceUsers, scenario, simulation } from "@gatling.io/core";
    import { http, status } from "@gatling.io/http";