空手道 API 测试 - 如何在同一特征中使用从 API 1 到另一个 API 的变量(响应输出)
Karate API Testing - How to use a variable (output from response) from API 1 to another API in same feature
我有一个场景:
调用 API - 捕获响应 - 从响应中获取 ID 并调用另一个 API 从响应 1 中获取输入 ID。
例如:
Feature: test graphql end point
Background:
* url baseUrl + '/graphql'
Scenario: Create Org Call
Given text query =
"""
mutation {
test: createOrganization(
name: "Org Name"
)
{
Id
name
}
}
"""
And request { query: '#(query)' }
When method post
Then status 200
* def res = response
* def id = res.data.test.Id
* print 'response:', response
* print 'Id:', id
Given text query =
"""
mutation {
createBackendHistory(orgId: '#(id)') {
orgId
}
}
"""
And request { query: '#(query)' }
When method post
Then status 200
如何传递值(来自调用 1 的 ID)是 createBackendHistory API
当我尝试 orgId: '#(id)' 时出现错误。
由于 query
是 text
,您不能使用 #()
嵌入表达式。请参考文档:https://github.com/intuit/karate#replace
试试这个:
Given text query =
"""
mutation {
createBackendHistory(orgId: '<id>') {
orgId
}
}
"""
And replace query.id = id
And request { query: '#(query)' }
When method post
Then status 200
我有一个场景: 调用 API - 捕获响应 - 从响应中获取 ID 并调用另一个 API 从响应 1 中获取输入 ID。
例如:
Feature: test graphql end point
Background:
* url baseUrl + '/graphql'
Scenario: Create Org Call
Given text query =
"""
mutation {
test: createOrganization(
name: "Org Name"
)
{
Id
name
}
}
"""
And request { query: '#(query)' }
When method post
Then status 200
* def res = response
* def id = res.data.test.Id
* print 'response:', response
* print 'Id:', id
Given text query =
"""
mutation {
createBackendHistory(orgId: '#(id)') {
orgId
}
}
"""
And request { query: '#(query)' }
When method post
Then status 200
如何传递值(来自调用 1 的 ID)是 createBackendHistory API
当我尝试 orgId: '#(id)' 时出现错误。
由于 query
是 text
,您不能使用 #()
嵌入表达式。请参考文档:https://github.com/intuit/karate#replace
试试这个:
Given text query =
"""
mutation {
createBackendHistory(orgId: '<id>') {
orgId
}
}
"""
And replace query.id = id
And request { query: '#(query)' }
When method post
Then status 200