From 4f5af39f4d65818b12a4b479ee3c131a181af8ca Mon Sep 17 00:00:00 2001 From: Vedanta Krishna Date: Thu, 16 Nov 2023 11:23:12 +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 | 21 +++++++++++++++- test/unit/sandbox-libraries/pm.test.js | 33 ++++++++++++++++++++++++++ types/sandbox/prerequest.d.ts | 13 +++++++++- types/sandbox/test.d.ts | 13 +++++++++- 6 files changed, 98 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..9f763978 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,25 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore, * @excludeFromTestScript * @instance */ - skipRequest: onSkipRequest + skipRequest: onSkipRequest, + + /** + * @interface ExecutionLocation + * @extends Array + */ + /** + * Name of the current item + * + * @name ExecutionLocation#current + * @type {string} + */ + /** + * Path of the current execution context + * + * @type {ExecutionLocation} - current execution path + * @instance + */ + 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..c85ac0af 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,17 @@ declare interface Execution { * @excludeFromTestScript */ skipRequest(): void; + /** + * Path of the current execution context + */ + location: ExecutionLocation; +} + +declare interface ExecutionLocation extends Array { + /** + * Name of the current item + */ + current: string; } /** diff --git a/types/sandbox/test.d.ts b/types/sandbox/test.d.ts index a8559413..2df4c6df 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,17 @@ declare interface Visualizer { } declare interface Execution { + /** + * Path of the current execution context + */ + location: ExecutionLocation; +} + +declare interface ExecutionLocation extends Array { + /** + * Name of the current item + */ + current: string; } /**