将变量作为属性传递给XML主体

时间:2019-06-07 12:10:36

标签: scala gatling

我正在Scala中创建一个加特林负载测试,并且希望创建一个具有属性的HTTP POST请求。

 val getServiceCall = http("get service call")
.post("/MyService")
.headers(header)
.body(ElFileBody("templates/GetServiceCallRqTemplate.xml"))
.check(status.is(200))

这是我的xml中的代码段:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
    <pus:GetServiceCallRq>
        <com:header>
            <com:type>pus:GetServiceCallRq</com:type>
            <com:timestamp>timestamp</com:timestamp>
            <com:source>source</com:source>
            <com:destination>destination</com:destination>
            <com:serverNode>serverNode</com:serverNode>
        </com:header>
        <pus:body>
            <pus:reference>${reference}</pus:reference>
            ....

我想通过代码中创建的变量而不是csv文件传递${reference}属性。

如何将其作为请求参数传递?

1 个答案:

答案 0 :(得分:1)

使用供稿器(csv文件)不是将值添加到会话参数中的唯一方法。

您还可以通过会话操作手动设置值

exec(session => session.set("reference", "ref0001"))

或通过检查较早的请求(无论响应中'ref'键中的内容如何保存到$ {reference}变量

exec(
  http("setupReq")
  .get("someURL")
  .check(
    jsonPath("$.ref").saveAs("reference")
  )
)

甚至创建自己的自定义Feeder(例如,如果您只想生成随机引用)

//somewhere at the top of your scala class
private val refs = Iterator.continually(
  Map("reference" -> (Random.alphanumeric take 10 mkString).toUpperCase())
)

//then later in your scenario...
.feed(refs) //will put a new random string into ${reference}
//then make your "get service call" request 
相关问题