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
comments powered by Disqus

Recent Articles

blog-image
7 HTTP methods and how to use them

HTTP protocol works by clients sending requests to the servers and servers responding to the requests. We do CRUD operations (Create, Read, Update, Delete) by sending HTTP requests with different HTTP methods, sometimes called HTTP verbs.



blog-image
Introduction to API Blueprint

API blueprint is a powerful high-level API design language for web APIs. In this article, we want to dive deeper into it and learn more about how it works, the differences between API blueprint and Swagger, and what makes it unique that leads to its extensive use. But before we dig into API Blueprint, we must ensure a solid base of information about the “API first approach” concepts.

blog-image
False positive & false negative in software testing

Exports in automated software testing have borrowed false positive and false negative terms from the medical examination field. In the medical field, the purpose of a test is to determine whether the patient has a particular medical condition or not. As far as software testing is concerned, a false positive indicates a bug when there is none. Conversely, a false negative indicates no bug when there is one.