Skip to content

Commit

Permalink
Added support for pm.execution.location (#950)
Browse files Browse the repository at this point in the history
* Add support to get path of current request in sandbox script

* Refactor `pm.execution.location`

---------

Co-authored-by: Appurva Murawat <[email protected]>
  • Loading branch information
vedkribhu and appurva21 authored Nov 18, 2023
1 parent 61e893f commit af5c60e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 8 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
23 changes: 22 additions & 1 deletion lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,28 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
* @excludeFromTestScript
* @instance
*/
skipRequest: onSkipRequest
skipRequest: onSkipRequest,

/**
* @interface ExecutionLocation
* @extends Array<string>
*/

/**
* The path of the current request.
*
* @type {ExecutionLocation} - current execution path
* @instance
*/
location: _assignDefinedReadonly(execution.legacy._itemPath || [], /** @lends ExecutionLocation */ {
/**
* The item name whose script is currently being executed.
*
* @instance
* @type {string}
*/
current: execution.legacy._eventItemName
})
}
}, options.disabledAPIs);

Expand Down
34 changes: 34 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,39 @@ 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',
_itemPath: ['C1', 'R1'],
_eventItemName: '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',
_itemPath: ['C1', 'R1'],
_eventItemName: 'R1'
}
}, done);
});
});
});
});
});
28 changes: 23 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// Type definitions for postman-sandbox 4.3.0
// Project: https://github.com/postmanlabs/postman-sandbox
// Definitions by: PostmanLabs
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
/// <reference types="node" />

declare const CONSOLE_EVENT = "execution.console";

/**
* List of functions that we expect and create for console
*/
declare const logLevels: String[];
declare const logLevels: string[];

/**
* Replacer to be used with teleport-javascript to handle cases which are not
Expand Down Expand Up @@ -90,7 +97,7 @@ declare type Return = {
*/
declare class Postman {
constructor(execution: Execution, onRequest: (...params: any[]) => any, onSkipRequest: (...params: any[]) => any, onAssertion: (...params: any[]) => any, cookieStore: any, options?: {
disabledAPIs?: String[];
disabledAPIs?: string[];
});
/**
* The pm.info object contains information pertaining to the script being executed.
Expand Down Expand Up @@ -183,6 +190,17 @@ declare interface Execution {
* Stops the current request and its scripts from executing.
*/
skipRequest(): void;
/**
* The path of the current request.
*/
location: ExecutionLocation;
}

declare interface ExecutionLocation extends Array {
/**
* The item name whose script is currently being executed.
*/
current: string;
}

/**
Expand Down Expand Up @@ -228,19 +246,19 @@ declare var SandboxGlobals: any;
* The set of timer function names. We use this array to define common behaviour of all setters and clearer timer
* functions
*/
declare const timerFunctionNames: String[];
declare const timerFunctionNames: string[];

/**
* This object defines a set of timer function names that are trigerred a number of times instead of a single time.
* Such timers, when placed in generic rules, needs special attention.
*/
declare const multiFireTimerFunctions: Boolean[];
declare const multiFireTimerFunctions: boolean[];

/**
* This object defines a set of function timer names that do not fire based on any pre-set duration or interval.
* Such timers, when placed in generic rules, needs special attention.
*/
declare const staticTimerFunctions: Boolean[];
declare const staticTimerFunctions: boolean[];

/**
* A local copy of Slice function of Array
Expand Down
13 changes: 12 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,17 @@ declare interface Execution {
* @excludeFromTestScript
*/
skipRequest(): void;
/**
* The path of the current request.
*/
location: ExecutionLocation;
}

declare interface ExecutionLocation extends Array {
/**
* The item name whose script is currently being executed.
*/
current: string;
}

/**
Expand Down
13 changes: 12 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,17 @@ declare interface Visualizer {
}

declare interface Execution {
/**
* The path of the current request.
*/
location: ExecutionLocation;
}

declare interface ExecutionLocation extends Array {
/**
* The item name whose script is currently being executed.
*/
current: string;
}

/**
Expand Down

0 comments on commit af5c60e

Please sign in to comment.