From f44e700453834c9ab1c27cb1d553d1d3626bd2c5 Mon Sep 17 00:00:00 2001 From: Vedanta Krishna Date: Wed, 15 Nov 2023 09:42:45 +0530 Subject: [PATCH] feat(pmapi): add support to get path of current request in sandbox script --- CHANGELOG.yaml | 4 ++++ lib/sandbox/execution-location.js | 17 +++++++++++++ lib/sandbox/pmapi.js | 12 +++++++++- test/unit/sandbox-libraries/pm.test.js | 33 ++++++++++++++++++++++++++ types/sandbox/prerequest.d.ts | 7 +++++- types/sandbox/test.d.ts | 7 +++++- 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 lib/sandbox/execution-location.js diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index f7f69ca3..789fbf47 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,3 +1,7 @@ +unreleased: + new features: + - GH-950 Added support for pm.execution.location + 4.3.0: date: 2023-11-02 new features: diff --git a/lib/sandbox/execution-location.js b/lib/sandbox/execution-location.js new file mode 100644 index 00000000..dcc6b323 --- /dev/null +++ b/lib/sandbox/execution-location.js @@ -0,0 +1,17 @@ +class ExecutionClass extends Array { + /** + * + * @param {string} current - Current item name, current item is the item whose script is being executed + * @param {string} rest - Arguments to pass to Array constructor + */ + constructor (current, ...rest) { + super(...rest); + this._current = current; + } + + get current () { + return this._current; + } +} + +module.exports = ExecutionClass; diff --git a/lib/sandbox/pmapi.js b/lib/sandbox/pmapi.js index be9df549..3582c65c 100644 --- a/lib/sandbox/pmapi.js +++ b/lib/sandbox/pmapi.js @@ -6,6 +6,7 @@ const _ = require('lodash'), PostmanResponse = sdk.Response, PostmanCookieList = sdk.CookieList, chai = require('chai'), + ExecutionClass = require('./execution-location'), /** * Use this function to assign readonly properties to an object @@ -269,7 +270,16 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore, * @excludeFromTestScript * @instance */ - skipRequest: onSkipRequest + skipRequest: onSkipRequest, + + /** + * Path of the request + * + * @instance + * @type {Array} + * @property {string} current - Name of the item whose script is executing. + */ + location: new ExecutionClass(execution.legacy.currentItem, ...execution.legacy.requestPath) } }, options.disabledAPIs); diff --git a/test/unit/sandbox-libraries/pm.test.js b/test/unit/sandbox-libraries/pm.test.js index bdaaf890..bb411d16 100644 --- a/test/unit/sandbox-libraries/pm.test.js +++ b/test/unit/sandbox-libraries/pm.test.js @@ -1087,5 +1087,38 @@ describe('sandbox library - pm api', function () { }); }); }); + describe('.location', function () { + it('should return the correct path of the request', function (done) { + context.execute({ + script: ` + var assert = require('assert'); + assert.deepEqual(Array.from(pm.execution.location), ['C1', 'R1']); + ` }, { + legacy: { + _itemName: 'request-name', + _itemId: 'request-id', + requestPath: ['C1', 'R1'], + currentItem: 'R1' + } + }, done); + }); + + describe('.current ', function () { + it('should return the correct current item', function (done) { + context.execute({ + script: ` + var assert = require('assert'); + assert.deepEqual(pm.execution.location.current, 'R1'); + ` }, { + legacy: { + _itemName: 'request-name', + _itemId: 'request-id', + requestPath: ['C1', 'R1'], + currentItem: 'R1' + } + }, done); + }); + }); + }); }); }); diff --git a/types/sandbox/prerequest.d.ts b/types/sandbox/prerequest.d.ts index 1142ade7..245264b7 100644 --- a/types/sandbox/prerequest.d.ts +++ b/types/sandbox/prerequest.d.ts @@ -1,4 +1,4 @@ -// Type definitions for postman-sandbox 4.2.8 +// Type definitions for postman-sandbox 4.3.0 // Project: https://github.com/postmanlabs/postman-sandbox // Definitions by: PostmanLabs // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -112,6 +112,11 @@ declare interface Execution { * @excludeFromTestScript */ skipRequest(): void; + /** + * Path of the request + * @property current - Name of the item whose script is executing. + */ + location: string[]; } /** diff --git a/types/sandbox/test.d.ts b/types/sandbox/test.d.ts index a8559413..7161ec32 100644 --- a/types/sandbox/test.d.ts +++ b/types/sandbox/test.d.ts @@ -1,4 +1,4 @@ -// Type definitions for postman-sandbox 4.2.8 +// Type definitions for postman-sandbox 4.3.0 // Project: https://github.com/postmanlabs/postman-sandbox // Definitions by: PostmanLabs // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -113,6 +113,11 @@ declare interface Visualizer { } declare interface Execution { + /** + * Path of the request + * @property current - Name of the item whose script is executing. + */ + location: string[]; } /**