Skip to main content

Writing API Tests with the Composer

The API Testing Composer enables you to quickly generate API functional tests (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

Creating a Test with the Composer

Create a Test

  1. In Sauce Labs, click API Testing.
Navigating to API Testing
  1. On the Projects page:

    • If you have no tests or projects yet, in the Write your own test box, click Use Composer.
    Navigating to the Composer
    • If you have a project but no tests, on the Projects page, click Write your own test.

    • If your project has tests, click Create Test and then click From Scratch.

      Navigating to the New Test window
  2. In the New Test box, enter a test name, test description (optional), and tags (optional), and then click Create Test.

New Test window
note

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

For more information, see Input Sets and Visual View and Code View.

Add Test Components

When test components are combined, they act as our test logic. See the following pages for more information about the components types available in API Testing:

Add an I/O Request Test Component

To create a simple GET request and validate that response is correct:

  1. In API Testing, on the Compose page, click Add child component.
Navigating to the Add component screen
  1. In the list of component options, click the GET component.
Navigating to the GET request window
  1. In the GET request window, in the Url field, enter https://api.us-west-1.saucelabs.com/rest/v1/public/tunnels/info/versions. This endpoint will return a JSON response body.

  2. In the Variable field, enter payload. This variable stores the response, so it can now be referred to as payload.

Editing in the GET request window
  1. Leave the rest of the fields blank and then click Save Changes.

The result should look like the following:

What the GET request should look like

For more information, see I/O Request Test Components.

Add an Assertion Component

  1. In API Testing, on the Compose page, click Add child component.
Navigating to the Add component screen
  1. In the list of component options, click the Assert Exists component.
Navigating to the Assert exists window
  1. In the Assert exists window, in the Expression field, enter payload.downloads. This expression checks for the downloads field in the json response body.

  2. Leave the rest of the fields blank and click Save Changes.

Confirm changes
  1. The result should look like the following:
What the Assert request should look like

For more information, see Assertion Test Components.

Additional Example

In the following example, the expression checks if the download_url value inside the Linux object is a valid URL.

  1. In API Testing, on the Compose page, click Add child component.

  2. In the list of component options, click the Assert Is component.

  3. In the Assert is window, in the Expression field, enter payload.downloads.linux.download_url. This expression checks for the download_url field in the json response payload.

  4. Leave the rest of the fields blank and click Save Changes.

Confirm changes
  1. The result should look like the following:
What the Assert request should look like

Run the Test

In the Composer, click Run.

Save and Run icons in the Composer

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

Test Runs in the Composer

Review Test Results

To view your results, in the Composer, in the Test Runs list, click the name of the test. This will open the Test Report Details. For more information, see Test Outcome Report.

Edit a Test

To edit a test at any time, on the Projects page, on the Tests screen, hover over a test name and then click Edit Test.

Navigating to the test editor

Test Options

Once you've generated your tests in the Composer, you can manage them from the Tests screen. In your project, on the Tests screen, hover your mouse over the test line item. You'll see icons that allow you to edit, run, schedule, or delete a test.

Test Options Icons
  • Pencil icon: Edit the test (opens the Compose tab)
  • Play icon: Run the test manually
  • Calendar icon: Open the test scheduler
  • Gauge icon: Opens the load testing page
  • Trash icon: Delete the test

Terminology

Visual View and Code View

This toggle switches between the Visual and Code views in the Composer. 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

Code View

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

Add Child 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.

Component Options

Click Edit to modify an existing component, or use the dropdown menu next to Edit to perform the actions shown below.

Component Options

Save

Saves your progress.

Save

Publish

Publishes your test.

Publish

Clear

Clears the most recent unpublished changes made to your test.

Clear

Run

Executes a test.

Run

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 specific to the test but are not related to a specific scenario (for example, api key).
  • 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.

If you define a variable in both the Global Parameters and Input Set sections, the value used in the test is what you specified in the Input Set.

Input Set with Visual ViewInput Set Visual View
Input Set with Code ViewInput Set Code View

Unit View

These buttons switch between the Input Set and Unit views.

Unit View

More Information