Skip to content

Commit

Permalink
wdio-selenium-standalone: fix watch mode (webdriverio#3970)
Browse files Browse the repository at this point in the history
  • Loading branch information
fastmx authored and christian-bromann committed May 14, 2019
1 parent 838bd15 commit 30f81e1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
23 changes: 20 additions & 3 deletions packages/wdio-selenium-standalone-service/src/launcher.js
@@ -1,11 +1,13 @@
import { promisify } from 'util'
import logger from '@wdio/logger'

import { promisify } from 'util'
import fs from 'fs-extra'
import SeleniumStandalone from 'selenium-standalone'

import getFilePath from './utils/getFilePath'

const DEFAULT_LOG_FILENAME = 'selenium-standalone.txt'
const log = logger('@wdio/selenium-standalone-service')

export default class SeleniumStandaloneLauncher {
constructor () {
Expand All @@ -21,6 +23,7 @@ export default class SeleniumStandaloneLauncher {
this.seleniumInstallArgs = config.seleniumInstallArgs || {}
this.seleniumLogs = config.seleniumLogs
this.skipSeleniumInstall = !!config.skipSeleniumInstall
this.watchMode = !!config.watch

if (!this.skipSeleniumInstall) {
await promisify(SeleniumStandalone.install)(this.seleniumInstallArgs)
Expand All @@ -31,11 +34,18 @@ export default class SeleniumStandaloneLauncher {
if (typeof this.seleniumLogs === 'string') {
this._redirectLogStream()
}

if (this.watchMode) {
process.on('SIGINT', this._stopProcess)
process.on('exit', this._stopProcess)
process.on('uncaughtException', this._stopProcess)
}
}

onComplete () {
if(this.process) {
this.process.kill()
// selenium should not be killed in watch mode
if (!this.watchMode) {
this._stopProcess()
}
}

Expand All @@ -49,4 +59,11 @@ export default class SeleniumStandaloneLauncher {
this.process.stdout.pipe(logStream)
this.process.stderr.pipe(logStream)
}

_stopProcess = () => {
if (this.process) {
log.info('shutting down all browsers')
this.process.kill()
}
}
}
36 changes: 35 additions & 1 deletion packages/wdio-selenium-standalone-service/tests/launcher.test.js
Expand Up @@ -22,7 +22,8 @@ describe('Selenium standalone launcher', () => {
const config = {
seleniumLogs : './',
seleniumArgs : { foo : 'foo' },
seleniumInstallArgs : { bar : 'bar' }
seleniumInstallArgs : { bar : 'bar' },
watch: true
}

await Launcher.onPrepare(config)
Expand All @@ -31,6 +32,7 @@ describe('Selenium standalone launcher', () => {
expect(Launcher.seleniumInstallArgs).toBe(config.seleniumInstallArgs)
expect(Launcher.seleniumArgs).toBe(config.seleniumArgs)
expect(Launcher.skipSeleniumInstall).toBe(false)
expect(Launcher.watchMode).toEqual(true)
})

test('should set correct config properties when empty', async () => {
Expand All @@ -42,6 +44,7 @@ describe('Selenium standalone launcher', () => {
expect(Launcher.seleniumInstallArgs).toEqual({})
expect(Launcher.seleniumArgs).toEqual({})
expect(Launcher.skipSeleniumInstall).toEqual(false)
expect(Launcher.watchMode).toEqual(false)
})

test('should call selenium install and start', async () => {
Expand Down Expand Up @@ -113,6 +116,23 @@ describe('Selenium standalone launcher', () => {

expect(Launcher._redirectLogStream).not.toBeCalled()
})

test('should add exit listeners to kill process in watch mode', async () => {
const processOnSpy = jest.spyOn(process, 'on')

const Launcher = new SeleniumStandaloneLauncher()
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare({
seleniumInstallArgs : {},
seleniumArgs : {},
watch: true
})

expect(processOnSpy).toHaveBeenCalledWith('SIGINT', Launcher._stopProcess)
expect(processOnSpy).toHaveBeenCalledWith('exit', Launcher._stopProcess)
expect(processOnSpy).toHaveBeenCalledWith('uncaughtException', Launcher._stopProcess)
})
})

describe('onComplete', () => {
Expand All @@ -136,6 +156,20 @@ describe('Selenium standalone launcher', () => {

expect(Launcher.process).toBeFalsy()
})

test('should not call process.kill in watch mode', async () => {
const Launcher = new SeleniumStandaloneLauncher()
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare({
seleniumInstallArgs : {},
seleniumArgs : {},
watch: true
})

Launcher.onComplete()
expect(Launcher.process.kill).not.toBeCalled()
})
})

describe('_redirectLogStream', () => {
Expand Down

0 comments on commit 30f81e1

Please sign in to comment.