我如何使用REST保证将API响应中的值存储为API响应的全局变量,并将其传递给Cucumber功能文件中的另一个API作为参数
我正在使用REST确保在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”,如携带者的“创建订单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"
I am designing automation scripts on Cucumber BDD framework for REST APIs using Rest assured.
I have one api which generates the "Token" and then there is an another api for order create which requires this "Token" in the authorisation parameter. Here is my feature file
Feature: Create Order 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"
}
Now i need this "access_token" as in the "Create Order API" Authorisation parameter withe Bearer. the "Create Order API" feature file is below.
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
How can i set "access_token" in "token" as global variable from the feature file so that i can use it anywhere in this feature file using the following step?
And Set global "access_token" in "token"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个方案上下文对象或测试上下文对象(取决于对象所需的生命周期),在该对象可以使用键值对进行hashmap。然后,您可以使用此对象存储所需的信息,并以后再检索。请看一下这篇文章,因为它详细解释了该概念: https://www.toolsqa.com/selenium-cucumber-framework/share-data-data-between-steps-in-steps-in-cucumber-using-scenario-scenario-context/
You can create a scenario context object or a test context object (depending on the needed lifetime of the object) in which you can have a HashMap with key-value pairs. You can then use this object to store the information you need and retrieve it later. Take a look at this article, as it explains the concept in great detail: https://www.toolsqa.com/selenium-cucumber-framework/share-data-between-steps-in-cucumber-using-scenario-context/