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

Commit

Permalink
chromedriver: make v6 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinheitzman committed Apr 1, 2020
1 parent dbe0071 commit 4696bf5
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 53 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wdio-chromedriver-service",
"version": "5.0.2",
"version": "6.0.0",
"description": "WebdriverIO service to start & stop ChromeDriver",
"author": "Mattias Ekstrand <mattias.ekstrand@gmail.com>",
"homepage": "https://github.com/atti187/wdio-chromedriver-service#readme",
Expand Down Expand Up @@ -33,7 +33,7 @@
},
"peerDependencies": {
"chromedriver": "*",
"@wdio/cli": "^5.0.0"
"@wdio/cli": "^6.0.0"
},
"dependencies": {
"fs-extra": "^0.30.0"
Expand Down Expand Up @@ -66,7 +66,8 @@
"wdio-mocha-framework": "^0.2.12"
},
"contributors": [
"Mattias Ekstrand <mattias.ekstrand@gmail.com>"
"Mattias Ekstrand <mattias.ekstrand@gmail.com>",
"Erwin Heitzman <erwin.heitzman@gmail.com>"
],
"jest": {
"testMatch": [
Expand Down
69 changes: 49 additions & 20 deletions src/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,55 @@ import ChromeDriver from 'chromedriver'

import getFilePath from './utils/getFilePath'

const DEFAULT_LOG_FILENAME = 'ChromeDriver.txt'
const DEFAULT_LOG_FILENAME = 'chromedriver.log'

const DEFAULT_CONNECTION = {
protocol: 'http',
hostname: 'localhost',
port: 9515,
path: '/'
}

const isMultiremote = obj => typeof obj === 'object' && !Array.isArray(obj)
const isChrome = cap => cap.browserName.toLowerCase() === 'chrome'

export default class ChromeDriverLauncher {
constructor () {
this.chromeDriverLogs = null
this.chromeDriverArgs = null
this.logToStdout = false
constructor(options, capabilities, config) {
this.options = {
protocol: options.protocol || DEFAULT_CONNECTION.protocol,
hostname: options.hostname || DEFAULT_CONNECTION.hostname,
port: options.port || DEFAULT_CONNECTION.port,
path: options.path || DEFAULT_CONNECTION.path,
}

return this
this.outputDir = options.outputDir || config.outputDir
this.capabilities = capabilities
this.chromeDriverArgs = []
}

async onPrepare (config) {
this.chromeDriverArgs = config.chromeDriverArgs || []
this.chromeDriverLogs = config.chromeDriverLogs
async onPrepare() {
this.chromeDriverArgs.push(`--port=${this.options.port}`)
this.chromeDriverArgs.push(`--url-base=${this.options.path}`)

if (!this.chromeDriverArgs.find(arg => arg.startsWith('--port'))) {
this.chromeDriverArgs.push(`--port=${config.port}`)
}
/**
* update capability connection options to connect
* to chromedriver
*/
this._mapCapabilities()

if (!this.chromeDriverArgs.find(arg => arg.startsWith('--url-base'))) {
this.chromeDriverArgs.push(`--url-base=${config.path}`)
}

this.process = await ChromeDriver.start(this.chromeDriverArgs, true)

if (typeof this.chromeDriverLogs === 'string') {
if (typeof this.outputDir === 'string') {
this._redirectLogStream()
}
}

onComplete () {
onComplete() {
ChromeDriver.stop()
}

_redirectLogStream () {
const logFile = getFilePath(this.chromeDriverLogs, DEFAULT_LOG_FILENAME)
_redirectLogStream() {
const logFile = getFilePath(this.outputDir, DEFAULT_LOG_FILENAME)

// ensure file & directory exists
fs.ensureFileSync(logFile)
Expand All @@ -47,4 +60,20 @@ export default class ChromeDriverLauncher {
this.process.stdout.pipe(logStream)
this.process.stderr.pipe(logStream)
}

_mapCapabilities() {
if (isMultiremote(this.capabilities)) {
for (const cap in this.capabilities) {
if (isChrome(this.capabilities[cap])) {
Object.assign(this.capabilities[cap], this.options)
}
}
} else {
for (const cap of this.capabilities) {
if (isChrome(cap)) {
Object.assign(cap, this.options)
}
}
}
}
}

0 comments on commit 4696bf5

Please sign in to comment.