简体   繁体   English

如何使用 REST assured 将 API 响应中的值存储为全局变量并将其作为 Cucumber 功能文件中的参数传递给另一个 API

[英]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

I am designing automation scripts on Cucumber BDD framework for REST APIs using Rest assured.我正在使用 Rest assured 在 Cucumber BDD 框架上为 REST API 设计自动化脚本。 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.我有一个 api 生成“令牌”,然后还有另一个 api 用于订单创建,在授权参数中需要此“令牌”。 Here is my feature file这是我的功能文件

Feature: Create Order 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"
}

Now i need this "access_token" as in the "Create Order API" Authorisation parameter withe Bearer.现在我需要这个“access_token”,就像在“Create Order API”授权参数中一样。 the "Create Order API" feature file is below. “创建订单 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

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?如何将“token”中的“access_token”设置为功能文件中的全局变量,以便我可以使用以下步骤在此功能文件中的任何位置使用它?

And Set global "access_token" in "token"

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.您可以创建场景上下文 object 或测试上下文 object(取决于对象所需的生命周期),您可以在其中创建带有键值对的 HashMap。 You can then use this object to store the information you need and retrieve it later.然后您可以使用这个 object 来存储您需要的信息并在以后检索它。 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/看看这篇文章,因为它非常详细地解释了这个概念: https://www.toolsqa.com/selenium-cucumber-framework/share-data-between-steps-in-cucumber-using-scenario-context/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新来自 API 的 JSON 响应,并使用 Rest Assured 将更新后的响应作为输入/正文传递给另一个 API - Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured 如何从get api获取csrf令牌并使用REST Assured将csrf令牌传递给另一个post api? - How to get csrf token from get api and pass csrf token to another post api using REST Assured? 如何将访问令牌值从一个 API 响应主体(在一个类中)提取到另一个 API 标头(在另一个类中)在 rest 保证代码中 - How to extract accesss token value from one API response body(in one class) into another API header(in another class) in rest assured code 如何使用Rest Assured逐行打印出TestNG中的API响应? - How can I get the API response in TestNG using Rest Assured to print out line by line? 我想从一个 API 响应正文中获取价值,并使用 Cucumber gherkin 将其用于另一个 api 请求 - I want to take value from one API response body and use into another api request using Cucumber gherkin 如何使用 Rest 将多个文件作为输入传递给 api - How to pass multiple files as a input to an api using Rest Assured 如何使用java Cucumber将“#”作为特征文件中的有效参数传递 - How to pass "#" as a valid parameter in feature file using java cucumber 两个REST API响应差异使用放心 - Two rest api response diff using rest assured 如何将 Cucumber 功能文件中的场景名称作为 Cucumber 步骤中的参数传递? - How to pass Scenario name from the Cucumber feature file as a parameter in the Cucumber steps? 如何在json响应中获取数组的大小? 放心API、JAVA - How get size of an array in json response? rest assured API, JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM