Skip to main content

Other Test Components

Here are some other components that you can add to a test using the Composer. To access them, go to a Project > Test > Compose (aka Composer) > Click Add component (+ icon) in the Composer toolbar.

Other Components

What You'll Need

Tag

The Tag component within the Composer enables you to dynamically tag the resulting document of a test execution. You can add different tags based on dynamic events happening during the test execution, such as a certain value retrieved in the payload. You can assign multiple tags to each test by adding more 'tag' components to it.

This is different from the tags applied when you first create the test. That feature enables you to search for that tag(s) filter in your Project Tests tab or Dashboard tab.

Parameters
Fields
Value

| REQUIRED | STRING |

Examples

Here's an example of how you could use a tag in the Compose tab.

tagComposer.jpg

You can also add tags to the test details screen when you create or edit a test.
Tag Component - create new test

Static tags will be displayed in your Tests list.
Tests tag.jpg

All tags, dynamic and static will mark the test execution documents. On your project Dashboard, you can filter events by tags.
projDashTag.png

Set (Variable)

In Sauce Labs API Testing you can create variables in several different ways by using SET (variable). This component has different modes that allow you to create variables in different ways, such as String, Data, and Language.

Set var component
Parameters
Fields
Var

| REQUIRED | STRING |

Variable mode

| REQUIRED | STRING, DATA, or LANGUAGE |

Value (depends on ‘Variable mode = String’)

| REQUIRED | DATA |

Data (depends on ‘Variable mode = Data’)

| REQUIRED | DATA |

Lang (depends on ‘Variable mode = Language’)

| REQUIRED | JAVASCRIPT or TEMPLATE |

Content (depends on ‘Variable mode = Language’)

| REQUIRED | DATA |

Variable Mode: String

This mode generates a String variable, which can be a static value or a variable taken from the response payload. If it is a static value you just have to write it and the engine will take it as is.

Consider the following example:

Set static string
Var: product
Variable mode: String
Value: t-shirt

In the above example the variable product will always have the value t-shirt.

If you want to make it dynamic and let the engine evaluate the value every time the test will be executed, you have to write it in ${...} brackets.

Consider the following response payload and let’s say it has been stored in the payload variable:

{
"id": 1,
"name": "Baseball Cap",
"price": 29.99,
"category": "1",
"description": "This is product 1!",
"quantity": 5,
"imageURL": "http://image.com",
"color": [
"blue",
"yellow"
],
"createdAt": "2021-11-28T21:58:43.461Z",
"updatedAt": "2021-11-28T21:58:43.461Z"
}

If you write the following:

Set dynamic value
Var: product
Variable mode: String
Value:${payload.name}

The engine will evaluate the variable value every time the test will be executed. In the above scenario the variable product will contain the value Baseball Cap but if the response is the following:

{
"id": 2,
"name": "Long Sleeve Shirt",
"price": 39.99,
"category": "1",
"description": "This is product 2!",
"quantity": 7,
"imageURL": "http://image.com",
"color": [
"blue",
"yellow",
"red"
],
"createdAt": "2021-11-28T21:58:43.461Z",
"updatedAt": "2021-11-28T21:58:43.461Z"
}

The value will be Long Sleeve Shirt, without changing your test.

Variable mode: Data

Using this mode, the variable will be evaluated (like the Expression field), therefore the variable type can be everything. The variable type will depend on the object being evaluated. In the Data field, you need to enter a single line expression that returns a value.

For example, you can create a new array in this way:

Set array
Var: products
Variable mode: Data
Data: ["Bluetooth Headphones","Long Sleeve Shirt","Baseball Cap"]

Then, you can iterate over it using the each component or you can invoke a specific item using ${products[1]} where the number inside the square brackets identifies the position of the item you want to reach out, starting from 0.

Now, let's consider the example below and imagine we have the JSON payload stored in the payload variable:

[
{
"id": 1,
"name": "Baseball Cap",
"price": 29.99,
"category": "1",
"description": "This is product 1!",
"quantity": 5,
"imageURL": "http://image.com",
"color": [
"blue",
"yellow"
],
"createdAt": "2021-11-28T21:58:43.461Z",
"updatedAt": "2021-11-28T21:58:43.461Z"
},
{
"id": 2,
"name": "Long Sleeve Shirt",
"price": 39.99,
"category": "1",
"description": "This is product 2!",
"quantity": 7,
"imageURL": "http://image.com",
"color": [
"blue",
"yellow",
"red"
],
"createdAt": "2021-11-28T21:58:43.461Z",
"updatedAt": "2021-11-28T21:58:43.461Z"
},
{
"id": 3,
"name": "Bluetooth Headphones",
"price": 49.99,
"category": "1",
"description": "This is product 3!",
"quantity": 50,
"imageURL": "http://image.com",
"color": [
"green",
"yellow"
],
"createdAt": "2021-11-28T21:58:43.462Z",
"updatedAt": "2021-11-28T21:58:43.462Z"
}
]

If you write the following:

Set data
Var: product
Variable mode: Data
Data: payload.filter(it=>it.name=='Bluetooth Headphones')

It will return the following object:

{
"id": 3,
"name": "Bluetooth Headphones",
"price": 49.99,
"category": "1",
"description": "This is product 3!",
"quantity": 50,
"imageURL": "http://image.com",
"color": [
"green",
"yellow"
],
"createdAt": "2021-11-28T21:58:43.462Z",
"updatedAt": "2021-11-28T21:58:43.462Z"
}

Variable mode: Language

This is the most advanced way to create your variables. There are two different options available: Javascript and Template.

Lang: Javascript

In this mode you can create your variable by writing a Javascript script in the Content field. It can be a complete script with variable declarations or loops.

For example, you have a JWT token stored in the token variable and we need to decode it and return the JSON payload it was generated from:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2NTgyMzY1NjgsImV4cCI6MTY4OTc3MjU2OCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoiam9obi5kb2VAZXhhbXBsZS5jb20iLCJOYW1lIjoiSm9obiIsIlN1cm5hbWUiOiJEb2UiLCJFbWFpbCI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DN7vKPlHkAy1hwYOYpUKDwkV0yD-KS2pdoc76aKPhm8

To achieve this, you need to write the following script inside the Content field:

var pieces = token.split('.')
var b64payload = pieces[1]
var decoded = Buffer.from(b64payload,'base64').toString()
var json = JSON.parse(decoded)
return json
Set javascript

That’s producing the following JSON:

{
"iat": 1658236568,
"exp": 1689772568,
"aud": "www.example.com",
"sub": "john.doe@example.com",
"Name": "John",
"Surname": "Doe",
"Email": "john.doe@example.com",
"Role": ["Manager", "Project Administrator"]
}

Then, you can retrieve all the keys as jsonData.iat where jsonData is the variable name you entered in the Var field.

Lang: Template

In this mode you can create your own template in the same way as it is done for Request Body, the advantage here is that you can print the variable in order to check if all is correct (Body cannot be printed).

For example, if you need to add a new product in your database, you can create the body for the (PUT) request and paste the Body in the Content field and print it in a Comment.

{
"id": 4,
"name": "T-Shirt",
"price": ${price},
"category": "1",
"description": "This is product ${id}!",
"quantity": 5,
"imageURL": "http://image.com",
"color": ["red", "green"],
"createdAt": "${D.format (D.nowMillis(), 'yyyy-MM-DD')}",
"updatedAt": "${D.format (D.nowMillis(), 'yyyy-MM-DD')}T${D.format(D.nowMillis(), 'HH:mm:ssz')}"
}
Set template

Parse

This component allows you to parse a string into structured data, using one of the available parsers.

Parameters
Fields
parseVar

| REQUIRED | STRING |

The name of the variable you want to parse.

Adapter

| REQUIRED | json, xml, etc. |

The parser to use

Parse component UI

Examples

I have set a variable (which will be a plain string), in this case I have a list of colors.

Parse component variable

Next, we can use the comment component to see what happens if I print “colors[1]” before and after parsing it into JSON.

Parse component test

Here are the results of the above test:

Parse component test

As you can see before parsing the string, the test will consider the variable colors as one big string so colors[1] will print “ as that is the second character in the string. After parsing the string into JSON we can traverse through the variable as a JSON, so colors[1] will print the second element in the JSON array blue.

Comment

This assertion allows you to print out (in test reports) information. It can have two sorts of values, as described below.

Parameters

There are no parameters -- text only.

Examples

The first is a normal string value. An example of that would be to explain what a specific WHEN loop is being used for. Similar to when you write comments in code.

This is a comment

The second is useful for test debugging and analysis. You can pass variables into the comments. An example use of this would be to print out the product ID being used in the current loop of a test.

The value of the ID is ${payload.id}

Flow

This component allows you to pause or stop a test entirely.

Parameters
Fields
Command

| REQUIRED | 'stop', 'wait' |

This parameter defines the action you want to take. 'Stop' will stop the test. 'Wait' will pause the test for a number of milliseconds defined in the 'Value' parameter.

Value

| REQUIRED | INTEGER |

Depends on 'Command = wait'. The number of milliseconds you want to pause the test for.

Examples

This component is especially useful when combined with the If component. See the examples below:

flow_stop.jpg

If the statusCode is not 200, the test will be halt; none of the remaining assertions will be checked.

flow_wait.jpg

In this example, the test will wait 1000 milliseconds before performing the GET request.

Fact

The Fact component enables you to add information (facts) about the nature of the test execution, along with static or dynamic data. It's used to control the behavior of Email notifications, which (if enabled) alert you to test failures.

Test activity is tracked using test ID number. This may not work if you're testing in multiple environments (i.e., production, staging, QA), as an incident could be environment-specific. When a Fact component is added to a test, it will inform our system which environment the execution relates to so that the incident signature will carry the environment as well.

Parameters
Fields
ID

| REQUIRED | STRING |

Should be unique within the test.

Label

| REQUIRED |

To provide an understanding of the Fact.

Value

| REQUIRED | STRING |

String value that supports the template language (i.e., ${...}).

caution

The Fact component should be set as high up in the test as possible. If the test fails before it reaches the Fact, then it will not be set.


Examples/Uses Cases

Setting Alert Environments

Assume that in the variable scope of your test, you have a variable called env that contains your environment string (production, staging, qa etc.).

By configuring a Fact in the following way, you can add the environment value to the incident signature:

Fact id: environment  
label: The current environment
value: ${env}
screenshot.png

From this moment on, the signature of the incident will be id_of_the_test + value_of_environment.

For example, you will receive start/end incidents for test123 in the production environment, and start/end incidents for test123 in the staging environment, as separate flow of events.

You can use anything as a value of the environment, such as domain names and IDs.

Disabling Email Notifications

A second use case is disabling email notifications for the test from within the test:

Fact id: disable_alerts   
label: whatever you want here
value: true
screenshot.png

You can use logic within the test to set the Fact component and use that to alter the email notification.

As an example, you could say "IF the env is development, then disable emails for this test":

screenshot.png

Setting Email Notification Thresholds

Another use-case of the fact component is set an email alert threshold. If you want a test to fail more than once before an email is sent, a Fact called mail_threshold can be set in the test:

screenshot.png

This means the test will need to fail twice in a row before an email alert is sent.

Given that this can be configured within the test, it offers all the flexibility provided by conditional statements, such as an IF condition on the environment the test is running upon:

screenshot.png

K/V Store

The Key/Value store allows you to create temporary key/value pairs that can be accessed across different tests. The Key/Value store is accessed via the Key/Value Store Component.

danger

These Key/Value pairs are temporary. They expire after 24 hours has elapsed since the last update to the value itself.

Methods

The Key/Value Store component has four methods available for use:

  • Set: creates a new key/value pair in the Key/Value store. The value is entered in the Object field.
    screenshot
  • Load: recalls a value from the Key/Value store when provided with a key.
    screenshot
  • Push: adds a value to the end of an existent value of the datatype "Array" in the Key/Value store. If no such key exists, it will create a new array containing the passed in value.  The passed in value is entered in the Object field.
    screenshot
  • Pop: removes a value from the end of an existent value of the datatype "Array" in the Key/Value store.
    screenshot

Basic Workflow

Let's take a look at how this workflow works in a practical setting. The first example will be a simple set and retrieve of a value in the Key/Value Store.

  1. First, we'll make a GET request to an endpoint.
    screenshot
  2. Next, we'll add a K/V Store component.
    component.png
  3. This first K/V Store component (we're going to incorporate several) is going to set the Key/Value pair in the Store, so we're going to use Set.
    screenshot.png
  4. In this case, we're setting the Key "prods" equal to products[0].name, which in this case evaluates to "Baseball Cap."
  5. Next, we're going to retrieve this Key/Value pair from the store with the Load method. In the K/V Store Load component, we're going to assign the retrieved value to the variable kvprods.
    screenshot.png
  6. Finally, we'll add in a Comment component to ensure that the data was recovered successfully.
    screenshot.png
  7. When we run the test, we're presented with the following result:
    screenshot.png

Success!

Push/Pop Workflow

Next, we're going to take a look at how Push and Pop work. Push and Pop are both array methods and behave as they normally do outside of this context. Push will append a value to the end of an array. Pop will remove the last value in an array.

First, we're going to use Push. It should be noted that Pop works similarly but with the opposite result. Pop also assigns the removed value to a variable which can be used in the context of the test, but can no longer be accessed from the Key/Value Store. We'll discuss this further when we take a look at Pop.

  1. First, we're going to send a GET request and assign a key in the Key/Value Store to a value from the response body. In this case, we're going to use "Color," which is an array.
    screenshot.png
  2. Next, we're going to Load and Comment this key. We're doing that so we can actually see the change on the test report at the end of this workflow.
  3. The next step is to Push the new data on to the end of the existing array. In this case, we're pushing the integer 999 onto the prods array.
    screenshot.png
  4. Finally, we're going to Load the modified data into the test from the K/V Store.
    screenshot.png
  5. When we run the test, we're presented with the following test report.
    screenshot.png

The comments show us clearly that we have pushed the number 999 onto the array stored in the key prods.

Now, we've added something to the array. Let's remove it with Pop!

  1. The first step is to introduce a Pop K/V Store component.
    screenshot.png

    We provide the Pop component with the name of the key from the Key/Value Store, and the name of the variable we'd like to assign the popped value to. Remember, Pop removes the last value in an array and returns the value itself. In this case, we're going to assign it to a variable called "Popped."

  2. Next, we're going to recall the modified key from the Key/Value Store. Then, we're going to Comment both the recalled Key/Value Store value AND the previously popped value.
    screenshot.png

  3. Review the Test Report, where you can see the full workflow, showing that we first assigned an array to the Key/Value Store with Set, then added to that array with Push, and then removed the added value with Pop. Each time we made a change, we used Load to retrieve an updated value from the Key/Value Store.
    screenshot.png

The last two comments show the final state of the array in the Key/Value Store and the popped value itself. The popped value will only be available within the scope of this test run. The array in the Key/Value Store will remain retrievable and until 24 hours after it's most recent modification.

note

Use "Set," "Push," and "Pop" to reset the timer. "Load" does not reset the timer.