Gatling switch protocols during scenario
Asked Answered
R

2

14

I'm trying to create a Gatling scenario which requires switching the protocol to a different host during the test. The user journey is

https://example.com/page1
https://example.com/page2
https://accounts.example.com/signin
https://example.com/page3

so as part of a single scenario, I need to ether switch the protocol defined in the scenario set up, or switch the baseUrl defined on the protocol but I can't figure out how to do that.

A basic scenario might look like

package protocolexample

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class Example extends Simulation {
  val exampleHttp = http.baseURL("https://example.com/")
  val exampleAccountsHttp = http.baseURL("https://accounts.example.com/")

  val scn = scenario("Signin")
    .exec(
      http("Page 1").get("/page1")
    )
    .exec(
      http("Page 2").get("/page2")
    )
    .exec(
      // This needs to be done against accounts.example.com
      http("Signin").get("/signin")
    )
    .exec(
      // Back to example.com
      http("Page 3").get("/page3")
    )

  setUp(
    scn.inject(
      atOnceUsers(3)
    ).protocols(exampleHttp)
  )
}

I just need to figure out how to ether switch the host or protocol for the 3rd step. I know I can create multiple scenarios but this needs to be a single user flow across multiple hosts.

I've tried directly using the other protocol

exec(
  // This needs to be done against accounts.example.com
  exampleAccountsHttp("Signin").get("/signin")
)

which results in

protocolexample/example.scala:19: type mismatch;
 found   : String("Signin")
 required: io.gatling.core.session.Session
       exampleAccountsHttp("Signin").get("/signin")

and also changing the base URL on the request

exec(
  // This needs to be done against accounts.example.com
  http("Signin").baseUrl("https://accounts.example.com/").get("/signin")
)

which results in

protocolexample/example.scala:19: value baseUrl is not a member of io.gatling.http.request.builder.Http
Radmilla answered 4/4, 2016 at 12:45 Comment(1)
@Meiko Hi, yes sorry for the delay. Marked as acceptedRadmilla
R
19

You can use an absolute URI (inclusive protocol) as a parameter for Http.get, Http.post and so on.

class Example extends Simulation {
  val exampleHttp = http.baseURL("https://example.com/")
  val scn = scenario("Signin")
    .exec(http("Page 1").get("/page1"))
    .exec(http("Page 2").get("/page2"))
    .exec(http("Signin").get("https://accounts.example.com/signin"))
    .exec(http("Page 3").get("/page3"))
  setUp(scn.inject(atOnceUsers(3))
    .protocols(exampleHttp))
}

see: https://gatling.io/docs/current/cheat-sheet/#http-protocol-urls-baseUrl

baseURL: Sets the base URL of all relative URLs of the scenario on which the configuration is applied.

Recollect answered 4/4, 2016 at 19:57 Comment(1)
But this doesn't answer a question "how to switch protocols during scenario?". What if want to delete default protocol headers for particular requests? Or add more headers to the protocol after a certain request?Maestro
I
0

I am currently working in gatling.

SOLUTION:

=> WHAT WE USE IF WE HAVE ONE HTTPBASE:

var httpConf1= http.baseUrl("https://s1.com")

=>FOR MULTIPLE HTTPBASE:

var httpConf1=http.**baseUrls**("https://s1.com","https://s2.com","https://s3.com")

You can use baseUrls function which takes a list of httpBase urls.

ANOTHER METHOD:

Reading from csv file all httpbase and storing it in list buffer in scala language and then converting it into list while passing it in http.baseUrls

var listOfTenants:ListBuffer[String] = new ListBuffer[String] //buffer 
var httpConf1=http.**baseUrls**(listOfTenants.toList) //converting buffer to list.

Hope it helps!. Thank you.😊

Isacco answered 10/1, 2023 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.