【问题标题】:String.split() in Karate Feature File returning exceptions空手道特征文件中的 String.split() 返回异常
【发布时间】:2019-02-06 22:51:20
【问题描述】:

我不确定如何从已创建的功能中拆分响应字符串以获取响应标头 \"Location\" 值。

我尝试过的

1)

Feature: Create Tariff

  Background:
  * def result = call read(\'../../get-user-token.feature\')
  * def serviceId = call read(\'create-service.feature\')

  Scenario: Create Tariff
    Given url \'https://app-dev.topbox.pro/tariff-svc/api/v1/tariffs\'
    And header Authorization = result.response.token_type + \" \" + result.response.access_token
    And request
      \"\"\"
      {
      serviceTypeId: \'#(serviceId.responseHeaders[\'Location\'].split(\'/\')[1])\',
      owner: 1,
      type: 0,
      pencePerMile: \'69.69\',
      minMileage: \'1.00\',
      minCost: 5,
      zoneFrom: \'\',
      zoneTo: \'\',
      fixedCost: 0
      }
      \"\"\"
    When method POST
    Then status 201

这导致...

IntegrationTests.TestSetup.create-tariff: create-tariff.feature:10 - net.minidev.json.parser.ParseException:位置上的意外标记 L 46.

2)

Feature: Create Tariff

  Background:
  * def result = call read(\'../../get-user-token.feature\')
  * def serviceId = call read(\'create-service.feature\').responseHeaders[\'Location\'].split(\'/\')[1]

  Scenario: Create Tariff
    Given url \'https://app-dev.topbox.pro/tariff-svc/api/v1/tariffs\'
    And header Authorization = result.response.token_type + \" \" + result.response.access_token
    And request
      \"\"\"
      {
      serviceTypeId: \'#(serviceId)\',
      owner: 1,
      type: 0,
      pencePerMile: \'69.69\',
      minMileage: \'1.00\',
      minCost: 5,
      zoneFrom: \'\',
      zoneTo: \'\',
      fixedCost: 0
      }
      \"\"\"
    When method POST
    Then status 201

这导致...

失败的功能:IntegrationTests.TestSetup.create-tariff:-unknown-:5 - javascript 评估失败:read(\'create-service.feature\').responseHeaders[\'Location\'].split(\'/\')1, 类型错误:无法从未定义的 at 中读取属性 \"Location\" 行号 1

笔记指定的特性\“create-service.feature\”在隔离时确实有效,并且确实产生了响应头,如下所示

    标签: javascript dsl karate


    【解决方案1】:

    使用lastIndexOf 而不是split:

    * def location = responseHeaders['Location'][0]
    * def serviceId = location.substring(location.lastIndexOf('/') + 1)
    

    【讨论】:

      【解决方案2】:

      您需要使用 Javascript 函数:https://github.com/intuit/karate#javascript-functions

      * def greeter = function(name){ return 'hello ' + name }
      * assert greeter('Bob') == 'hello Bob'
      

      编辑:

      * def service = { key : "someinfo/myServiceId"}
      * def func = function(service){return service.key.split('/')[1]}
      * def serviceId = func(service)
      * match serviceId == "myServiceId"
      

      【讨论】:

      • 不幸的是,这似乎不起作用“失败的功能:IntegrationTests.TestSetup.create-tariff:-unknown-:7 - javascript 评估失败:func(service), TypeError: service.responseHeaders.Location.split is not a function in < eval> 在第 1 行"
      • 那么问题必须出在 service.responseHeaders['Location'] 上,而不是拆分上。我通过调用 split() 编辑了我的答案。
      【解决方案3】:

      我认为第一个错误是由于表达式中的单引号引起的,请尝试转义

      喜欢,

      And request
      """
      {
      serviceTypeId: '#(serviceId.responseHeaders.Location[0].split(\'/\')[1])',
      owner: 1,
      type: 0,
      pencePerMile: '69.69',
      minMileage: '1.00',
      minCost: 5,
      zoneFrom: '',
      zoneTo: '',
      fixedCost: 0
      }
      """
      

      编辑:刚才注意到 responseHeader 中的每个值都有一个列表类型值,因此可以像 Location[0] 一样访问它

      你的第二种方法应该是这样的,

      * def serviceId = call read('create-service.feature').responseHeaders.Location[0].split('/')[1]
      

      【讨论】:

      • 不幸的是,服务器端响应将其作为文字字符串 ' "detail":{"Property":"serviceTypeId","Message":"Invalid guid format","Value":"#(serviceId.responseHeaders.Location[0] .split('/')[1])"}} ' 而不是 Location Response Header 的实际值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多