Skip to content

Solution for working without wdio/sync & fibers #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 106 additions & 156 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,18 @@
"@oxygenhq/chrome-remote-interface": "0.30.0",
"@oxygenhq/logger": "0.5.1",
"@oxygenhq/mitmproxy-node": "1.0.1",
"@wdio/cli": "7.9.0",
"@wdio/devtools-service": "7.9.0",
"@wdio/sync": "7.9.0",
"@wdio/cli": "7.11.1",
"@wdio/devtools-service": "7.11.1",
"async": "3.2.0",
"chai": "4.3.4",
"chrome-har": "0.11.12",
"config": "3.3.6",
"csv-parse": "4.16.0",
"cucumber": "6.0.5",
"deasync": "0.1.21",
"decache": "4.6.0",
"easyxml": "2.0.1",
"ejs": "3.0.2",
"esm": "3.2.25",
"fibers": "5.0.0",
"glob": "7.1.7",
"glob-to-regexp": "0.4.1",
"got": "11.8.2",
Expand All @@ -92,7 +89,7 @@
"testingbot-api": "1.0.7",
"text-to-image": "2.4.4",
"twilio": "3.66.1",
"webdriverio": "7.9.0",
"webdriverio": "7.11.1",
"when": "3.7.8",
"xlsx": "0.17.0"
},
Expand Down
130 changes: 15 additions & 115 deletions src/core/OxygenCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import glob from 'glob';
import path from 'path';
import fs from 'fs';
import deasync from 'deasync';
import Future from 'fibers/future';
import Fiber from 'fibers';
import { EOL } from 'os';
import StepResult from '../model/step-result';
import OxygenEvents from './OxygenEvents';
Expand Down Expand Up @@ -505,9 +502,9 @@ export default class Oxygen extends OxygenEvents {
if (methodName === 'getCapabilities') {
wrapper['_getCapabilities'] = method.bind(module);
}
wrapper[methodName] = (...args) => {
wrapper[methodName] = async (...args) => {
try {
return _this._commandWrapper(methodName, args, module, name);
return await _this._commandWrapper(methodName, args, module, name);
}
catch (e) {
if (e instanceof OxError) {
Expand All @@ -522,7 +519,7 @@ export default class Oxygen extends OxygenEvents {
return wrapper;
}

_commandWrapper(cmdName, cmdArgs, module, moduleName) {
async _commandWrapper(cmdName, cmdArgs, module, moduleName) {
if (!module || !module[cmdName]) {
return undefined;
}
Expand All @@ -539,7 +536,7 @@ export default class Oxygen extends OxygenEvents {
cmdName !== 'init' &&
cmdName !== 'transaction'
) {
deasync.sleep(this.opts.delay * 1000);
await oxutil.sleep(this.opts.delay * 1000);
}

// throw if a command executed on unitialized module (except internal methods and a few other)
Expand Down Expand Up @@ -569,30 +566,13 @@ export default class Oxygen extends OxygenEvents {
try {
// emit before events
if (cmdName === 'dispose') {
this._wrapAsync(this._callServicesOnModuleWillDispose).apply(this, [module]);
await this._wrapAsync(this._callServicesOnModuleWillDispose).apply(this, [module]);
}

const retvalPromise = this._wrapAsync(module[cmdName]).apply(module, decryptedArgs);

if (retvalPromise && retvalPromise.then) {
let promiseDone = false;

retvalPromise.then((value) => {

retval = value;
promiseDone = true;
}, (e) => {
error = e;
promiseDone = true;
});

deasync.loopWhile(() => !promiseDone);
} else {
retval = retvalPromise;
}
retval = await this._wrapAsync(module[cmdName]).apply(module, decryptedArgs);

if (cmdName === 'init') {
this._wrapAsync(this._callServicesOnModuleInitialized).apply(this, [module]);
await this._wrapAsync(this._callServicesOnModuleInitialized).apply(this, [module]);
}

} catch (e) {
Expand All @@ -615,13 +595,12 @@ export default class Oxygen extends OxygenEvents {
const endTime = oxutil.getTimeStamp();

let stepResult;
let done = false;

if (publicMethod) {
const waitId = +new Date();
this._waitStepResultList.push(waitId);

stepResult = this._getStepResult(module, moduleName, cmdName, cmdArgs, cmdLocation, startTime, endTime, retval, error);
stepResult = await this._getStepResult(module, moduleName, cmdName, cmdArgs, cmdLocation, startTime, endTime, retval, error);

const index = this._waitStepResultList.indexOf(waitId);
this._waitStepResultList.splice(index, 1);
Expand All @@ -630,7 +609,6 @@ export default class Oxygen extends OxygenEvents {

this.resultStore.steps.push(stepResult);
this.emitAfterCommand(cmdName, moduleName, cmdFn, cmdArgs, this.ctx, cmdLocation, endTime, stepResult);
done = true;
}

if (error && error.isFatal && !this.opts.continueOnError) {
Expand All @@ -645,87 +623,13 @@ export default class Oxygen extends OxygenEvents {
throw error;
}

if (!publicMethod) {
done = true;
}

deasync.loopWhile(() => !done && !error);

return retval;
}

_wrapAsync (fn, context) {
return function (...args) {
return async function (...args) {
var self = context || this;
// if the current code is not running inside the Fiber context, then run async code as sync using deasync module
if (!Fiber.current) {
const retval = fn.apply(self, args);

let done = false;
let error = null;
let finalVal = null;

if (retval && retval.then) {
Promise.resolve(retval)
.then((val) => {
finalVal = val;
done = true;
})
.catch((e) => {
error = e;
done = true;
});
} else {
finalVal = retval;
done = true;
}

try {
deasync.loopWhile(() => !done && !error);
}
catch (e) {

if (e && e.message && typeof e.message === 'string' && e.message.includes('readyState')) {
return undefined;
}

// ignore this error as it usually happens
// when Oxygen is disposed and process is being killed
this.logger.error('deasync.loopWhile() failed:', e);
return undefined;
}

if (!error) {
return finalVal;
}
throw error;
}

let error = null;
let done = false;
let retval = null;

try {

// otherwise, if we are inside the Fiber context, then use Fiber's Future
const future = new Future();
var result = fn.apply(self, args);
if (result && typeof result.then === 'function') {
result.then((val) => future.return(val), (err) => future.throw(err));
return future.wait();
}
return result;

} catch (e) {
error = e;
}

deasync.loopWhile(() => !done && !error);

if (!error) {
return retval;
}
throw error;
return await fn.apply(self, args);
};
}

Expand All @@ -747,7 +651,7 @@ export default class Oxygen extends OxygenEvents {
return null;
}

_getStepResult(module, moduleName, methodName, args, location, startTime, endTime, retval, err) {
async _getStepResult(module, moduleName, methodName, args, location, startTime, endTime, retval, err) {
var step = new StepResult();

step.name = oxutil.getMethodSignature(moduleName, methodName, args);
Expand Down Expand Up @@ -794,14 +698,14 @@ export default class Oxygen extends OxygenEvents {

if (typeof module._takeScreenshotSilent === 'function' && !this.opts.disableScreenshot) {
try {
step.screenshot = module._takeScreenshotSilent(methodName);
step.screenshot = await module._takeScreenshotSilent(methodName);
}
catch (e) {
// If we are here, we were unable to get a screenshot
// Try to wait for a moment (in Perfecto Cloud, the screenshot might not be immidiately available)
deasync.sleep(1000);
await oxutil.sleep(1000);
try {
step.screenshot = module._takeScreenshotSilent(methodName);
step.screenshot = await module._takeScreenshotSilent(methodName);
}
catch (e) {
// FIXME: indicate to user that an attempt to take a screenshot has failed
Expand Down Expand Up @@ -841,11 +745,7 @@ export default class Oxygen extends OxygenEvents {

if (mod.dispose) {
try {
const disposeResult = mod.dispose(status);
if (disposeResult && typeof disposeResult.then === 'function') {
// probably a promise
await disposeResult();
}
await mod.dispose(status);
}
catch (e) {
// ignore module disposal error
Expand Down
4 changes: 2 additions & 2 deletions src/ox_modules/module-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function() {
return true;
};

module._takeScreenshotSilent = function(name) {
module._takeScreenshotSilent = async function(name) {
var mod;
if (ox && ox.modules && ox.modules.mob && ox.modules.mob.getDriver && ox.modules.mob.getDriver()) {
mod = ox.modules.mob;
Expand All @@ -34,7 +34,7 @@ module.exports = function() {
}

if (mod && mod._takeScreenshotSilent) {
return mod._takeScreenshotSilent();
return await mod._takeScreenshotSilent();
} else {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ox_modules/module-mob.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export default class MobileModule extends WebDriverModule {
return ACTION_COMMANDS.includes(name);
}

_takeScreenshotSilent(name) {
async _takeScreenshotSilent(name) {
if (!NO_SCREENSHOT_COMMANDS.includes(name)) {
let error;
try {
Expand All @@ -364,7 +364,7 @@ export default class MobileModule extends WebDriverModule {
this.driver.takeScreenshot
) {
let retval;
this.driver.call(() => {
await this.driver.call(() => {
return new Promise((resolve, reject) => {
const waitUntilRetVal = this.driver.waitUntil(async() => {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/ox_modules/module-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export default class WebModule extends WebDriverModule {
return ACTION_COMMANDS.includes(name);
}

_takeScreenshotSilent(name) {
async _takeScreenshotSilent(name) {
if (!NO_SCREENSHOT_COMMANDS.includes(name)) {
let error;
try {
Expand All @@ -490,7 +490,7 @@ export default class WebModule extends WebDriverModule {
this.driver.takeScreenshot
) {
let retval;
this.driver.call(() => {
await this.driver.call(() => {
return new Promise((resolve, reject) => {
const waitUntilRetVal = this.driver.waitUntil(async() => {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/ox_modules/module-win.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@ export default class WindowsModule extends WebDriverModule {
return ACTION_COMMANDS.includes(name);
}

_takeScreenshotSilent(name) {
async _takeScreenshotSilent(name) {
if (!NO_SCREENSHOT_COMMANDS.includes(name)) {
try {
if (
this.driver &&
this.driver.takeScreenshot
) {
return this.driver.takeScreenshot();
return await this.driver.takeScreenshot();
}
} catch (e) {
this.logger.error('Cannot get screenshot', e);
Expand Down
Loading