Skip to content
On this page

Empower Your Test Results with AI-Powered Insights ->

stores

stores method lets use save response data under Data Management which allows to refer the saved data anywhere in the tests using special syntax - $S{<variable>}.

Syntax

js
stores(name, path, options);
stores(name, handler_name, options);
stores(callback_function);

Usage

โœ… Correct Usage

js
// stores single item
await spec()
  .get('/posts')
  .stores('FirstPostId', '[0].id');
js
// stores multiple items in an array
const ids = await spec()
  .get('/posts')
  .stores('FirstPostId', '[0].id')
  .stores('SecondPostId', '[1].id');
js
// using handlers
const postID = await spec()
  .get('/posts')
  .stores('FirstPostId', '#GetFirstPostId');

Arguments

> name (string)

Name of the variable.

> path (string)

Json path. See json-query for more usage details.

If the path starts with one of the value, it has a special meaning

pathdescription
req.pathParamsRequest path params
req.queryParamsRequest query params
req.headersRequest headers
req.cookiesRequest cookies
res.bodyResponse body (this is default)
res.headersResponse headers
res.cookiesResponse cookies

> handler_name (string)

Name of the capture handler to use.

> cb ((request: Request, response: IncomingMessage & {body: Record<string, any>, json: Record<string, any>}) => T)

A custom function which should return an object to store the response value for pactum.

> options? (object)

Options to pass to the handler.

  • options.status? - run the handler only on the given status. Can be either 'PASSED' or 'FAILED'
  • options.append? - appends the stored data in an array.

Examples

Store single value

js
const { spec } = require('pactum');

await spec()
  .get('http://jsonplaceholder.typicode.com/posts')
  .expectStatus(200)
  .stores('FirstPostId', '[0].id');

await spec()
  .get(`http://jsonplaceholder.typicode.com/posts/{id}/comments`)
  .withPathParams('id', '$S{FirstPostId}')
  .expectStatus(200);

Using Capture Handlers

js
const { spec, handler } = require('pactum');

handler.addCaptureHandler('first post id', (ctx) => {
  return ctx.res.json[0].id;
});

await spec()
  .get('http://jsonplaceholder.typicode.com/posts')
  .expectStatus(200)
  .stores('FirstPostId', '#first post id');

await spec()
  .get(`http://jsonplaceholder.typicode.com/posts/{id}/comments`)
  .withPathParams('id', '$S{FirstPostId}')
  .expectStatus(200);

Using custom function

js
const { spec, handler } = require('pactum');

await spec()
  .get('http://jsonplaceholder.typicode.com/posts')
  .expectStatus(200)
  .stores((request, response) => {
    return {
      custom_func_id: response.body[0].id,
    };
  });
await spec()
  .get(`http://jsonplaceholder.typicode.com/posts/{id}/comments`)
  .withPathParams('id', '$S{custom_func_id}')
  .expectStatus(200);

Store value when request fails

js
const { spec } = require('pactum');

await spec()
  .get('http://jsonplaceholder.typicode.com/posts')
  .expectStatus(200)
  .stores('FirstPostId', '[0].id', { status: 'FAILED' });

Append value to the store

js
const { spec } = require('pactum');

await spec()
  .get('http://jsonplaceholder.typicode.com/posts/1')
  .expectStatus(200)
  .stores('UserIds', 'id', { append: true });

await spec()
  .get('http://jsonplaceholder.typicode.com/posts/2')
  .expectStatus(200)
  .stores('UserIds', 'id', { append: true });

See Also

Released under the MIT License.