Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
beforeEach(function () {
sandbox.stub(PortFinder, 'getPort').callsFake(function (cb) { // eslint-disable-line promise/prefer-await-to-callbacks
return cb(null, 4444); // eslint-disable-line promise/prefer-await-to-callbacks
});
driver = new AndroidDriver();
driver.adb = sandbox.stub();
driver.adb.curDeviceId = 'device_id';
driver.adb.getAdbServerPort = sandbox.stub().returns(5555);
sandbox.stub(Chromedriver.prototype, 'restart');
sandbox.stub(Chromedriver.prototype, 'start');
sandbox.stub(Chromedriver.prototype.proxyReq, 'bind').returns('proxy');
stubbedChromedriver = sinon.stub();
stubbedChromedriver.proxyReq = sinon.stub();
stubbedChromedriver.proxyReq.bind = sinon.stub();
stubbedChromedriver.restart = sinon.stub();
stubbedChromedriver.stop = sandbox.stub().throws();
stubbedChromedriver.removeAllListeners = sandbox.stub();
});
afterEach(function () {
beforeEach(function () {
sandbox.stub(PortFinder, 'getPort').callsFake(function (cb) { // eslint-disable-line promise/prefer-await-to-callbacks
return cb(null, 4444); // eslint-disable-line promise/prefer-await-to-callbacks
});
driver = new AndroidDriver();
driver.adb = sandbox.stub();
driver.adb.curDeviceId = 'device_id';
driver.adb.getAdbServerPort = sandbox.stub().returns(5555);
sandbox.stub(Chromedriver.prototype, 'restart');
sandbox.stub(Chromedriver.prototype, 'start');
sandbox.stub(Chromedriver.prototype.proxyReq, 'bind').returns('proxy');
stubbedChromedriver = sinon.stub();
stubbedChromedriver.proxyReq = sinon.stub();
stubbedChromedriver.proxyReq.bind = sinon.stub();
stubbedChromedriver.restart = sinon.stub();
stubbedChromedriver.stop = sandbox.stub().throws();
stubbedChromedriver.removeAllListeners = sandbox.stub();
});
afterEach(function () {
async.eachSeries(Object.keys(this.sessionChromedrivers), function (context, cb) {
logger.debug("Stopping chromedriver for context " + context);
// stop listening for the stopped state event
this.sessionChromedrivers[context].removeAllListeners(Chromedriver.EVENT_CHANGED);
this.sessionChromedrivers[context].stop().nodeify(function (err) {
if (err) logger.warn("Error stopping Chromedriver: " + err.message);
// chromedriver isn't valid anymore, so remove it from context list
delete this.sessionChromedrivers[context];
cb();
}.bind(this));
}.bind(this), function (err) {
// if one of these fails, go back to last proxy state and error out
ChromeAndroid.prototype.stop = function (cb) {
// stop listening for the stopped state event
this.chromedriver.removeAllListeners(Chromedriver.EVENT_CHANGED);
// now we can handle the stop on our own
this.chromedriver.stop().nodeify(function (err) {
if (err) logger.warn("Error stopping Chromedriver: " + err.message);
this.onChromedriverStop(cb);
}.bind(this));
};
const knownPackages = [
'org.chromium.chrome.shell',
'com.android.chrome',
'com.chrome.beta',
'org.chromium.chrome',
'org.chromium.webview_shell',
];
if (_.includes(knownPackages, this.opts.appPackage)) {
opts.chromeBundleId = this.opts.appPackage;
} else {
opts.chromeAndroidActivity = this.opts.appActivity;
}
this.chromedriver = await this.setupNewChromedriver(opts, this.adb.curDeviceId, this.adb);
this.chromedriver.on(Chromedriver.EVENT_CHANGED, (msg) => {
if (msg.state === Chromedriver.STATE_STOPPED) {
this.onChromedriverStop(CHROMIUM_WIN);
}
});
// Now that we have a Chrome session, we ensure that the context is
// appropriately set and that this chromedriver is added to the list
// of session chromedrivers so we can switch back and forth
this.curContext = CHROMIUM_WIN;
this.sessionChromedrivers[CHROMIUM_WIN] = this.chromedriver;
this.proxyReqRes = this.chromedriver.proxyReq.bind(this.chromedriver);
this.jwpProxyActive = true;
if (this.shouldDismissChromeWelcome()) {
// dismiss Chrome welcome dialog
await this.dismissChromeWelcome();
helpers.stopChromedriverProxies = async function stopChromedriverProxies () {
this.suspendChromedriverProxy(); // make sure we turn off the proxy flag
for (let context of _.keys(this.sessionChromedrivers)) {
let cd = this.sessionChromedrivers[context];
log.debug(`Stopping chromedriver for context ${context}`);
// stop listening for the stopped state event
cd.removeAllListeners(Chromedriver.EVENT_CHANGED);
try {
await cd.stop();
} catch (err) {
log.warn(`Error stopping Chromedriver: ${err.message}`);
}
delete this.sessionChromedrivers[context];
}
};
// For now the only known arg passed this way is androidDeviceSocket used
// by Operadriver (deriving from Chromedriver) // We don't know how other
// Chromium embedders will call this argument so for now it's name needs to
// be configurable. When Google adds the androidDeviceSocket argument to
// the original Chromedriver then we will be sure about its name for all
// Chromium embedders (as their Webdrivers will derive from Chromedriver)
if (this.args.specialChromedriverSessionArgs) {
_.each(this.args.specialChromedriverSessionArgs, function (val, option) {
logger.debug("This method is being deprecated. Apply chromeOptions " +
"normally to pass along options,see sites.google.com/a/" +
"chromium.org/chromedriver/capabilities for more info");
caps.chromeOptions[option] = val;
});
}
caps = this.decorateChromeOptions(caps);
this.chromedriver.on(Chromedriver.EVENT_CHANGED, function (msg) {
if (msg.state === Chromedriver.STATE_STOPPED) {
// bind our stop/exit handler, passing in context so we know which
// one stopped unexpectedly
this.onChromedriverStop(context);
}
}.bind(this));
this.chromedriver.start(caps).nodeify(function (err) {
if (err) return cb(err);
// save the chromedriver object under the context
this.sessionChromedrivers[context] = this.chromedriver;
cb();
}.bind(this));
};
this.chromedriver.on(Chromedriver.EVENT_CHANGED, function (msg) {
if (msg.state === Chromedriver.STATE_STOPPED) {
// bind our stop/exit handler, passing in context so we know which
// one stopped unexpectedly
this.onChromedriverStop(context);
}
}.bind(this));
this.chromedriver.start(caps).nodeify(function (err) {
helpers.setupNewChromedriver = async function setupNewChromedriver (opts, curDeviceId, adb) {
if (opts.chromeDriverPort) {
log.warn(`The 'chromeDriverPort' capability is deprecated. Please use 'chromedriverPort' instead`);
opts.chromedriverPort = opts.chromeDriverPort;
}
if (opts.chromedriverPort) {
log.debug(`Using user-specified port ${opts.chromedriverPort} for chromedriver`);
} else {
// if a single port wasn't given, we'll look for a free one
opts.chromedriverPort = await getChromedriverPort(opts.chromedriverPorts);
}
const chromedriver = new Chromedriver({
port: opts.chromedriverPort,
executable: opts.chromedriverExecutable,
adb,
cmdArgs: opts.chromedriverArgs,
verbose: !!opts.showChromedriverLog,
executableDir: opts.chromedriverExecutableDir,
mappingPath: opts.chromedriverChromeMappingFile,
bundleId: opts.chromeBundleId,
useSystemExecutable: opts.chromedriverUseSystemExecutable,
disableBuildCheck: opts.chromedriverDisableBuildCheck,
isAutodownloadEnabled: (this || {}).isChromedriverAutodownloadEnabled
? this.isChromedriverAutodownloadEnabled() : undefined,
});
// make sure there are chromeOptions
opts.chromeOptions = opts.chromeOptions || {};
it('should handle chromedriver event with STATE_STOPPED state', async function () {
await driver.startChromedriverProxy('WEBVIEW_1');
await driver.chromedriver.emit(Chromedriver.EVENT_CHANGED,
{state: Chromedriver.STATE_STOPPED});
driver.onChromedriverStop.calledWithExactly('WEBVIEW_1').should.be.true;
});
it('should ignore events if status is not STATE_STOPPED', async function () {