简体   繁体   English

空手道特征文件中的 String.split() 返回异常

[英]String.split() in Karate Feature File returning exceptions

I'm unsure about how I can split the response string from an already created feature to obtain the response header "Location" value.我不确定如何从已创建的要素中拆分响应字符串以获得响应 header“位置”值。

What I've tried我试过的

1) 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

Which resulted in...这导致...

IntegrationTests.TestSetup.create-tariff: create-tariff.feature:10 -.net.minidev.json.parser.ParseException: Unexpected token L at position 46. IntegrationTests.TestSetup.create-tariff: create-tariff.feature:10 -.net.minidev.json.parser.ParseException: 意外的令牌 L 在 position 46。

2) 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

Which resulted in...这导致...

failed features: IntegrationTests.TestSetup.create-tariff: -unknown-:5 - javascript evaluation failed: read('create-service.feature').responseHeaders['Location'].split('/') 1 , TypeError: Cannot read property "Location" from undefined in at line number 1失败的功能:IntegrationTests.TestSetup.create-tariff: -unknown-:5 - javascript 评估失败:read('create-service.feature').responseHeaders['Location'].split('/') 1 ,类型错误:无法从第 1 行的未定义中读取属性“位置”

NOTE The specified feature "create-service.feature" does indeed work when isolated and does produce the response header, as shown below注意指定的功能“create-service.feature”在隔离时确实有效,并产生响应 header,如下所示

位置标头响应

Use lastIndexOf instead of split : 使用lastIndexOf而不是split

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

You need to use a Javascript function : https://github.com/intuit/karate#javascript-functions 您需要使用Javascript函数: https : //github.com/intuit/karate#javascript-functions

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

EDIT: 编辑:

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

I think the first error is due to single quotes inside your expression, try escaping that 我认为第一个错误是由于表达式中的单引号引起的,请尝试转义

like, 喜欢,

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
}
"""

Edit: Just now noted each value in responseHeader has a list type value so access it like Location[0] 编辑:刚注意到,responseHeader中的每个值都有一个列表类型值,因此像Location [0]一样访问它

And your second Approach should be something like this, 第二种方法应该是这样的,

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

I just face the same issue ( .split is not a function ), and in my case, I need to convert the data to string first, before using split function.我只是面临同样的问题( .split 不是 function ),在我的例子中,我需要先将数据转换为字符串,然后再使用 split function。

Here is the custom code from adrien answer:这是来自adrien answer 的自定义代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM