我在从一个响应中提取数据时遇到问题,我在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-debugger-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", "[email protected]")
.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上使用它。
是否可以处理此请求?另一方面,我没有问题地提取了承载令牌。
您上面已经注释了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
会话中保存的变量没有被继承。