[英]Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured
[英]How can i store a value as global variable from an API response and pass it to another API as parameter in Cucumber feature file using REST assured
我正在使用 Rest assured 在 Cucumber BDD 框架上为 REST API 设计自动化脚本。 我有一个 api 生成“令牌”,然后还有另一个 api 用于订单创建,在授权参数中需要此“令牌”。 这是我的功能文件
功能:创建订单 API
@Background:
Scenario Outline: Generate Access token With Valid Details
Given Query param for request
| grant_type |
| client_credentials |
Given Basic Auth keys for request "<userName>" and "<key>"
When Build request for baseurl "PAYPAL_BASE_URI" and endpoint "ENDPOINT_GET_AUTH_KEY"
# And Set global "access_token" in "token"
And Perform "POST" request using
Then status code is 200
And response contains "scope"
Examples:
| userName | key |
| AWnCbuv9Bee0_6 | EMWowD696LqfznidhQ2RT_jZL2ys |
Now response of above API is as follows.
{
"scope": "https://uri.pppaypal.com/services/invoicing https://uri.pppaypal.com/services/applications/webhooks",
"access_token": "ALs1szFnv2TJ19Zf3vq",
"token_type": "Bearer",
"app_id": "APP-284543T",
"expires_in": 311286,
"nonce": "2022-05-31T03:41:41ZWs9dpOQ"
}
现在我需要这个“access_token”,就像在“Create Order API”授权参数中一样。 “创建订单 API”功能文件如下。
Scenario: Verify create order api using valid auth
Given Generate request
And Build request for baseurl "PAYPAL_BASE_URI" and endpoint "ENDPOINT_CREATE_ORDER_API"
And Set header values as
| Content-Type | Authorization |
| application/json | Bearer <token> |
When Perform "POST" request using "FILE_PATH_ORDER_JSON"
Then status code is 201
如何将“token”中的“access_token”设置为功能文件中的全局变量,以便我可以使用以下步骤在此功能文件中的任何位置使用它?
And Set global "access_token" in "token"
您可以创建场景上下文 object 或测试上下文 object(取决于对象所需的生命周期),您可以在其中创建带有键值对的 HashMap。 然后您可以使用这个 object 来存储您需要的信息并在以后检索它。 看看这篇文章,因为它非常详细地解释了这个概念: https://www.toolsqa.com/selenium-cucumber-framework/share-data-between-steps-in-cucumber-using-scenario-context/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.