From 0a3e80ea3d1c5e1f3ff683c26fc671673de09975 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Fri, 8 Mar 2024 13:23:05 -0800 Subject: [PATCH] fix: respect executableDir even non-adb environment such as desktop (#383) * chore: remove unnecessary adb check as no usage * add test temporary * fix test * add a flag * add comment * fix lint --- lib/chromedriver.js | 15 +++++++++++++-- test/unit/chromedriver-specs.js | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/chromedriver.js b/lib/chromedriver.js index c70ea55..bbc255c 100644 --- a/lib/chromedriver.js +++ b/lib/chromedriver.js @@ -44,7 +44,7 @@ export class Chromedriver extends events.EventEmitter { port = DEFAULT_PORT, useSystemExecutable = false, executable, - executableDir = getChromedriverDir(), + executableDir, bundleId, mappingPath, cmdArgs, @@ -74,6 +74,15 @@ export class Chromedriver extends events.EventEmitter { port: this.proxyPort, log: this._log, }); + + if (this.executableDir) { + // Expects the user set the executable directory explicitly + this.isCustomExecutableDir = true; + } else { + this.isCustomExecutableDir = false; + this.executableDir = getChromedriverDir(); + } + this.verbose = verbose; this.logPath = logPath; this.disableBuildCheck = !!disableBuildCheck; @@ -323,10 +332,12 @@ export class Chromedriver extends events.EventEmitter { } /** + * When executableDir is given explicitly for non-adb environment, + * this method will respect the executableDir rather than the system installed binary. * @returns {Promise} */ async getCompatibleChromedriver() { - if (!this.adb) { + if (!this.adb && !this.isCustomExecutableDir) { return await getChromedriverBinaryPath(); } diff --git a/test/unit/chromedriver-specs.js b/test/unit/chromedriver-specs.js index a3270f4..8742409 100644 --- a/test/unit/chromedriver-specs.js +++ b/test/unit/chromedriver-specs.js @@ -30,6 +30,21 @@ describe('chromedriver', function () { const binPath = await cd.getCompatibleChromedriver(); binPath.should.eql('/path/to/chromedriver'); }); + + it('should search specified directory if provided', async function () { + const cd = new Chromedriver({ + executableDir: '/some/local/dir/for/chromedrivers', + }); + + sandbox.stub(utils, 'getChromeVersion').returns('63.0.3239.99'); + sandbox.stub(fs, 'glob').returns(['/some/local/dir/for/chromedrivers/chromedriver']); + sandbox.stub(tp, 'exec').returns({ + stdout: 'ChromeDriver 2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8)', + }); + + const binPath = await cd.getCompatibleChromedriver(); + binPath.should.eql('/some/local/dir/for/chromedrivers/chromedriver'); + }); }); describe('Android', function () {