Skip to content

Commit

Permalink
feat: add support to skip request execution from script.
Browse files Browse the repository at this point in the history
  • Loading branch information
vedkribhu committed Oct 30, 2023
1 parent b98d1e3 commit d49315f
Show file tree
Hide file tree
Showing 3 changed files with 338 additions and 6 deletions.
53 changes: 47 additions & 6 deletions lib/runner/extensions/event.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _ = require('lodash'),
EXECUTION_ASSERTION_EVENT_BASE = 'execution.assertion.',
EXECUTION_ERROR_EVENT_BASE = 'execution.error.',
EXECUTION_COOKIES_EVENT_BASE = 'execution.cookies.',
EXECUTION_SKIP_EVENT_BASE = 'execution.skip.',

COOKIES_EVENT_STORE_ACTION = 'store',
COOKIE_STORE_PUT_METHOD = 'putCookie',
Expand Down Expand Up @@ -125,6 +126,11 @@ getCookieDomain = function (fnName, args) {
return domain;
};

const skippedExecutions = new Set(),
isExecutionSkipped = (executionId) => {
return skippedExecutions.has(executionId);
};

/**
* Script execution extension of the runner.
* This module exposes processors for executing scripts before and after requests. Essentially, the processors are
Expand Down Expand Up @@ -162,18 +168,34 @@ module.exports = {
run.host = context;

context.on('console', function () {
const cursor = arguments[0];

if (cursor && cursor.execution && isExecutionSkipped(cursor.execution)) { return; }

run.triggers.console(...arguments);
});

context.on('error', function () {
const cursor = arguments[0];

if (cursor && cursor.execution && isExecutionSkipped(cursor.execution)) { return; }

run.triggers.error(...arguments);
});

context.on('execution.error', function () {
const cursor = arguments[0];

if (cursor && cursor.execution && isExecutionSkipped(cursor.execution)) { return; }

run.triggers.exception(...arguments);
});

context.on('execution.assertion', function () {
const cursor = arguments[0];

if (cursor && cursor.execution && isExecutionSkipped(cursor.execution)) { return; }

run.triggers.assertion(...arguments);
});

Expand Down Expand Up @@ -268,7 +290,16 @@ module.exports = {

// create copy of cursor so we don't leak script ids outside `event.command`
// and across scripts
scriptCursor = _.clone(cursor);
scriptCursor = _.clone(cursor),

disposeListeners = function () {
this.host.removeAllListeners(EXECUTION_REQUEST_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_ASSERTION_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_RESPONSE_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_COOKIES_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_ERROR_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_SKIP_EVENT_BASE + executionId);
}.bind(this);

// store the execution id in script
script._lastExecutionId = executionId; // please don't use it anywhere else!
Expand Down Expand Up @@ -425,6 +456,18 @@ module.exports = {
}.bind(this));
}.bind(this));

this.host.on(EXECUTION_SKIP_EVENT_BASE + executionId, function () {
// execution can not be skipped for test event
if (eventName === 'test') {
return;
}

// we dispose all listeners to avoid any more events being triggered
// and mark the execution as skipped
disposeListeners();
skippedExecutions.add(executionId);
});

// finally execute the script
this.host.execute(event, {
id: executionId,
Expand All @@ -439,11 +482,7 @@ module.exports = {
_itemName: item.name
}
}, function (err, result) {
this.host.removeAllListeners(EXECUTION_REQUEST_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_ASSERTION_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_RESPONSE_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_COOKIES_EVENT_BASE + executionId);
this.host.removeAllListeners(EXECUTION_ERROR_EVENT_BASE + executionId);
disposeListeners();

// Handle async errors as well.
// If there was an error running the script itself, that takes precedence
Expand Down Expand Up @@ -513,6 +552,8 @@ module.exports = {
// upcoming commands(request, httprequest).
result && result.request && (payload.context.request = result.request);

result && (result.shouldSkipExecution = skippedExecutions.has(executionId));

// now that this script is done executing, we trigger the event and move to the next script
this.triggers.script(err || null, scriptCursor, result, script, event, item);

Expand Down
22 changes: 22 additions & 0 deletions lib/runner/extensions/item.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ var _ = require('lodash'),
extractVisualizerData,
getResponseJSON;

function shouldSkipExecution (executions) {
if (!executions) {
return false;
}

if (!Array.isArray(executions)) {
executions = [executions];
}

return executions.some((execution) => {
return execution && execution.result && execution.result.shouldSkipExecution;
});
}

/**
* Returns visualizer data from the latest execution result.
*
Expand Down Expand Up @@ -163,6 +177,14 @@ module.exports = {
});
}

if (shouldSkipExecution(prereqExecutions)) {
this.triggers.item(prereqExecutionError, coords, item);

return callback && callback.call(this, null, {
prerequest: prereqExecutions
});
}

// update allowed request mutation properties with the mutated context
// @note from this point forward, make sure this mutated
// request instance is used for upcoming commands.
Expand Down
Loading

0 comments on commit d49315f

Please sign in to comment.