Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Avoid chromedriver dependency when using chromedriverCustomPath #64

Merged
merged 2 commits into from
Jan 6, 2022
Merged
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
58 changes: 22 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"dependencies": {
"@wdio/logger": "^7.5.3",
"fs-extra": "^9.1.0",
"split2": "^3.2.2"
"split2": "^3.2.2",
"tcp-port-used": "^1.0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch 👍

Copy link
Contributor Author

@codigodiabolico codigodiabolico Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already needed by launcher.js (https://github.com/webdriverio-community/wdio-chromedriver-service/blob/main/src/launcher.js#L7), but is not declared inside package.json, making it unavailable if "chromedriver" is not installed (dependency of this library too)

edit: didn't see your comment

},
"devDependencies": {
"@babel/cli": "^7.12.17",
Expand Down
12 changes: 10 additions & 2 deletions src/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { spawn } from 'child_process'
import fs from 'fs-extra'
import path from 'path'
import split2 from 'split2'
import { path as chromedriverPath } from 'chromedriver'
import logger from '@wdio/logger'
import tcpPortUsed from 'tcp-port-used'

Expand Down Expand Up @@ -36,7 +35,7 @@ export default class ChromeDriverLauncher {
this.logFileName = options.logFileName || DEFAULT_LOG_FILENAME
this.capabilities = capabilities
this.args = options.args || []
this.chromedriverCustomPath = options.chromedriverCustomPath ? path.resolve(options.chromedriverCustomPath) : chromedriverPath
this.chromedriverCustomPath = options.chromedriverCustomPath ? path.resolve(options.chromedriverCustomPath) : this._getChromedriverPath()
}

async onPrepare() {
Expand Down Expand Up @@ -112,4 +111,13 @@ export default class ChromeDriverLauncher {
}
}
}

_getChromedriverPath() {
try {
return require('chromedriver').path
} catch (e) {
log.error('Can\'t load chromedriver, please define "chromedriverCustomPath" property or install dependency via "npm install chromedriver --save-dev"')
throw e
}
}
}
24 changes: 15 additions & 9 deletions tests/launcher.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import fs from 'fs-extra'
import { spawn } from 'child_process'
import {spawn} from 'child_process'
import ChromeDriverLauncher from '../src/launcher'

jest.mock('child_process', () => {
Expand Down Expand Up @@ -359,33 +359,39 @@ describe('ChromeDriverLauncher launcher', () => {
test('should select custom chromedriver path "chromedriver.exe"', async () => {
options.chromedriverCustomPath = 'chromedriver.exe'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()
await Launcher.onPrepare()
Launcher._redirectLogStream = jest.fn()
await Launcher.onPrepare()
expect(Launcher.chromedriverCustomPath).toEqual(path.resolve(options.chromedriverCustomPath))
})

test('should select custom chromedriver path "c:\\chromedriver.exe"', async () => {
options.chromedriverCustomPath = 'c:\\chromedriver.exe'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()
await Launcher.onPrepare()
Launcher._redirectLogStream = jest.fn()
await Launcher.onPrepare()
expect(Launcher.chromedriverCustomPath).toEqual(path.resolve(options.chromedriverCustomPath))
})

test('should select custom chromedriver path "./chromedriver.exe"', async () => {
options.chromedriverCustomPath = './chromedriver.exe'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()
await Launcher.onPrepare()
Launcher._redirectLogStream = jest.fn()
await Launcher.onPrepare()
expect(Launcher.chromedriverCustomPath).toEqual(path.resolve(options.chromedriverCustomPath))
})

test('should select default chromedriver path if no custome path provided"', async () => {
options.chromedriverCustomPath = undefined
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()
await Launcher.onPrepare()
Launcher._redirectLogStream = jest.fn()
await Launcher.onPrepare()
expect(Launcher.chromedriverCustomPath).not.toBeUndefined
})

test('should throw if chromedriver not installed and no custom path provided"', async () => {
jest.mock('chromedriver', () => undefined)
options.chromedriverCustomPath = undefined
expect(() => new ChromeDriverLauncher(options, capabilities, config)).toThrow()
})
})
})