Below are the primary learning objectives for this module:

Assertions and Request Components

Variables

The test component library contains many useful test components that can enrich and add further validity to your API Tests.

To access the component library:

Add Component

All available test components, as well as a search bar, appears on the next screen.

Component Library

Example

If you select the Tag component at the top, it will automatically add this component to the bottom of your test like in this image:

Example Component

You can add any of the following components to your test, depending on your use case. For example:

Update InputUpdate InputUpdate Input

There are many components you can play around with, and hover over them to see what they do, but next you will focus on Request, the building block of every API test.

A Request component is the foundation for creating a new test in API Fortress. Whether you wish to test a chain of multiple requests, or a single request, the request component is where your test should begin. The available request components are:

Refer to the API Testing Basics module for further details about API request methods.

Note:

Negative : In the previous module, Introduction to API Fortress, we used the Generate Test button. This button automagically generates the HTTP request, assertions, and other test elements so that you can focus on the more intricate and tricky parts of your tests. In this module we will build a test from scratch in order to understand the importance of assertions, but refer back to that test for inspiration and ideas on how to design your assertions.

Exercise: Add a GET Request Component

  1. Open your project and create a new test: Create New Test
  2. Name it AssertionTest (we cover assertions in the next module): Assertion Test
  3. Select Compose, then select +Add Request/Assertions in the Test Composer. Add Request / Assertion
  4. Select the GET Request component and add the following data:
    • url: https://saucelabs.com/versions.json
    • var: payloadAdd GET Request
  5. This is what the finished result should look like: Finished result of GET component

In the next section you will work with Assertion components, learning how they are a vital part of your API tests.

There's a common phrase in the automated testing space: "If there is no assertion, it isn't a test." The previous sentence demonstrates a common testing anti-pattern; even if your code doesn't throw errors or crash, it doesn't mean it's a valid test.

In order to validate an API endpoint works properly, you must assert whether the API's expected output is correct or incorrect.

There are several assertions to choose from and below are a few examples, along with the accompanying documentation:

Exercise: Add an Assertion

  1. Open the previous test you created, called AssertionTest.
  2. Select +Add Request/Assertions, and select Assert Exists with the following data:
    • Expression: payload['Sauce Connect']
    • Mode: one
    • Click the check mark to save
  3. Next, select +Add Request/Assertions again. This time select Assert Is, and add the following data:
    • Expression: payload['Sauce Connect'].download_url
    • Type: url
    • Click the check mark to save
  4. Here's what the final result should look like: Sample Payload Test
  5. Click Save to save your test, then select Run > Default Downloader. The test results should look like this: Sample Test Results

Recap

In the next section we will cover how to store information as a Global Variable, or an Input Set.

As the complexity and number of tests in your test suite increase, it's a best practice to parametrize test details and data to allow more flexibility. There are generally two ways to store data within API Fortress:

Global Variables

The global variables (referred to as parameters in the API Fortress interface), are usually common variables designed to run with an entire test such as authentication API Key, or a domain name. Global variables can be used across different tests in a project.

To add a global variable/parameter select the Input Set tab to the right of the interface, and select Add Global Param.

Sample Test Results

Input Sets

An input set differs from a global parameter in that it is usually a group of input variables related to a specific scneario or contextual use case—for example a list of relevant product ids returned from a product API endpoint. Input sets are used within a single test.

To add an input set select the Input Set tab to the right of the interface, and select Input Set.

Create Global Parameters

Navigate and select the Input Set tab on the left side of the interface to begin the exercise.

Input Set Tab

  1. Select Add Global Parameters in the Global Parameters section Global Parameter
  2. Add the following data to the global parameter:
    • Name: domain
    • Value: saucelabs.comDomain Parameter

Next we need to substitute the domain value in the current GET request:

  1. In the menu to the left of the GET request component, select Edit componentEdit Component
  2. Substitute saucelabs.com with the ${domain} global parameter: Use Param
  3. Run the test again to ensure the test still passes with the same result.
  4. Create protocol and uri global parameters. The values should be https:// and /versions.json respectively
  5. Below is what the finished result should look like: Finished Result of Global Params

Note:

Negative : Please refer to the documentation for further information on using variables.

In the next section we will discover how to store some of the information we created in the Vault.

The Vault is a unique feature of the API Fortress platform that allows you to store information for use across all projects. While Input Sets are typically only used within the same test or project, the vault allows you to store things that can be used across any project.

The Vault allows you to save more than just variables, with the vault you can save, edit, and reuse almost anything including:

In the Vault, you can store data at three different levels: scope, project, and global. At the project level the vault will allow you to reuse those values across any test within that project scope. Similarly, the global level allows use of stored values across any test within any project.

Note:

Negative : If you plan on re-using code snippets from the Vault, make sure those variables remain consistent across each test. Also note that if you add an input set or global parameter with the same name, those values will override what is saved in the Vault.

Exercise: Use the Vault to Store a Snippet

To add a snippet to your account.

  1. Select the elements you wish to store (hold SHIFT and click first and last elements to select a range).
  2. Open the Vault side Menu. Then select the + icon next to Snippets: Select Snippets
  3. Fill in the following details for the snippet:
    • Name: QuickAssertionSnippet
    • Description: quick test to store snippet in the vaultSnippet Details
  4. Save the snippet. It should now appear in the Snippets section of the vault: New Snippet

Note

Negative : In order to delete a snippet, select Vault in the top menu, find the project where the snippet is stored, and click on the snippet to access the trash can icon. Invoke Snippet

Using a Snippet

If you wish to re-use this snippet in a different project, create a new empty test, and open up the Vault menu on the side. Choose the bottom element in your test, and click the arrow to either invoke the snippet, or to copy and paste the component:

Invoke Snippet

Now you just need to make sure variables are aligned:

Payload Variables

The finished result should be identical to your previous test:

Finished Result of Global Params

This approach is much easier than recreating the entire test from scratch! Run your test to see the report.

Adding and Editing Variables in the Vault

Access the Vault and add variables and code snippets by first clicking on the Vault in the main menu.

Vault in the Toolbar

From here you can access and edit code for snippets:

Access Snippet

Or access and edit variables for both global project-specific snippets and variables.

Edit Snippet Variables

To learn more about The Vault and Environments see below links: Learn the Basics, Environments Basics, Using Variables