我不确定我如何从一个已经创建的要素分割响应字符串,以获得响应头“位置”值。
我已经试过
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.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关税:-unknown-:5 - JavaScript的评估失败:阅读( '创建-service.feature')responseHeaders响应[ '位置']分裂( '/')1,类型错误:不能从1号线未定义在读取属性“位置”
注意指定的功能“创建-service.feature”分离的和不产生响应标头时确实工作,如下所示
使用lastIndexOf
代替split
的:
* def location = responseHeaders['Location'][0]
* def serviceId = location.substring(location.lastIndexOf('/') + 1)
我认为第一个错误是由于您的表达式中的单引号,试图逃脱那
喜欢,
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每个值都有一个列表类型值,访问它就像位置[0]
和你的第二个方法应该是这样的,
* def serviceId = call read('create-service.feature').responseHeaders.Location[0].split('/')[1]
您需要使用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"