当空手道框架中的被叫要素发出肥皂请求时,无法访问被叫特征响应值

时间:2018-09-20 17:56:07

标签: soap karate

我有一个共同的功能,该功能可以通过替换request.xml中的字段来发出所有肥皂请求。

Feature: Common Soap Feature

  Background:
    * url 'http://XXX.XXX.XXX.XXX:8080/soap/GetTransaction'

  Scenario:
    * def soap = read('request.xml')
    * replace soap
      | token                  | value              |
      | @@MTID@@               | MTID               |
      | @@MCC_Code@@           | MCC_Code           |
      | @@Token@@              | Token              |
      | @@Trans_link@@         | Trans_link         |
      | @@traceid_lifecycle@@  | traceid_lifecycle  |
      | @@TXn_ID@@             | TXn_ID             |
      | @@Txn_Type@@           | Txn_Type           |
      | @@Bill_Amt@@           | Bill_Amt           |
      | @@Bill_Ccy@@           | Bill_Ccy           |
      | @@Settle_Amt@@         | Settle_Amt         |
      | @@Settle_Ccy@@         | Settle_Ccy         |
      | @@Fx_Fee_Fixed@@       | Fx_Fee_Fixed       |
      | @@Fx_Fee_Rate@@        | Fx_Fee_Rate        |
      | @@FX_Pad@@             | FX_Pad             |
      | @@MCC_Pad@@            | MCC_Pad            |
      | @@Txn_Amt@@            | Txn_Amt            |
      | @@Txn_CCy@@            | Txn_CCy            |
      | @@Txn_Ctry@@           | Txn_Ctry           |
      | @@Txn_GPS_Date@@       | Txn_GPS_Date       |
      | @@GPS_POS_Data@@       | GPS_POS_Data       |
      | @@GPS_POS_Capability@@ | GPS_POS_Capability |
      | @@Proc_Code@@          | Proc_Code          |
      | @@Authorised_by_GPS@@  | Authorised_by_GPS  |
      | @@Txn_Stat_Code@@      | Txn_Stat_Code      |
    Given request soap
    When soap action
    Then status 200

从调用者功能中,我可以按以下方式调用上面的常见soap功能,它可以成功运行,但是我无法访问soap请求的responseTime或其他响应值。

Feature: Caller Feature

  Background:
    * def soap = read('soap.feature')
    * def config = { username: 'XXX', password:'XXX', url: 'jdbc:mysql://XXX.XXX.XXX.XXX:32231/XXX', driverClassName: 'com.mysql.jdbc.Driver' }
    * def DbUtils = Java.type('com.XXX.XXX.utils.DbUtils')
    * def db = new DbUtils(config)
    * def result = db.readRows('SELECT public_token FROM card where status="ACTIVE" and type="PHYSICAL" order by id desc limit 1')
    * def publicToken = result[0].public_token
    * def SampleUtils = Java.type('com.XXX.XXX.utils.SampleUtils')
    * def traceIdLifecycle = SampleUtils.randomTraceId()
    * def transactionLink = SampleUtils.randomId()

  Scenario:
    * def transactionId = SampleUtils.randomId()
    * def gpsDateTime = SampleUtils.now()
    * table parameters
      | MTID   | MCC_Code | Token       | Trans_link      | traceid_lifecycle | TXn_ID        | Txn_Type | Bill_Amt | Bill_Ccy | Settle_Amt | Settle_Ccy | Fx_Fee_Fixed | Fx_Fee_Rate | FX_Pad | MCC_Pad | Txn_Amt  | Txn_CCy | Txn_Ctry | Txn_GPS_Date | GPS_POS_Data      | GPS_POS_Capability                                   | Proc_Code | Authorised_by_GPS | Txn_Stat_Code |
      | '0100' | '6011'   | publicToken | transactionLink | traceIdLifecycle  | transactionId | 'A'      | '-6.95'  | '826'    | '0.00'     | ''         | '0.20'       | '0.30'      | '0.00' | '0.00'  | '0.0000' | '949'   | 'TUR'    | gpsDateTime  | '0051000300000Nx' | '00001001000100000000000010010000000000000012230041' | '010000'  | 'N'               | 'A'           |

    * def caller = call soap parameters
    * assert caller.responseTime < 400

但是使用称为功能responseTime的断言不起作用。

如何访问肥皂功能响应字段?

3 个答案:

答案 0 :(得分:1)

您要访问的数据在变量“ caller”中,但caller是一个数组。

所有常用脚本参数都在其中,发出的请求,响应,标头和responseTime等。

访问时间:

assert caller[0].responseTime > 10

要访问并验证响应,请执行以下操作:

* xml soapResponseXml = caller[0].response
* match soapResponseXml /Envelope/Body/AddResponse/AddResult == 4

这是一个可行的例子。呼叫者功能:

Feature: Caller Feature

Background: 
  * def soap = read('common.feature')

Scenario: soap 1.2
  * table parameters
    | param_a   | param_b | 
    | 1         | 3       | 

  * def caller = call soap parameters
  * print caller
  * xml soapResponseXml = caller[0].response
  * match soapResponseXml /Envelope/Body/AddResponse/AddResult == 4
  * assert caller[0].responseTime > 10

共同特征:

Feature: Common Soap Feature

Background: 
  * url 'http://www.dneonline.com/calculator.asmx'

Scenario: 
  * def soap = read('request.xml')
  * replace soap
    | token | value   |
    | @@a@@ | param_a |
    | @@b@@ | param_b |
  Given request soap
  When soap action 'http://tempuri.org/Add'
  Then status 200

以及请求XML模板:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
  <Add xmlns="http://tempuri.org/">
    <intA>@@a@@</intA>
    <intB>@@b@@</intB>
  </Add>
</soap12:Body>

以下是呼叫者功能中* print caller的输出:

[
 {
  "param_b": 3,
  "responseTime": 846,
  "requestMethod": "POST",
  "requestParams": null,
  "requestUri": "http://www.dneonline.com/calculator.asmx",
  "responseStatus": 200,
  "__loop": 0,
  "__arg": {
    "param_a": 1,
    "param_b": 3
  },
  "requestHeaders": {
    "SOAPAction": [
      "http://tempuri.org/Add"
    ],
    "Content-Type": [
      "text/xml"
    ]
  },
  "responseHeaders": {
    "Cache-Control": [
      "private, max-age=0"
    ],
    "Content-Length": [
      "323"
    ],
    "Content-Type": [
      "application/soap+xml; charset=utf-8"
    ],
    "Server": [
      "Microsoft-IIS/7.5"
    ],
    "X-AspNet-Version": [
      "2.0.50727"
    ],
    "X-Powered-By": [
      "ASP.NET"
    ],
    "Date": [
      "Thu, 27 Sep 2018 19:57:50 GMT"
    ]
  },
  "requestBody": "<soap12:Envelope xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n  <soap12:Body>\n    <Add xmlns=\"http://tempuri.org/\">\n      <intA>1</intA>\n      <intB>3</intB>\n    </Add>\n  </soap12:Body>\n</soap12:Envelope>",
  "response": {
    "soap:Envelope": {
      "_": {
        "soap:Body": {
          "AddResponse": {
            "_": {
              "AddResult": "4"
            },
            "@": {
              "xmlns": "http://tempuri.org/"
            }
          }
        }
      },
      "@": {
        "xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
        "xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
        "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
      }
    }
  },
  "requestTimeStamp": 1538078269984,
  "param_a": 1,
  "parameters": [
    "#ref:java.util.LinkedHashMap"
  ],
  "soap": "<soap12:Envelope xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n  <soap12:Body>\n    <Add xmlns=\"http://tempuri.org/\">\n      <intA>1</intA>\n      <intB>3</intB>\n    </Add>\n  </soap12:Body>\n</soap12:Envelope>",
  "responseCookies": null
 }
]

答案 1 :(得分:1)

另一种选择是只调用通用脚本而不将结果分配给变量。

然后在通用脚本中定义将在调用方范围内的变量。

例如:

Feature: Caller Feature

  Background: 
    * def soap = read('common.feature')

  Scenario: soap 1.2
    * table parameters
      | param_a   | param_b | 
      | 1         | 3       | 

    * call soap parameters
    * match soapResponseXml /Envelope/Body/AddResponse/AddResult == 4
    * assert soapReponseTime > 10

Feature: Common Soap Feature

  Background: 
    * url 'http://www.dneonline.com/calculator.asmx'

  Scenario: 
    * def soap = read('request.xml')
    * replace soap
      | token | value   |
      | @@a@@ | param_a |
      | @@b@@ | param_b |
    Given request soap
    When soap action 'http://tempuri.org/Add'
    Then status 200
    * xml soapResponseXml = response
    * def soapReponseTime = responseTime
    * def soapResponseStatus = responseStatus

答案 2 :(得分:0)

看来您做对了所有事情!一些建议:

  • 这是空手道的最新版本
  • 尝试在同一功能中暂时拨打第二个电话
  • 作为一种解决方法,您可以使用System.currentTimeMillis()
  • 手动计算时间

如果您认为有问题,请按照以下过程操作:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue