Skip to content

Commit

Permalink
feat(pmapi): add support to get path of current request in sandbox sc…
Browse files Browse the repository at this point in the history
…ript
  • Loading branch information
vedkribhu committed Nov 15, 2023
1 parent 61e893f commit be79bf8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
unreleased:
new features:
- GH-950 Added support for pm.execution.location

4.3.0:
date: 2023-11-02
new features:
Expand Down
17 changes: 17 additions & 0 deletions lib/sandbox/execution-location.js
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 11 additions & 1 deletion lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -269,7 +270,16 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
* @excludeFromTestScript
* @instance
*/
skipRequest: onSkipRequest
skipRequest: onSkipRequest,

/**
* Complete Path of the current request being executed.
*
* @instance
* @type {Array<string>}
* @property {string} current - Name of the current item being executed.
*/
location: new ExecutionClass(execution.legacy.currentItem, ...execution.legacy.requestPath)
}
}, options.disabledAPIs);

Expand Down
33 changes: 33 additions & 0 deletions test/unit/sandbox-libraries/pm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
});
7 changes: 6 additions & 1 deletion types/sandbox/prerequest.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -112,6 +112,11 @@ declare interface Execution {
* @excludeFromTestScript
*/
skipRequest(): void;
/**
* Path of the current script being executed.
* @property current - Returns the current item name whose script is being executed
*/
location: string[];
}

/**
Expand Down
7 changes: 6 additions & 1 deletion types/sandbox/test.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -113,6 +113,11 @@ declare interface Visualizer {
}

declare interface Execution {
/**
* Path of the current script being executed.
* @property current - Returns the current item name whose script is being executed
*/
location: string[];
}

/**
Expand Down

0 comments on commit be79bf8

Please sign in to comment.