最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - String.split() in Karate Feature File returning exceptions - Stack Overflow

programmeradmin1浏览0评论

I'm unsure about how I can split the response string from an already created feature to obtain the response header "Location" value.

What I've tried

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 ''
    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.

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 ''
    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

NOTE The specified feature "create-service.feature" does indeed work when isolated and does produce the response header, as shown below

I'm unsure about how I can split the response string from an already created feature to obtain the response header "Location" value.

What I've tried

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.

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

NOTE The specified feature "create-service.feature" does indeed work when isolated and does produce the response header, as shown below

Share Improve this question asked Feb 6, 2019 at 14:13 TaranTaran 5312 gold badges8 silver badges22 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Use lastIndexOf instead of split:

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

You need to use a Javascript function : https://github./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]

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.

Here is the custom code from adrien answer:

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

评论列表(0)

  1. 暂无评论