Search code examples
scalagatlingscala-gatling

What is the difference between using HttpRequestBuilder vs ChainBuilder in exec() method in Gatling?


I have this example from the Gatling Academy, which I modified slightly:

  object Checkout {
    def viewCart: HttpRequestBuilder = {
        http("Load Cart Page")
          .get("/cart/view")
          .check(status.is(200))
    }

    def completeCheckout: ChainBuilder = {
      exec(
        http("Checkout Cart")
          .get("/cart/checkout")
          .check(status.is(200))
          .check(substring("Thanks for your order! See you soon!"))
      )
    }
  }

  val scn = scenario("RecordedSimulation")
    .exec(CmsPages.homepage)
    .pause(PAUSE_TIME)
    .exec(CmsPages.aboutUs)
    .pause(PAUSE_TIME)
    .exec(Catalog.Category.view)
    .pause(PAUSE_TIME)
    .exec(Catalog.Product.add)
    .pause(PAUSE_TIME)
    .exec(Checkout.viewCart)
    .pause(PAUSE_TIME)
    .exec(http("Login User")
      .post("/login")
      .formParam("_csrf", "${csrfValue}")
      .formParam("username", "user1")
      .formParam("password", "pass"))
    .pause(PAUSE_TIME)
    .exec(Checkout.completeCheckout)

  setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)

As you can see viewCart has HttpRequestBuilder type, whereas completeCheckout has ChainBuilder type. Both of them are working, the test is passing. I don't quite understand what is the difference between using ChainBuilder and HttpRequestBuilder. Which one is preferable and why?

Thank you.


Solution

  • ChainBuilder is a type for the chain of requests. You can add one request or several. Also, you can add requests with different protocols

    val myChain: ChainBuilder =
        exec()
        .exec()
        .exec()
    

    HttpRequestBuilder - it's only about one http request.


    Advise: don't use def for requests, use valinstead