我已经解析了json响应并使用.findAll()保存了它。我想要做的是,如果值为' -1'则忽略keyframeId。否则将keyframeId放入带有循环的.get()请求中。我做了一些代码但是没有在get()请求中设置值并给出了“KO”。这里它采用Vector中的所有值并将所有值放在单个http调用中查看错误以获取详细信息。我也不确定doIf条件。你能帮忙吗?谢谢。 我的Json响应格式是这样的。
{
totalCount: 1134,
limit: 9,
offset: 0,
resources: [
{
title: "Test",
keyframeId: -1
}
{
title: "Test1",
keyframeId: 12345
}
{
title: "Test2",
keyframeId: 12341
}
{
title: "Test3",
keyframeId: -1
}
{
title: "Test4",
keyframeId: 135481
}
....
....
]}
这是gatling脚本,
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
class MamamSearch extends Simulation {
val testServerUrl = System.getProperty("testServerUrl", "https://someurl")
val username = System.getProperty("username", "ma")
val password = System.getProperty("password", "ma")
val userCount = Integer.getInteger("userCount", 1).toInt
val httpProtocol = http
.baseURL(testServerUrl)
.inferHtmlResources(BlackList(""".*\.js""", """.*\.css""", """.*\.gif""", """.*\.jpeg""", """.*\.jpg""", """.*\.ico""", """.*\.woff""", """.*\.(t|o)tf""", """.*\.png"""), WhiteList())
val headers_0 = Map(
"Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Cache-Control" -> "max-age=0",
"Upgrade-Insecure-Requests" -> "1")
val headers_2 = Map("Accept" -> "text/css,*/*;q=0.1")
val headers_6 = Map(
"Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding" -> "gzip, deflate, br",
"Cache-Control" -> "max-age=0",
"Origin" -> testServerUrl,
"Upgrade-Insecure-Requests" -> "1")
val headers_80 = Map(
"Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Upgrade-Insecure-Requests" -> "1")
val headers_7 = Map("Accept" -> "image/webp,image/*,*/*;q=0.8")
val headers_11 = Map("Origin" -> testServerUrl)
val headers_12 = Map(
"Cache-Control" -> "no-cache",
"If-Modified-Since" -> "Mon, 26 Jul 1997 05:00:00 GMT",
"Pragma" -> "no-cache",
"X-Requested-With" -> "XMLHttpRequest")
val headers_15 = Map(
"Accept" -> "application/json, text/plain, */*",
"Cache-Control" -> "no-cache",
"If-Modified-Since" -> "Mon, 26 Jul 1997 05:00:00 GMT",
"Pragma" -> "no-cache",
"X-Requested-With" -> "XMLHttpRequest")
val headers_16 = Map("Accept" -> "*/*")
val headers_18 = Map(
"Accept" -> "text/html",
"Cache-Control" -> "no-cache",
"If-Modified-Since" -> "Mon, 26 Jul 1997 05:00:00 GMT",
"Pragma" -> "no-cache",
"X-Requested-With" -> "XMLHttpRequest")
val headers_19 = Map(
"Accept" -> "*/*",
"Accept-Encoding" -> "gzip, deflate, br",
"Origin" -> testServerUrl)
val headers_27 = Map(
"Accept" -> "*/*",
"Accept-Encoding" -> "gzip, deflate, br",
"Content-type" -> "text/plain",
"Origin" -> testServerUrl)
val uri1 = testServerUrl + "/mamam"
val uri2 = "https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0"
// Login request
val scn = scenario("MamamSearch")
.exec(http("Login")
.post("/mamam/a/masteraccount/login")
.headers(headers_6)
.formParam("username", username)
.formParam("password", password))
// Fetch and save data
.exec(http("Keyframe_request")
.get(uri1 + "/awsset/browse%3Bresource_type=media%3Boffset=1%3Blimit=9")
.headers(headers_12)
.check(jsonPath("$.resources[*].keyframeId").findAll.saveAs("kList"))
)
// added loop and conditions
.doIf(session => session("Keyframe_request").validate[String].map(kList => !kList.contains("-1")))
{
foreach("${kList}", "keyId") {
exec(http("Set_Keyframes")
.get(uri1 + "/keyframes/${kList};width=185;height=103")
.headers(headers_7))
}
}
.exec(http("Logout")
.get("/mam/logout")
.headers(headers_80))
setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
}
引发以下错误:
21345 [GatlingSystem-akka.actor.default-dispatcher-13] WARN i.g.http.ahc.AsyncHandlerActor - Request 'Set_Keyframes' failed: status.find.in(200,304,201,202,203,204,205,206,207,208,209), but actually found 400
21346 [GatlingSystem-akka.actor.default-dispatcher-13] DEBUG i.g.http.ahc.AsyncHandlerActor -
>>>>>>>>>>>>>>>>>>>>>>>>>>
Request:
Set_Keyframes: KO status.find.in(200,304,201,202,203,204,205,206,207,208,209), but actually found 400
21390 [GatlingSystem-akka.actor.default-dispatcher-4] INFO io.gatling.http.ahc.HttpEngine - Sending request=Set_Keyframes uri=https://someurl/mamam/keyframes/Vector(167154,%20167035,%20167037,%20167040,%20167029,%20167024,%20167026,%20167022,%20167023);width=185;height=103:
答案 0 :(得分:0)
如果我已正确理解您想从响应中获取值,然后在下一个请求中使用它。首先,您必须保存正确的keyframeId。
不清楚的是,您是想从列表中选择一个随机keyframeId还是多个关键帧,我选择了随机选择。
首先,我们必须将一些jsonPath应用于我们的JSON响应。此代码.check(jsonPath("$.resources[?(@.keyframeId > -1)].keyframeId")
将仅返回大于-1的keyframeId列表。
接下来,我们选择一个随机的并将其保存到变量。
// Fetch and save data
.exec(http("Keyframe_request")
.get(uri1 + "/awsset/browse%3Bresource_type=media%3Boffset=1%3Blimit=9")
.headers(headers_12)
.check(jsonPath("$.resources[?(@.keyframeId > -1)].keyframeId")
.ofType[String]
.findAll
.transform(s => util.Random.shuffle(s).apply(1))
.saveAs("keyframeId"))
现在您只需检查会话变量keyframeId是否存在并使用它来驱动您的响应${keyframeId}
答案 1 :(得分:0)
.foreach("${IdList}", "id") {
doIf(session => session("id").as[String] != "-1") {
exec(http("ID != -1").get(session => session("id").as[String])
.check(status.is(404)))}
}