1

I am designing automation scripts using the REST APIs, RestAssured with Cucumber BDD framework. I have multiple APIs where one API's response is used as a parameter in another API. 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 Perform "POST" request using
    Then status code is 200
    And  response contains "scope"
    Examples:
      | userName | key        |
      | AWnCb    | EMAekuSA2f |

Now the response of the 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" Authorization parameter with Bearer. also i need to pass "app_id" and "nonce" in the Create Order API. 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 | app_id   | nonce   |
      | application/json | <token>       | <app_id> | <nonce> |
    When Perform "POST" request using "FILE_PATH_ORDER_JSON"
    Then status code is 201

How can I get the values from the response of one API and the use that data in the next API as payload or query param in the same feature file so that I can use it anywhere in this feature file?

1 Answer 1

0

Considering that you will be using the same access token for all scenarios, the simplest way is to have a Singleton that holds the data.

class AccessData {

   public static String accessToken;
   public static String ....
   
   private AccessData() {}

}

Then, on the Background scenario, you should do AccessData.accessToken = blablabla. In the scenario itself, you can retrieve the data AccessData.accessToken.

If you want to have access data for multiple users (userName in the Background scenario), you can have a Singleton that holds a map from a user name to an AccessData object.

class AccessData {

   private static HashMap<String, UserDataAccess> usersData = new HashMap();
   
   private AccessData() {}

   public void setAccessData(String userName, UserDataAccess userDataAccess) {
     // here you set the new entry on the map
   }

   public void getAccessData(String userName) {
     return usersData.get(userName);
   }

}

class UserDataAccess { // This is a simple data class; from Java 17 onwards you can Records

   public String accessToken;
   public String ....
}
1
  • Background scenario would also be written in Cucumber feature file only. Java logic can not be applied to feature files. It won't recognise anything except step definition. I am still not getting how would i pass the access token and other parameters in the next API in same feature file.
    – iAutomate
    Aug 31, 2023 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.