0

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)
    }
Peter
  • 855
  • 2
  • 15
  • 35
  • Instead of using `val` can you try `var counter =` ? – Pritam Banerjee Nov 15 '16 at 06:23
  • I did it with 'var' but throws same error "failed to execute: No attribute named 'counter' is defined" – Peter Nov 15 '16 at 06:26
  • To set the counter you will need to inject the session in front of the http request. – Pritam Banerjee Nov 15 '16 at 06:31
  • how? if possible can you please show? – Peter Nov 15 '16 at 06:34
  • Like this: .exec(session => .. http("Change_Password") .put(setPasswordURL + "/api/authentication/accounts/" + accountName + "/users/" + unm + "${counter}/actions/setPassword") – Pritam Banerjee Nov 15 '16 at 06:34
  • Something like this: http://stackoverflow.com/questions/38834933/how-to-dynamically-generate-json-in-gatling. Look at the solution. – Pritam Banerjee Nov 15 '16 at 06:34
  • I tried, .exec(session => http("Change_Password").......) gives scala:67: type mismatch; found : io.gatling.http.request.builder.HttpRequestBuilder required: io.gatling.core.validation.Validation[io.gatling.core.session.Session] 3829 [main] ERROR io.gatling.compiler.ZincCompiler$ - .body(StringBody("""{"newPassword":"password"}""")) 3830 [main] ERROR io.gatling.compiler.ZincCompiler$ - ^ 3877 [main] ERROR io.gatling.compiler.ZincCompiler$ - one error found 3877 [main] DEBUG io.gatling.compiler.ZincCompiler$ - Compilation failed (CompilerInterface) – Peter Nov 15 '16 at 06:39
  • Did you look at the solution of the question I just shared? I don't have a gatling setup right now, otherwise I could have helped you to fix it. – Pritam Banerjee Nov 15 '16 at 06:40
  • Yes, I have tried with that code. Added session => before http("Change_Password") and .body(StringBody(s"""{"newPassword":"password"}""")). But throws same error which I mentioned in above comment. – Peter Nov 15 '16 at 06:46

1 Answers1

0

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)
   }
}
crak
  • 1,635
  • 2
  • 17
  • 33