0
votes

I have two URLs 1st which is BaseURL creates users and 2nd which is used to set password to every users which is in asLongAs loop. I have issue in setting "counter" in 2nd url request which is "Change_Password". It throws below error,

9425 [GatlingSystem-akka.actor.default-dispatcher-11] ERROR i.g.http.action.HttpRequestAction - 'httpRequest-4' failed to execute: No attribute named 'counter' is defined

I am not able to set counter here,

.exec(http("Change_Password")
            .put(setPasswordURL + "/api/authentication/accounts/" + accountName + "/users/" + unm + "${counter}/actions/setPassword")

It works fine if I put static values in .put request.

Here is the code,

import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class setPass extends Simulation {
    val testServerUrl = System.getProperty("testServerUrl", "https://abcde.net")
    val username = System.getProperty("username", "ma")
    val password = System.getProperty("password", "ma")

    val accntID = Integer.getInteger("accountID", 27280asd5).toInt
    val rolID = Integer.getInteger("roleId", 272812506).toInt
    val startNum = Integer.getInteger("startNum", 2).toInt
    val endNum = Integer.getInteger("EndNum", 2).toInt
    val userCount = Integer.getInteger("userCount", 1).toInt
    val unm = System.getProperty("CreateUserName", "TestUser")
    val FirstName = System.getProperty("FirstName", "Test")
    val LastName = System.getProperty("LastName", "Yo")
    val emailDomain = System.getProperty("EmailDomain", "@testdomain.com")
    val accountName = System.getProperty("AccountName", "info")

    val counter = new java.util.concurrent.atomic.AtomicInteger(startNum)

    val setPasswordURL = "http://XYZ-differentURL.net:18101"

    val httpProtocol = http
        .baseURL(testServerUrl)
        .basicAuth(username, password)
        .inferHtmlResources()
        .acceptHeader("""*/*""")
        .acceptEncodingHeader("""gzip, deflate""")
        .acceptLanguageHeader("""en-US,en;q=0.8""")
        .contentTypeHeader( """application/json""")
        .userAgentHeader("""Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36""")

    val headers_0 = Map(
        """Cache-Control""" -> """no-cache""",
        """Origin""" -> """chrome-extension://fhbjgbifasdsgdshghfdhdcbncdddomop""")

    val headers_1 = Map(
        """Accept""" -> """application/json""",
        """Content-Type""" -> """application/json""")   

    val scn = scenario("sc1")
        .asLongAs(session => counter.getAndIncrement() < endNum)
        {
        exec(http("req1")
            .post("""/api/user""")
            .headers(headers_0)
            .body(StringBody(session =>s"""{"name": "$unm${counter.get()}" ,"roleId": $rolID, "accountId": $accntID, "firstName": "$FirstName${counter.get()}", "lastName": "$LastName${counter.get()}", "email": "$unm${counter.get()}$emailDomain"}"""))
            .check(jsonPath("$.id").saveAs("UserID"))
            )

        .exec(http("Change_Password")
            .put(setPasswordURL + "/api/authentication/accounts/" + accountName + "/users/" + unm + "${counter}/actions/setPassword")
            .headers(headers_1)
            .body(StringBody("""{"newPassword":"password"}"""))
            )   
        }

    .exec(session => session.set("count", counter.getAndIncrement))
    setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
    }
1
Instead of using val can you try var counter = ? - Pritam Banerjee
I did it with 'var' but throws same error "failed to execute: No attribute named 'counter' is defined" - Peter
To set the counter you will need to inject the session in front of the http request. - Pritam Banerjee
how? if possible can you please show? - Peter
Like this: .exec(session => .. http("Change_Password") .put(setPasswordURL + "/api/authentication/accounts/" + accountName + "/users/" + unm + "${counter}/actions/setPassword") - Pritam Banerjee

1 Answers

0
votes

You can use the Gatling request id in an inelegant way.

.exec{session => session.set("number",session.userId.split("-").last.toInt)}
.exec{session => session("number").as[Int] }

I use it to call a request every 10 requests, and every 100 request.

.exec{session => session.set("number",session.userId.split("-").last.toInt)}
.exec(request1)
.doIf(session => session("number").as[Int] % 10 == 0) {
   exec(request2)
   .doIf(session => session("number").as[Int] % 100 == 0) {
      exec(pause(10 seconds))
      .exec(request3)
      .exec(pause(4 seconds))
      .exec(request4)
   }
}