Selecting random attribute from list captured via check.(jsonPath("$..attribute").findAll.saveAs

1,612 views
Skip to first unread message

mlastephenw...@gmail.com

unread,
Apr 16, 2015, 10:26:43 AM4/16/15
to gat...@googlegroups.com
Hi,

I have an issue where I when I've captured  a list of all values returned for 4 attributes in an http response:

    .exec(http("addPosition")
      .post(baseUrl + "/url/getManagePositionByARVAAjax?organisationId=${userOrgIDParam}&isARAVA=false&screenContext=OWN")
      .headers(baseHeaders)
      .headers(headers_21)
      .check(status.is(200))
      .check(jsonPath("$..positionUuid").findAll.saveAs("positionUuidParamList"))
      .check(jsonPath("$..positionId").findAll.saveAs("positionIdParamList"))
      .check(jsonPath("$..positionName").findAll.saveAs("positionNameParamList"))
      .check(jsonPath("$..orgCodeId").findAll.saveAs("orgCodeIdParamList"))
     )


So say if there are a list of 20 values for each attribute, I want to select randomly, say value 5, for all 4 attributes to use in the next http Post, meaning that I can't use the ${Attributename.random()} function. How do I do that? Any help would be greatly appreciated.

    .exec(http("createPositionAssignment")
      .get(baseUrl + "/url/getServerTime?_=${serverTimestamp}")
      .headers(headers_10)
      .resources(http("createPositionAssignmentRequest")
      .post(baseUrl + "/url/createPositionAssignmentRequest ")
      .headers(baseHeaders)
      .headers(headers_21)
      .headers(headers_19)
      .formParam("applicantNotes", "")
      .formParam("positionExists", "true")
      .formParam("userPositions", "[]")
      .formParam("userName", "${firstNameParam} ${lastNameParam}")
      .formParam("userUuid", "${newUserUUID}")
      .formParam("workListId", "${workListIdParam}")
      .formParam("workListOrgId", "")
      .formParam("workListOrgCode", "")
      .formParam("workListOrgName", "")
      .formParam("requestType", "OWN")
      .formParam("organizationId", "${orgIDParam}")
      .formParam("changedOrgId", "")
      .formParam("withOutPermission", "false")
      .formParam("proceedWOSponsorApproval", "true")
      .formParam("userPositionVO[0].startDate", "${dateString}")
      .formParam("userPositionVO[0].endDate", "${dateStringPlusTen}")
      .formParam("userPositionVO[0].usersPositionId", "")
      .formParam("userPositionVO[0].positionName", "${positionNameParam}")
      .formParam("userPositionVO[0].positionUuid", "${positionUuidParam}")
      .formParam("userPositionVO[0].positionId", "${positionIdParam}")
      .formParam("userPositionVO[0].orgCode", "${orgCodeIdParam}")
      .formParam("userPositionVO[0].state", "New")
      .formParam("userPositionVO[0].source", "CareID")
      .formParam("userPositionVO[0].managedInId", "1")
      .formParam("userPositionVO[0].organizationId", "${orgIDParam}")
      .check(
            regex("SUCCESS").find(0).exists
              )
      .check(
            regex("code\":\"SUCCESS\",\"message\":\"(.*)\",\"category")
            .saveAs("requestIdParam")
              )
      ))

Thanks,

Steve

Stéphane LANDELLE

unread,
Apr 27, 2015, 4:53:24 AM4/27/15
to gat...@googlegroups.com
The simplest solution is probably to save the whole array in the session, then extract the fields of a random entry manually in a exec(function), like:

.check(jsonPath("$..array").ofType[Seq[Any]].saveAs("theArray"))

...

.exec { session =>
  for {
    array <- session("theArray").validate[Seq[Map[String, Any]]]
    entry = array(Random.nextInt(array.size))
  } yield session.setAll("positionUuid" -> entry("positionUuid"), "positionId" -> entry("positionId"))
}

Stéphane Landelle
Lead developer


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mlastephenw...@gmail.com

unread,
Apr 29, 2015, 4:58:26 AM4/29/15
to gat...@googlegroups.com
Thanks Stephane,

That works, although I did have to amend the check slightly:

      .check(jsonPath("$[*]").ofType[Map[String,Any]].findAll.saveAs("theArray"))

Thanks for your help!

Steve
Reply all
Reply to author
Forward
0 new messages