加特林:获取REST资源,更改JSON叶子,回发 [英] Gatling: Get REST resource, change JSON leaf, Post back

查看:106
本文介绍了加特林:获取REST资源,更改JSON叶子,回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个测试脚本,使用Scala在Gatling中测试REST接口.

I am working on a test script, testing a REST interface in Gatling using Scala.

对于特定的REST资源,这是我想要实现的:

For a specific REST resource, this is what I would like to achieve:

  1. 获取资源(这将在体内提供JSON数据).
  2. 使用jsonpath更改正文中的值.
  3. 将修改后的正文重新发布到相同的网址

我已经成功地在1和3上取得了成功.剩下的唯一问题是更改了似乎是字符串格式的json数据.

I have managed to success with 1 and 3. The only problem left is to change the json data which seem to be in string format.

测试步骤

object WebTestCollection {


    def getAccountDetails() = http("Get account")
      .get("/account")
      .check(jsonPath("$.billingAccount").saveAs("accountjson"))


    def postNewAccountDetails() = http("Post modified account")
      .post("/account").asJSON
      .body("${accountjson}")

}

部分场景

val scn = scenario("Web Usage")
    .feed(testRuns)


        .exec(WebTestCollection.getAccountDetails())
        .exitHereIfFailed

        .exec(session => {
              var account = session.getAttribute("accountjson")
              account.notes = "Performance Test Comment"
              println(account)
              session.setAttribute("accountjson", account)
          }
        )       

        .exec(WebTestCollection.postNewAccountDetails())
        .exitHereIfFailed

我收到以下错误

09:59:30.267 [ERROR] c.e.e.g.a.ZincCompiler$ - <snip>/WebScenario.scala:172: value notes is not a member of Any
09:59:30.268 [ERROR] c.e.e.g.a.ZincCompiler$ -            account.notes = "Performance Test Comment"
09:59:30.269 [ERROR] c.e.e.g.a.ZincCompiler$ -                                   ^
09:59:30.664 [ERROR] c.e.e.g.a.ZincCompiler$ - one error found
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala_maven_executions.MainHelper.runMain(MainHelper.java:164)
    at scala_maven_executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)
Caused by: Compilation failed
    at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:76)
    at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:35)
    at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:29)
    at sbt.compiler.AggressiveCompile$$anonfun$4$$anonfun$compileScala$1$1.apply$mcV$sp(AggressiveCompile.scala:71)
    at sbt.compiler.AggressiveCompile$$anonfun$4$$anonfun$compileScala$1$1.apply(AggressiveCompile.scala:71)
    at sbt.compiler.AggressiveCompile$$anonfun$4$$anonfun$compileScala$1$1.apply(AggressiveCompile.scala:71)
    at sbt.compiler.AggressiveCompile.sbt$compiler$AggressiveCompile$$timed(AggressiveCompile.scala:101)
    at sbt.compiler.AggressiveCompile$$anonfun$4.compileScala$1(AggressiveCompile.scala:70)
    at sbt.compiler.AggressiveCompile$$anonfun$4.apply(AggressiveCompile.scala:88)
    at sbt.compiler.AggressiveCompile$$anonfun$4.apply(AggressiveCompile.scala:60)
    at sbt.inc.IncrementalCompile$$anonfun$doCompile$1.apply(Compile.scala:24)
    at sbt.inc.IncrementalCompile$$anonfun$doCompile$1.apply(Compile.scala:22)
    at sbt.inc.Incremental$.cycle(Incremental.scala:52)
    at sbt.inc.Incremental$.compile(Incremental.scala:29)
    at sbt.inc.IncrementalCompile$.apply(Compile.scala:20)
    at sbt.compiler.AggressiveCompile.compile2(AggressiveCompile.scala:96)
    at sbt.compiler.AggressiveCompile.compile1(AggressiveCompile.scala:44)
    at com.typesafe.zinc.Compiler.compile(Compiler.scala:158)
    at com.typesafe.zinc.Compiler.compile(Compiler.scala:142)
    at com.excilys.ebi.gatling.app.ZincCompiler$.apply(ZincCompiler.scala:104)
    at com.excilys.ebi.gatling.app.SimulationClassLoader$.fromSourcesDirectory(SimulationClassLoader.scala:34)
    at com.excilys.ebi.gatling.app.Gatling$$anonfun$12.apply(Gatling.scala:89)
    at com.excilys.ebi.gatling.app.Gatling$$anonfun$12.apply(Gatling.scala:89)
    at scala.Option.getOrElse(Option.scala:108)
    at com.excilys.ebi.gatling.app.Gatling.start(Gatling.scala:89)
    at com.excilys.ebi.gatling.app.Gatling$.fromMap(Gatling.scala:54)
    at com.excilys.ebi.gatling.app.Gatling$.runGatling(Gatling.scala:74)
    at com.excilys.ebi.gatling.app.Gatling$.main(Gatling.scala:49)
    at com.excilys.ebi.gatling.app.Gatling.main(Gatling.scala)
    ... 6 more

推荐答案

只有当前的加特林2快照允许您

Only current Gatling 2 snapshot let you save JsonPath result into something else than String:

jsonPath(expression).ofType[T]

但是,我们目前尚无用于编辑然后重新序列化JSON AST的工具. 我们提供了JSON解析器(Boon和Jackson),因此您可以自己实现.您还可以打开功能请求.

But then, we don't currently have a facility for editing and then re-serializing a JSON AST. We ship JSON parsers (Boon and Jackson) so you can probably achieve this yourself. You can also open a feature request.

这篇关于加特林:获取REST资源,更改JSON叶子,回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆