首页 > 解决方案 > 从 Gatling 中的编码响应中提取数据

问题描述

我在从一个响应中提取数据时遇到问题,我在 Fiddler 中对其进行了检查,并收到消息表明此响应已编码: https ://i.postimg.cc/G3VfMtxh/2019-10-15-12-31-41- Progress-Telerik-Fiddler-Web-Debugger.png

当我将此内容复制到https://jsonpath.com/时,我看到 https://i.postimg.cc/rpYP4wh4/2019-10-14-22-41-19-jsonpath-online-evaluator.png

所以我尝试使用正则表达式,我在这里检查了它https://regex101.com/ https://i.postimg.cc/DZ6m6RX1/2019-10-14-22-58-57-Online-regex-tester-and -调试器-PHP-PCRE.png

它可以在在线编辑器中使用,但是脚本存在问题。


import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import java.util.Base64
import io.gatling.http.response._
import java.nio.charset.StandardCharsets.UTF_8

class login2 extends Simulation {

    val httpProtocol = http
        .baseUrl("myapiaddress")
        .inferHtmlResources()
        .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")
        .proxy(Proxy("localhost", 8888).httpsPort(8888))

    val headers_0 = Map(
        "Accept" -> "application/json, text/plain, */*",
        "Origin" -> "mysiteaddress",
        "Sec-Fetch-Mode" -> "cors")

    val headers_1 = Map(
        "Access-Control-Request-Headers" -> "authorization",
        "Access-Control-Request-Method" -> "GET",
        "Origin" -> "mysiteaddress",
        "Sec-Fetch-Mode" -> "cors")

    val headers_2 = Map(
        "Accept" -> "application/json, text/plain, */*",
        "Origin" -> "mysiteaddress",
        "Sec-Fetch-Mode" -> "cors",
        "authorization" -> "Bearer ${authToken}")

    val headers_3 = Map("Sec-Fetch-Mode" -> "no-cors")

    val headers_6 = Map(
        "Origin" -> "mysiteaddress",
        "Sec-Fetch-Mode" -> "cors",
        "content-type" -> "application/x-www-form-urlencoded; charset=UTF-8")


    val uri1 = "api/signalr"



    val scn = scenario("login2")
        .exec(http("request_0")
            .post("/api/oauth/token")
            .headers(headers_0)
            .formParam("username", "yelari@malboxe.com")
            .formParam("password", "Zaq1@wsx")
            .formParam("grant_type", "password")
            .check(jsonPath("$..access_token").exists.saveAs("authToken"))
            .resources(http("request_1")
            .options("/api/account")
            .headers(headers_1),
            http("request_2")
            .get("/api/account")
            .headers(headers_2),
            http("request_3")
            .get("/UploadedFiles/03765fee-ede8-4689-9a4c-13dd2ada18a4.png")
            .headers(headers_3),
            http("request_4")
            .options("/api/conversations/")
            .headers(headers_1),
            http("request_5")
            .options("/api/notifications")
            .headers(headers_1),
            http("request_6")
            .get(uri1 + "/negotiate?clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
            //.check(jsonPath("$.ConnectionToken").exists.saveAs("MyConnectionToken"))
            //.check(regex("""a-zA-Z0-9=/+{152}""").findAll.saveAs("MyConnectionToken"))
            .headers(headers_6),
            http("request_7")
            .get("/api/notifications")
            .headers(headers_2),
            http("request_8")
            .get("/api/conversations/")
            .headers(headers_2),
            http("request_9")
            .get("/UploadedFiles/5899f868-8473-4dfc-a39c-f13a94c47b3a.jpeg")
            .headers(headers_3),
            http("request_10")
            .get("/UploadedFiles/26ac4d69-8a63-4575-bec4-849d5a5e194a.png")
            .headers(headers_3),
            http("request_11")
            .get(uri1 + "/start?transport=serverSentEvents&clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionToken=${MyConnectionToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
            .headers(headers_6)))
val printSesssionVar = scenario("print session var")
    .exec{session => println(session("printSesssionVar").as[String])
      session}
    setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}


但是在这两种情况下我都会出错

> request_11: Failed to build request: No attribute named 'MyCon      1 (50.00%)
nectionToken' is defined
> status.find.in(200,201,202,203,204,205,206,207,208,209,304), f      1 (50.00%)
ound 404

我试图在 request_6 上提取它并在 request_11 上使用它。

是否可以处理此请求?在其他方面,我毫无问题地提取不记名令牌。

标签: testingqagatlingscala-gatling

解决方案


MyConnectionToken您已经在上面注释掉了这两个提取,但我想您已经知道了?

.exec { session =>尝试通过将块放在同一请求链中并将其打印出来,即:

// ... other code above
http("request_11")
            .get(uri1 + "/start?transport=serverSentEvents&clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionToken=${MyConnectionToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
            .headers(headers_6)))
               .exec{session => println(session("printSesssionVar").as[String])
                session}

因为您声明val printSesssionVar = scenario会话中保存的变量没有被继承。


推荐阅读