Skip to main content

Writing API Tests with the Composer

The API Test Compose tab (aka Composer) enables you to generate sophisticated API functional tests in minutes (no coding experience required) and/or code them from scratch. You can reuse these tests as end-to-end integration tests and load (stress) tests. In turn, load tests can be reused as monitors for performance testing.

What You'll Need#

How to Write a Composer Test#

Create a Test#

  1. Log in to Sauce Labs, then click API TESTING > Get Started.
    API Testing landing page
  2. Go into any Project.
  3. On the Tests tab, click Create Test > From Scratch.
    Create a Test button
  4. In the New Test fields, enter a Test Name (required), Test Description (optional), and Tag(s) (optional), then click Save.
    Enter Test Details
  5. Make sure you're now in the Compose tab (aka Composer) with the Input and Visual views toggled.
    Add Components from Composer
    Composing tests

    You can use either Visual composer (guides you through building components - no coding required) or Code composer (requires you to write code from scratch). For this guide, we're using Visual.

To go back and edit a Test at any time, go into your Project's Tests tab, hover over a test name, then click Edit Test (pencil icon).
Components available

Add Test Components#

The next of set steps is to add the necessary test components. When combined, they act as our test logic. You can choose from I/O (Request) Components, Assertion Components, and Logical Components.

Add I/O Request Component#

Create a simple GET request and validate that response is correct.

  1. Click the Add Component button (+ icon).
    Add Request Component
  2. Select the GET request component.
    GET request Component
  3. In the Url field, write https://api.us-west-1.saucelabs.com/rest/v1/public/tunnels/info/versions. This URL will return a json response body.
  4. In the Variable field, write payload. This variable stores the response.
    GET request fields
  5. Leave the rest of the fields blank and click Save. This is what the end result will look like:
    GET request end result
tip

For detailed information, see Request (I/O) Components.

Add Assertion Component#

  1. Click the Add Component button (+ icon).
    Add Request Component
  2. Select the Assert Exists assertion component.
    Assert Exists Component
  3. Edit the following details:
    • Expression: payload.downloads - This expression checks for the field "downloads" in the json response body.
      Assert Exists Details
    • Leave the rest of the fields blank and click Confirm changes (checkmark icon).
tip

For detailed information, see Assertion Components.

Run Test#

  1. Before you run the test, select the Save icon.save icon at the top of the Composer.
  2. Then select the Run icon run test icon directly next to the Save icon.Test Composer Tool Bar

All test runs appear to the right of the Composer, underneath the test details and environment sections.

Test Runs Section

Review Test Results#

To view your results, click on the test run. This will open the Test Outcome Report, which displays information regarding the test.

Terminology#

Visual View and Code View#

This button toggles back and forth between the Visual and Code Test Composer views. You can make calls and add assertions for testing your APIs, and insert variables wherever needed. You can use either, depending on which you're more comfortable with.

Visual View#

Guides you through creating API tests using automated real-time suggestions via predictive text. No coding experience is required.
Test Composer Visual View Pic

Code View#

Enables you to write tests here from scratch, if you feel more comfortable working in code.
Test Composer Code View Pic

Add Component#

This button displays all available assertion components, I/O components, and logical components.

Add Component

If a component is not valid for the operation you are conducting, it will not be made available to help avoid mistakes. For instance, if you don’t add a POST first, you cannot add a POST Body or POST Param.

note

Sauce Labs free trials may not give you access to all available components.

Transform Component#

Transforms an existing component into another component of the same type.

Add Component

Delete Component#

Deletes a selected component from the test while using Visual view.

Delete Component

Invoke Snippet#

Allows you to use a previously created code snippet stored in The Vault.

Invoke Snippet

Export Snippet#

Allows you to export a selected code snippet to the vault in order to be re-used later, or in another test.

Export Snippet

Save Test#

Saves your progress.

Save Test

Run Test#

Executes a test.

Run Test

Input Sets#

Displays the Input Set view where you can store input data sets to reuse within the specific test you're working on.
Input Sets

There are two types of input data sets you can use:

  • Global Parameters: variables that are available within a test, valid for that specific test only.
  • Input Set: group of input variables representing a scenario, valid for that specific test only. The test will be executed once for each input set, overriding the variable values into your test.
Input Set with Visual View Input Set Visual View
Input Set with Code ViewInput Set Code View

Unit View#

This button toggles back and forth between the Input Set and Unit Test Composer view.

Unit View

More Information#