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 10, 2023
1 parent 61e893f commit 533abb7
Show file tree
Hide file tree
Showing 5 changed files with 98 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.current and pm.execution.location.path

4.3.0:
date: 2023-11-02
new features:
Expand Down
21 changes: 20 additions & 1 deletion lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,26 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
* @excludeFromTestScript
* @instance
*/
skipRequest: onSkipRequest
skipRequest: onSkipRequest,

/**
* @typedef {Object} Item
* @property {String} id - id of the item
* @property {String} name - name of the item
*/

/**
* Exposes properties related to location of current execution
*
* @instance
* @type {Object}
* @property {Item} current - the current item being executed.
* @property {Array.<Item>} path - complete path of the current request being executed.
*/
location: {
path: execution.legacy.requestPath,
current: execution.legacy.currentItem
}
}
}, options.disabledAPIs);

Expand Down
36 changes: 36 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,41 @@ describe('sandbox library - pm api', function () {
});
});
});
describe('.location', function () {
describe('.path', function () {
it('should return the correct path of the request', function (done) {
context.execute({
script: `
var assert = require('assert');
assert.deepEqual(pm.execution.location.path,
[{ id: 'R1-id', name: 'R1' }, { id: 'C1-id', name: 'C1' }]);
` }, {
legacy: {
_itemName: 'request-name',
_itemId: 'request-id',
requestPath: [{ id: 'R1-id', name: 'R1' }, { id: 'C1-id', name: 'C1' }],
currentItem: { id: 'R1-id', name: '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, { id: 'R1-id', name: 'R1' });
` }, {
legacy: {
_itemName: 'request-name',
_itemId: 'request-id',
requestPath: [{ id: 'R1-id', name: 'R1' }, { id: 'C1-id', name: 'C1' }],
currentItem: { id: 'R1-id', name: 'R1' }
}
}, done);
});
});
});
});
});
20 changes: 19 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,8 +112,26 @@ declare interface Execution {
* @excludeFromTestScript
*/
skipRequest(): void;
/**
* Exposes properties related to location of current execution
* @property current - the current item being executed.
* @property path - complete path of the current request being executed.
*/
location: {
current: Item;
path: Item[];
};
}

/**
* @property id - id of the item
* @property name - name of the item
*/
declare type Item = {
id: string;
name: string;
};

/**
* The pm object encloses all information pertaining to the script being executed and
* allows one to access a copy of the request being sent or the response received.
Expand Down
20 changes: 19 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,8 +113,26 @@ declare interface Visualizer {
}

declare interface Execution {
/**
* Exposes properties related to location of current execution
* @property current - the current item being executed.
* @property path - complete path of the current request being executed.
*/
location: {
current: Item;
path: Item[];
};
}

/**
* @property id - id of the item
* @property name - name of the item
*/
declare type Item = {
id: string;
name: string;
};

/**
* The pm object encloses all information pertaining to the script being executed and
* allows one to access a copy of the request being sent or the response received.
Expand Down

0 comments on commit 533abb7

Please sign in to comment.