@wdio/sync
Advanced tools
Comparing version 5.11.0 to 5.11.7
@@ -25,35 +25,24 @@ "use strict"; | ||
function wrapCommand(commandName, fn) { | ||
/** | ||
* helper method that runs the command with before/afterCommand hook | ||
*/ | ||
const runCommand = async function (...args) { | ||
// save error for getting full stack in case of failure | ||
// should be before any async calls | ||
const stackError = new Error(); | ||
await (0, _executeHooksWithArgs.default)(this.options.beforeCommand, [commandName, args]); | ||
let commandResult; | ||
let commandError; | ||
try { | ||
commandResult = await fn.apply(this, args); | ||
} catch (err) { | ||
commandError = (0, _utils.sanitizeErrorMessage)(err, stackError); | ||
return function wrapCommandFn(...args) { | ||
/** | ||
* Avoid running some functions in Future that are not in Fiber. | ||
*/ | ||
if (this._NOT_FIBER === true) { | ||
this._NOT_FIBER = isNotInFiber(this, fn.name); | ||
return runCommand.apply(this, [fn, ...args]); | ||
} | ||
/** | ||
* all named nested functions run in parent Fiber context | ||
*/ | ||
await (0, _executeHooksWithArgs.default)(this.options.afterCommand, [commandName, args, commandResult, commandError]); | ||
if (commandError) { | ||
throw commandError; | ||
} | ||
return commandResult; | ||
}; | ||
return function (...args) { | ||
this._NOT_FIBER = fn.name !== ''; | ||
const future = new _future.default(); | ||
const result = runCommand.apply(this, args); | ||
const result = runCommandWithHooks.apply(this, [commandName, fn, ...args]); | ||
result.then(future.return.bind(future), future.throw.bind(future)); | ||
try { | ||
return future.wait(); | ||
const futureResult = future.wait(); | ||
this._NOT_FIBER = false; | ||
return futureResult; | ||
} catch (e) { | ||
@@ -79,2 +68,49 @@ /** | ||
}; | ||
} | ||
/** | ||
* helper method that runs the command with before/afterCommand hook | ||
*/ | ||
async function runCommandWithHooks(commandName, fn, ...args) { | ||
await (0, _executeHooksWithArgs.default)(this.options.beforeCommand, [commandName, args]); | ||
let commandResult; | ||
let commandError; | ||
try { | ||
commandResult = await runCommand.apply(this, [fn, ...args]); | ||
} catch (err) { | ||
commandError = err; | ||
} | ||
await (0, _executeHooksWithArgs.default)(this.options.afterCommand, [commandName, args, commandResult, commandError]); | ||
if (commandError) { | ||
throw commandError; | ||
} | ||
return commandResult; | ||
} | ||
async function runCommand(fn, ...args) { | ||
// save error for getting full stack in case of failure | ||
// should be before any async calls | ||
const stackError = new Error(); | ||
try { | ||
return await fn.apply(this, args); | ||
} catch (err) { | ||
throw (0, _utils.sanitizeErrorMessage)(err, stackError); | ||
} | ||
} | ||
/** | ||
* isNotInFiber | ||
* if element or its parent has element id then we are in parent's Fiber | ||
* @param {object} context browser or element | ||
* @param {string} fnName function name | ||
*/ | ||
function isNotInFiber(context, fnName) { | ||
return fnName !== '' && !!(context.elementId || context.parent && context.parent.elementId); | ||
} |
{ | ||
"name": "@wdio/sync", | ||
"version": "5.11.0", | ||
"version": "5.11.7", | ||
"description": "A WebdriverIO plugin. Helper module to run WebdriverIO commands synchronously", | ||
@@ -44,3 +44,3 @@ "author": "Christian Bromann <christian@saucelabs.com>", | ||
}, | ||
"gitHead": "f235de6d9876a1a81bc1007430346d493ba7f579" | ||
"gitHead": "945feb83efec8431cb448293bb2cc0f3b7fc1c16" | ||
} |
@@ -92,3 +92,3 @@ /// <reference types="node"/> | ||
config: Config, | ||
capabilities: WebDriver.DesiredCapabilities | ||
capabilities: WebDriver.DesiredCapabilities[] | ||
): void; | ||
@@ -169,2 +169,4 @@ | ||
interface Element { | ||
selector: string; | ||
elementId: string; | ||
addCommand( | ||
@@ -171,0 +173,0 @@ name: string, |
30626
847