Package detail

@apideck/postman-to-k6

apideck-libraries4kApache-2.01.13.0

Convert a Postman collection to k6 JavaScript

k6, postman, loadtest, load-test

readme

postman-to-k6-cover

Postman-to-k6

Converts a Postman collection to a k6 script.

The postman-to-k6 converter utilizes your Postman collection and converts all the Postman requests, including tests, variables, ... to K6 scripts that can be executed by K6 to run performance tests.

Github badge npm npm

This project is a friendly fork of the original grafana/postman-to-k6 converter, actively maintained and open for new contributions.

Feel free to contribute or pass any suggestion to improve postman-to-k6.

Content

Features

  • [x] Pre-request scripts.
  • [x] Test scripts.
  • [x] Variables (at all scopes + dynamic).
  • [x] Data files.
  • [x] Authentication methods (except Hawk).
  • [x] File uploads (experimental).
  • [x] postman.* interface (exceptions below).
  • [x] pm.* interface (exceptions below).
  • [x] Support for Postman Dynamic Variables & ReplaceIn function.
  • [x] Global variables exposed by Postman: globals environment data iteration.
  • [x] xml2Json conversion.
  • [x] All Postman Schema versions.

Installation

Local Installation (recommended)

While possible to install globally, we recommend that you, if possible, add the converter to the node_modules of your test project using:

$ npm install -D @apideck/postman-to-k6

or using yarn...

$ yarn add @apideck/postman-to-k6

Note that this will require you to run the converter with npx @apideck/postman-to-k6 your-postman-file or, if you are using an older versions of npm, ./node_modules/.bin/postman-to-k6 your-postman-file.

Global Installation

$ npm install -g @apideck/postman-to-k6

Usage

To convert an exported collection to a k6 script:

$ postman-to-k6 collection.json -o k6-script.js

Then run the script in k6, as usual, using:

$ k6 run k6-script.js

Video Introduction

Together with Nicole van der Hoeven & Paul Balogh from the K6 team, we talked about "postman-to-k6" in the K6 Office Hours podcast. We talked about how to convert a Postman collection to a k6 load test, the advantages of Postman, and the benefits of reusing automation testing scripts for performance.

Postman for load testing using k6, with Tim Haselaars (k6 Office Hours #43)

Watch it on Youtube >> https://www.youtube.com/watch?v=Be66Db4wHLA

Topics covered:

  • Use cases for k6
  • Quick tour of Postman
  • Why Postman is not designed for load testing
  • Demo of postman-to-k6
  • How to use Portman to generate tests from an OpenAPI spec

Options

Iterations

Configures how many times the script will be executed before completion.

Flag Verbose Default
-i --iterations 1

Example:

$ postman-to-k6 collection.json --iterations 25 -o k6-script.js

Environment Variables

Provide environment variables from a JSON file.

Flag Verbose Default
-e --environment N/A

Example:

$ postman-to-k6 collection.json --environment environment.json -o k6-script.js

Global Variables

Provide global variables from a JSON file.

Flag Verbose Default
-g --global N/A
$ postman-to-k6 collection.json --global globals.json -o k6-script.js

CSV Data File

Provide a data file in the CSV format.

Flag Verbose Default
-c --csv N/A
$ postman-to-k6 collection.json --csv data.csv -o k6-script.js

JSON Data File

Pass in a data file in the JSON format.

Flag Verbose Default
-j --json N/A
$ postman-to-k6 collection.json --json data.json -o k6-script.js

K6 Param Options File

Pass K6 parameter options as a file in JSON format.

Flag Verbose Default
--k6-params N/A
$ postman-to-k6 collection.json --k6-params k6-params.json -o k6-script.js

K6 Handle Summary as JSON

Output the K6 summary as a file in JSON format. This will add the K6 handleSummary(data) to the generated script, providing the functionality that K6 will store the summary output as JSON file locally.

Flag Verbose Default
--k6-handle-summary-json N/A
$ postman-to-k6 collection.json --k6-handle-summary-json summary-report.json -o k6-script.js

K6 Request tag

Generate K6 request name tags based on available naming strategies:

  • none: no automatic generated tags | default
  • request: uses the request name as tag (example "Show all accounts")
  • folder-request: uses Postman folder name and the request name (example: "Accounts - Show all accounts")
Flag Verbose Default
--k6-request-tagging N/A

Example for request strategy

$ postman-to-k6 collection.json --k6-request-tagging=request -o k6-script.js

Example for folder-request strategy

$ postman-to-k6 collection.json --k6-request-tagging=folder-request -o k6-script.js

Separate

Split requests into separate files, for easier rearrangement of the logic.

Flag Verbose Default
-s --separate false
$ postman-to-k6 collection.json --separate -o k6-script.js
$ postman-to-k6 collection.json -s -o k6-script.js

Skip Pre

Skips any pre-request scripts during conversion

Flag Verbose Default
--skip-pre false
$ postman-to-k6 collection.json --skip-pre -o k6-script.js

Skip Post

Skips any post-request scripts during conversion

Flag Verbose Default
--skip-post false
$ postman-to-k6 collection.json --skip-pre -o k6-script.js

CLI options file

Manage all the CLI options in a separate configuration file and pass them along to the postman-to-k6 command. To make the CLI usage easier, especially in CI/CD implementations.

All the available CLI options can be used in the config file. By passing the CLI options as parameters, you can overwrite the defined CLI options defined in the file.

Flag Verbose Default
--cli-options-file false
$ postman-to-k6 collection.json --cli-options-file cli-config.json

Example of JSON CLI config file

{
    "output": "k6-script.js",
    "k6-params": "config/k6-params.json",
    "environment": "config/envs/team.env.json",
    "separate": true
}

Examples

A collection of Postman examples are located under example. To run one of the examples, just run it as you would any other command:

$ postman-to-k6 example/v2/echo.json -o k6-script.js

Unsupported Features

  • Sending requests from scripts using pm.sendRequest.
  • Controlling request execution order using postman.setNextRequest.
  • Cookie properties, like hostOnly, session, and storeId.
  • Postman methods:
    • pm.response.reason
    • pm.response.to.have.status(reason)
    • pm.response.to.not.have.status(reason)
    • pm.request.auth
  • The Hawk authentication method.
  • Deprecated xmlToJson method.
  • Request IDs are changed. Postman doesn't provide them in the export, so we have to generate new ones.

Resources

A collection of blog posts and resources about postman-to-k6

Other similar tools

Credits

Special thanks to the K6 team from Grafana for open-sourcing & growing the converter and contributing it to the community. Thanks to bookmoons for creating this tool. Also, thanks to borjacampina for creating the original incarnation of the tool.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

[1.13.0] - 2024-08-18

  • Implement Atob via K6 encoding (#86)
  • Extended pm.request with support for headers, url, method, name, id, body, body.raw (#136)

[1.12.0] - 2024-07-31

  • Fix initialization of headers for each K6 request (#98)
  • Bumped dependencies: uuuid, postman-collection, oauth-1.0a

[1.11.0] - 2024-07-12

  • Handle mixing ESM and commonJS to be ready for k6 v0.53.0

[1.10.0] - 2024-06-13

  • Bump minimal Node version to 18.x
  • Code refactoring with optional chaining to provide JS safe code

[1.9.0] - 2024-04-01

  • Support for pm.response.headers.all() & pm.response.headers.get() (thanks to the contribution of @aegrey)

[1.8.10] - 2024-01-05

Security

  • Upgraded dependencies: crypto-js 4.2.0, postman-collection 4.3.0, postman-collection-transformer 4.1.8

[1.8.9] - 2023-09-28

Fixed

  • Docker build -t postman-to-k6 (#114)

Security

  • Upgraded dependencies: get-func-name, word-wrap, semver, xmi2js

[1.8.8] - 2023-02-09

Fixed

  • Fixed missing module prettier (#96)

[1.8.7] - 2023-01-14

Added

  • Support for Postman files upload (experimental)(#93, #28, #83)
  • Build postman-to-k6 as a Docker image

Security

  • Bump json5 from 2.1.3 to 2.2.3

[1.8.6] - 2022-11-12

Security

  • Bumped minimatch from 3.0.4 to 3.1.2 and other vulnerable dependencies

[1.8.5] - 2022-08-11

Fixed

  • Added missing scripts to the package.json file.

[1.8.4] - 2022-08-11

Fixed

  • Extend pm.variables.replaceIn to support Postman static & dynamic variables (#69)
  • Transform Postman API exported wrapped collection & environment objects (#67)
  • Enhanced auto-import of libs (#68)
  • Replace the Postman variables in the global, environment and collection scopes (#64)
  • Use correct directory paths for importing libs (#49)
  • Auth headers with - in the key name are handled properly (#62)
  • Bump dependencies like husky, urijs, papaparse, chai, aws4, shell-quote, trim-off-newlines, minimist (#70)

[1.8.3] - 2021-11-22

Fixed

  • Allows variables that evaluate to falsy to be returned (#23)

[1.8.2] - 2021-10-08

Changed

  • Temporary fix to overcome missing optional chaining support

[1.8.1] - 2021-09-20

Changed

  • Minor enhancement to convert the CLI parameter to camelCased options for easier usage in the CLI options file

[1.8.0] - 2021-09-18

Added

  • Added the option to generate K6 Request tags for reporting purpose.
  • Added option to use a file to pass all CLI options.

[1.7.0] - 2021-09-01

Added

  • Added option to generate a K6 JSON summary through the handleSummary() callback.

Changed

  • Updated readme
  • Migrated the "integration" tests to use Ava snapshots instead of the hardcoded comparison for easier maintenance

[1.6.1] - 2021-08-18

Fixed

  • Fixes a bug that when using the --skip-pre/--skip-post options was passed, the converter did not remove the pre-request/post-request scripts at the collection or folder level. (grafana#105)

Changed

  • Updated readme to remove the docker hub reference to prevent confusion with the original package, since this fork does not build dockers.

[1.6.0] - 2021-08-18

Added

  • Extend support for Postman random functions/dynamic variables (grafana#92)
  • Exclude disabled headers when converting requests (grafana#103)
  • Implement postman replaceIn function
  • Extended tests for url encode
  • Extended tests for encoding of space characters
  • Extended test to include checks for randomPhoneNumber & isoTimestamp

Security

  • Bumped dependencies for ajv, browserify, eslint, lodash, postman-collection, postman-collection-transformer, strip-json-comments, urijs
  • Bumped dev dependencies for ava

[1.5.1] - 2021-08-16

Fixed

  • Bug fix for unwanted conversion of Postman query variables (#106 / #104)

1.1.0 - 2020-05-22

Added

  • Support for api key authorization.

1.0.0 - 2020-05-14

Added

  • Switched code style from standardjs to eslint and prettier
  • ci workflows moved to github actions
  • Automated deployment of version tags to dockerhub and npm registry

0.5.0 - 2020-03-24

Added

  • Support for tags
  • Body in aws4 auth calls

Fixed

  • Dependency versions

0.4.0 - 2019-11-21

Added

  • Support for file uploads.

Fixed

  • Support empty query string in requests using AWSv4 signature authentication

0.3.1 - 2019-10-28

Fixed

  • Polyfill for Object.setPrototypeOf method when a conversion uses any dependency requiring it.

0.3.0 - 2019-08-23

Added

  • Support GraphQL variables.

0.2.0 - 2019-08-22

Added

  • Dockerfile and installation instructions on how to use Docker image from DockerHub.
  • Support for injecting OAuth1 credentials when converting a collection.
  • Support for splitting requests into separate JS files for easier rearrangement of logic post-conversion.
  • Support for GraphQL body mode.

Fixed

  • Resolution of variables in request bodies.

0.1.2 - 2019-03-28

Fixed

  • Support alternate no-body encoding.

0.1.1 - 2019-03-26

Fixed

  • Postinstall command.
  • Don't ignore scripts folder when packaging npm package.

0.1.0 - 2019-03-26

Added

  • Support for prerequest scripts.
  • Support for test scripts.
  • Support for variables (at all scopes + dynamic).
  • Support for data files.
  • Support for authentication methods (except Hawk).
  • Support for postman.* interface.
  • Support for pm.* interface.
  • Support for global variables exposed by Postman: globals environment data iteration.
  • Support for xml2Json conversion.
  • Support for file formats v2 and v2.1.

Updated

  • Installation and usage instructions to recommend nvm to avoid filesystem permission issues when installing the tool globally with npm install -g ...