val tName = System.getProperty("tName", "asdflkjh")
我打算让"tName"
动态,但它会在输出中打印"$tName"
而不是" asdflkjh"。 .body(StringBody("""{"objectId":${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}"""))
如果我在stringBody中使用'session=>s'
,则会抛出错误.body(StringBody(session=>s"""{"objectId":${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}"""))
//////////////////////错误://////////////////// ////////////
3894 [main] ERROR io.gatling.compiler.ZincCompiler$ - type mismatch;
found : String("${ThumbIdList}")
required: ?{def apply: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps
and method stringToExpression in object Predef of type [T](string: String)(implicit evidence$1: scala.reflect.ClassTag[T])io.gatling.core.session.Expression[T]
are possible conversion functions from String("${ThumbIdList}") to ?{def apply: ?}
3894 [main] ERROR io.gatling.compiler.ZincCompiler$ - .foreach("${ThumbIdList}", "thumbid") {
3894 [main] ERROR io.gatling.compiler.ZincCompiler$ - ^
3914 [main] ERROR io.gatling.compiler.ZincCompiler$ - D:\myTags.scala:110: type mismatch;
found : String("thumbid")
required: ?{def apply: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps
and method stringToExpression in object Predef of type [T](string: String)(implicit evidence$1: scala.reflect.ClassTag[T])io.gatling.core.session.Expression[T]
are possible conversion functions from String("thumbid") to ?{def apply: ?}
3914 [main] ERROR io.gatling.compiler.ZincCompiler$ - .foreach("${ThumbIdList}", "thumbid") {
3924 [main] ERROR io.gatling.compiler.ZincCompiler$ - ^
4004 [main] ERROR io.gatling.compiler.ZincCompiler$ - D:\myTags.scala:98: not found: value assetid
4004 [main] ERROR io.gatling.compiler.ZincCompiler$ - .body(StringBody(session=>s"""{"objectId":${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}"""))
4004 [main] ERROR io.gatling.compiler.ZincCompiler$ - ^
4054 [main] ERROR io.gatling.compiler.ZincCompiler$ - three errors found
4054 [main] DEBUG io.gatling.compiler.ZincCompiler$ - Compilation failed (CompilerInterface)
/////////////脚本从这里开始///////////
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
class myTags 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 accountname = System.getProperty("accountname", "ma1")
val tName = System.getProperty("tName", "asdflkjh")
val httpProtocol = http
.baseURL(testServerUrl)
val headers_6 = 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_52 = Map(
"Accept" -> "application/json, text/plain, */*",
"Accept-Encoding" -> "gzip, deflate, br",
"Cache-Control" -> "no-cache",
"Origin" -> testServerUrl,
"Pragma" -> "no-cache",
"X-Requested-With" -> "XMLHttpRequest",
"Content-Type" -> "application/json;charset=UTF-8",
"Connection" -> "keep-alive",
"User-Agent" -> "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")
val scn = scenario("Add")
.exec(http("Fetch")
.get("/someurl2")
.headers(headers_6)
.check(jsonPath("$.resources[*].ids").findAll.saveAs("IdList"))
.check(jsonPath("$.resources[*].thumbIds").findAll.saveAs("ThumbIdList")))
.foreach("${IdList}", "assetid") {
exec(http("Load_Details")
.get("/mmm/images/loader.svg")
.resources(
http("C1_request")
.get("/mmm/ast/${assetid}/c1")
.headers(headers_6),
http("T1_request")
.get("/mmm/ast/${assetid}/t1")
.headers(headers_6),
http("A1_request")
.post("/mmm/actions")
.headers(headers_52)
.body(StringBody("""{"objects":[{"id":${assetid},"resource":"m-asset"}]}""")),
http("R1_request")
.get("/mmm/variants%3BresourceType=m-asset")
.headers(headers_6),
http("S1_request")
.get("/mmm/ast/${assetid}/keyframes")
.headers(headers_6)))
.exec(http("Add Tags")
.post("/mmm/objs/${assetid}/tags")
.headers(headers_52)
// The PROBLEM IS Here. I want to pass "$tName" dynamically
.body(StringBody("""{"objectId":${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}"""))
)
}
.foreach("${ThumbIdList}", "thumbid") {
doIf(session => session("thumbid").as[String] != "-1")
{
exec(http("Set_Keyframes")
.get("/mmm/keyframes/${thumbid};width=185;height=103")
)
}
}
setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
}
答案 0 :(得分:1)
错误只是你已经在字符串中有$
,所以当你切换到字符串插值(s"""..."""
)时,它被误解了。使用$$
来逃避它:s"""{"objectId":$${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}"""
。请注意,session => ...
部分与字符串插值完全正交。
我会注意到这是不安全的:如果tName
包含引号会怎么样?在这种情况下,假设您要在控制环境变量的机器上运行脚本,但查找“SQL注入”并注意问题不仅限于SQL,这可能并不重要。
答案 1 :(得分:0)
您是否尝试获取环境变量值?如果是,请试试这个:
val tName = sys.env("tName").getOrElse(doSomething here...)