Extract values from one step & embed them in other steps

Extract values from one step & embed them in other steps


Introduction

Some test cases are simple; you only need to send one HTTP request to verify the correctness of a scenario. There are also scenarios in which you need to involve more than one endpoint. We developed multi-step test cases for this exact reason, and today we’re releasing a major upgrade that enables you to extract values from one step and use them in other steps.

To explain this new feature, let’s continue with an example. Let’s say we need to verify the correctness of the access tokens our API issues; can it be used immediately to fetch data using a protected endpoint?

How would you go about testing such a thing? One could store an access token as a config and call a protected endpoint by providing the access token. This approach kind of works, but it has the following limitations:

  • We only test one access token. Would tokens issued by the latest version of API also work? We never know till we update the token.
  • Tokens often expire at some point, so we cannot use a single token for a long time, which means we have to update the token manually; obviously, it’s less than ideal as it quickly becomes a maintenance overhead for your team.
  • To run tests against multiple environments, we need to maintain multiple access tokens.

Ideally, we should request a new token every time the test runs and use it to fetch the data. Taking this approach will have the following benefits:

  • We always verify the correctness of tokens using the latest version of our API
  • We do not need to be worried about the token being expired at all, as we request a new token every time the test runs.
  • This approach works with multiple environments out of the box as you don’t need to maintain any token.

Test scenarios like the mentioned one motivated us to work on a new feature that allows Testfully users to extract values from one step and use it in other steps.


Where can the values be extracted from?

Testfully can extract values from the following segments of any step:

  • Request Headers
  • Request Body
  • Response Headers
  • Response Body

As you can see, the entire request data set is available, so no matter the values you need is within request or response, Testfully will extract them for you. When it comes to extracting values from the request/response body, you can also extract values from nested fields and arrays as well using a simple syntax which will be explained shortly.


How does value extraction work?

Extracting values from one step and embedding them in another step works the same way as embedding configs, templates or random data. Start by typing @ in Url, Header Value, Request Payload or validations and select the step you want to extract the values from by clicking on the step number.

select an step to embed a value

Upon clicking on the step, Testfully opens the value extraction modal for you. Using this modal, you can set where the value should be extracted from.

select an step to embed a value


What happens when embedded value is missing?

Let’s say the value of the “token” field in response of a step is being embedded in another step. What if API does not return the “token” field? What would happen to the test case?

Testfully offers three options for this edge case:

  • Test Fails
  • Use a default value (provided by the end-user)
  • Use “null” as the value

When setting up an Extract & Embed value, Testfully asks you about your prefered way of handling the missing value. Moreover, you’re not limited to one option for all of the values. Each extract value can have its missing value behaviour.


Extract request or response Headers

To extract the value of a request or response header, you need to take the following steps.

  • Type @ and select the step you want to extract the value from
  • Open the “Section” dropdown and select “Request Headers” or “Response Headers” based on your need
  • Enter header name as value of the “Path to value” field
  • Select your desired strategy for missing value edge case
  • Click on the “save” button

You’re done! Save your test and Testfully extracts & embeds the value the very next time you run your test.The below screenshot shows how “x-access-token” header from the response can be extracted.

Extract Request or Response Headers


Extract values from request or response body

To extract the value of a request or response body, you need to take the following steps.

  • Type @ and select the step you want to extract the value from
  • Open the “Section” dropdown and select “Request Body” or “Response Body” based on your need
  • Enter the path to the field that contains the data you need as value of the “Path to value” field
  • Select your desired strategy for missing value edge case
  • Click on the “save” button

You’re done! Save your test and Testfully extracts & embeds the value the very next time you run your test.The below screenshot shows how value of a field called email in from he response body can be extracted.

Extract values from request or response body


Path to values in the request/response body

Let’s say the login endpoint returns the following response:

Login endpoint response

  • To extract the value of the “success” field, you need to enter success as value of the Path To Value field.
  • To extract the token value on line 4, you need to enter data.token as the value of the Path To Value field. We use the “dot” notion to access nested values.
  • To extract “mattvalleycodes”, You need to enter data.profiles.1.handle as the value of the Path To Value field. We use element index (starting from zero) to access element value when it comes to array elements.


Use cases

To give you some idea about how Extract & Embed feature can be used, we have listed some of the common use cases of this feature.

  • Authenticate & Use the token to fetch data via protected endpoints
  • Sign up & sign in immediately using the same username and password
  • Add a new product & try to fetch the product

Recent Articles

blog-image
Top 7 Free & Paid mock API tools (2022 Review)

Sometimes called a fake API, A Mock API is when you build an API that returns the desired data. Still, it is not your actual API, and it all has been simulated for some use cases. This article covers best free & paid mock API tools in the market.




blog-image
Top 10 GraphQL clients

GraphQL is one of the hottest topics in the API development world, and that’s for good reasons: GraphQL APIs address many of the issues that we had with Restful and SOAP APIs. This article goes through the Top 10 GraphQL clients you can use today to develop and use GraphQL API.




blog-image
Why your Website is giving an HTTP 405 Method Not Allowed message and how to fix it

When a response has the status code 405, the client attempted to access a resource using an HTTP method that is not permitted by the server for that resource. Websites, Restful APIs, and web applications tend to return this error. This article provides a more detailed explanation of this status code’s meaning and how to handle it in your applications.