空手道:解析json中的变量的奇怪问题

时间:2019-03-06 14:05:00

标签: karate

我今天在更新IDE之后发现了这个问题:

Scenario: test scenario
    * def amqpHost = '10.10.10.10'
    * def amqpPort = 5555
    * def amqpUser = 'tesetUser'
    * def amqpPass = 'testPass'
    * def amqpExchange = 'test.rabbitmq.exchange'
    * def amqpQueueName = 'test.rabbitmq.queue'
    * def amqpRoute = 'test.rabbitmq.route'
    * def amqpVirtualHost = '/'

    * print "PORT: " + amqpPort
    * print "PORT: " + amqpUser
    * print "PORT: " + amqpPass
    * print "PORT: " + amqpExchange
    * print "PORT: " + amqpQueueName

    * def inputParams = { fleetId: '#(fleetId)', host: '#(amqpHost)', port:'#(amqpPort)', user:'#(amqpUser)', password: '#(amqpPassword)', exchange: '#(amqpExchange)', route: '#(amqpRoute)', virtual_host: '#(amqpVirtualHost)' }
    * print "INPUT: " + inputParams

输出为:

[print] PORT: 5555
[print] PORT: tesetUser
[print] PORT: testPass
[print] PORT: test.rabbitmq.exchange
[print] PORT: test.rabbitmq.queue
[print] INPUT: {fleetId=#(fleetId), host=10.10.10.10, port=5555, user=tesetUser, password= '#(amqpPassword)', exchange= '#(amqpExchange)', route= '#(amqpRoute)', virtual_host= '#(amqpVirtualHost)'}

amqpPassword和其余变量未正确解析。但是,当我删除json中的所有空格(“ host:'#(amqpHst)'>>“ host:'#(amqpHst)'”)时:

* def inputParams = { fleetId:'#(fleetId)', host:'#(amqpHost)', port:'#(amqpPort)', user:'#(amqpUser)', password:'#(amqpPassword)', exchange:'#(amqpExchange)', route:'#(amqpRoute)', virtual_host:'#(amqpVirtualHost)' }

我得到了结果:

{fleetId=#(fleetId), host=10.10.10.10, port=5555, user=tesetUser, password=#(amqpPassword), exchange=test.rabbitmq.exchange, route=test.rabbitmq.route, virtual_host=/}

今天更新我的IntelliJ Idea或其中一个插件后,似乎出现了此问题。我用: Idea 2018.3.5 CE(内部版本#IC-183.591221) Java黄瓜v183.5429.1

1 个答案:

答案 0 :(得分:1)

首先-请注意,有一种非常方便的方法来使用print关键字,如果适用的话,它可以漂亮地打印JSON,请参见下文:

* print "PORT:", amqpPort
* print "PORT:", amqpUser
* print "PORT:", amqpPass
* print "PORT:", amqpExchange
* print "PORT:", amqpQueueName

* def inputParams = { fleetId: '#(fleetId)', host: '#(amqpHost)', port:'#(amqpPort)', user:'#(amqpUser)', password: '#(amqpPass)', exchange: '#(amqpExchange)', route: '#(amqpRoute)', virtual_host: '#(amqpVirtualHost)' }
* print "INPUT:", inputParams

我得到:

[print] INPUT: {
  "fleetId": "#(fleetId)",
  "host": "10.10.10.10",
  "port": 5555,
  "user": "tesetUser",
  "password": "testPass",
  "exchange": "test.rabbitmq.exchange",
  "route": "test.rabbitmq.route",
  "virtual_host": "/"
}

所以对我来说很好。一个建议,为什么不将整个inputParams定义为JSON本身?您甚至可以从文件中读取它。并注意如何使用foo.bar点符号:

* def inputParams = { fleetId: 'foo', host: '10.10.10.10', port: 5555, user: 'tesetUser', password: 'testPass', exchange: 'test.rabbitmq.exchange', route: 'test.rabbitmq.route', virtual_host: '/' }

* print "port:", inputParams.port
* print "user:", inputParams.user
* print "pass:", inputParams.password
* print "exch:", inputParams.exchange

* print "INPUT:", inputParams

我认为这根本与IDE不相关-如果您仍然发现问题,请执行以下过程:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue