空手道特征文件中的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.
我尝试过的事情
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.
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
这导致了...
失败的功能:IntegrationTests.TestSetup.create-tariff:-unknown-:5 -JavaScript评估失败:read('create-service.feature').responseHeaders ['Location'].split('/') 1 , TypeError:无法从位于的未定义中读取属性"Location" 行号1
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
注意 指定的功能"create-service.feature"在隔离时确实可以工作,并且会生成响应标头,如下所示
NOTE The specified feature "create-service.feature" does indeed work when isolated and does produce the response header, as shown below
推荐答案
使用lastIndexOf
代替split
:
* def location = responseHeaders['Location'][0]
* def serviceId = location.substring(location.lastIndexOf('/') + 1)
这篇关于空手道特征文件中的String.split()返回异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!