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

Commit

Permalink
Merge pull request #33 from erwinheitzman/fix-passing-custom-args
Browse files Browse the repository at this point in the history
Fix support for passing custom arguments
  • Loading branch information
atti187 committed Apr 16, 2020
2 parents 1782953 + 26c5a70 commit 59ba575
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ WDIO ChromeDriver Service
Note:
If you're working with WebdriverIO v6, use version 6.X.X\
If you're working with WebdriverIO v5, use version 5.X.X\
If you're working with WebdriverIO v4, use version 4.X.X\

----

Expand Down Expand Up @@ -59,6 +58,7 @@ export.config = {
services: [
['chromedriver', {
outputDir: 'driver-logs', // overwrites the config.outputDir
args: ['--silent'] //
}]
],
// ...
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wdio-chromedriver-service",
"version": "6.0.1",
"version": "6.0.2",
"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
17 changes: 13 additions & 4 deletions src/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,29 @@ export default class ChromeDriverLauncher {

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

async onPrepare() {
this.chromeDriverArgs.push(`--port=${this.options.port}`)
this.chromeDriverArgs.push(`--url-base=${this.options.path}`)
this.args.forEach(argument => {
if (argument.includes('--port')) {
throw new Error('Argument "--port" already exists')
}
if (argument.includes('--url-base')) {
throw new Error('Argument "--url-base" already exists')
}
})

this.args.push(`--port=${this.options.port}`)
this.args.push(`--url-base=${this.options.path}`)

/**
* update capability connection options to connect
* to chromedriver
*/
this._mapCapabilities()

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

if (typeof this.outputDir === 'string') {
this._redirectLogStream()
Expand Down
146 changes: 74 additions & 72 deletions tests/launcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jest.mock('fs-extra', () => ({

const pipe = jest.fn()

let config, options, capabilities, multiremoteCaps

describe('ChromeDriverLauncher launcher', () => {
beforeAll(() => {
ChromeDriver.start = jest.fn().mockReturnValue({
Expand All @@ -18,13 +20,26 @@ describe('ChromeDriverLauncher launcher', () => {
})
})

beforeEach(() => {
config = {}
options = {}
capabilities = [
{ browserName: 'chrome' },
{ browserName: 'firefox' }
]
multiremoteCaps = {
myCustomChromeBrowser: { browserName: 'chrome' },
myCustomFirefoxBrowser: { browserName: 'firefox' }
}
})

afterEach(() => {
jest.clearAllMocks()
})

describe('onPrepare', () => {
test('should set correct starting options', async () => {
const Launcher = new ChromeDriverLauncher({}, [{ browserName: 'chrome' }, { browserName: 'firefox' }], {})
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -33,7 +48,9 @@ describe('ChromeDriverLauncher launcher', () => {
})

test('should set (and overwrite config.outputDir) outputDir when passed in the options', async () => {
const Launcher = new ChromeDriverLauncher({ outputDir: 'options-outputdir'}, [], { outputDir: 'config-outputdir'})
options.outputDir = 'options-outputdir'
config.outputDir = 'config-outputdir'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -42,7 +59,8 @@ describe('ChromeDriverLauncher launcher', () => {
})

test('should set path when passed in the options', async () => {
const Launcher = new ChromeDriverLauncher({ path: 'options-path'}, [{ browserName: 'chrome' }, { browserName: 'firefox' }], {})
options.path = 'options-path'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -62,7 +80,8 @@ describe('ChromeDriverLauncher launcher', () => {
})

test('should set port when passed in the options', async () => {
const Launcher = new ChromeDriverLauncher({ port: 7676}, [{ browserName: 'chrome' }, { browserName: 'firefox' }], {})
options.port = 7676
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -82,7 +101,8 @@ describe('ChromeDriverLauncher launcher', () => {
})

test('should set protocol when passed in the options', async () => {
const Launcher = new ChromeDriverLauncher({ protocol: 'https'}, [{ browserName: 'chrome' }, { browserName: 'firefox' }], {})
options.protocol = 'https'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -102,7 +122,8 @@ describe('ChromeDriverLauncher launcher', () => {
})

test('should set hostname when passed in the options', async () => {
const Launcher = new ChromeDriverLauncher({ hostname: 'dummy'}, [{ browserName: 'chrome' }, { browserName: 'firefox' }], {})
options.hostname = 'dummy'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -121,8 +142,8 @@ describe('ChromeDriverLauncher launcher', () => {
])
})

test('should set correct capabilities', async () => {
const Launcher = new ChromeDriverLauncher({}, [{ browserName: 'chrome' }, { browserName: 'firefox' }], {})
test('should set capabilities', async () => {
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -141,8 +162,8 @@ describe('ChromeDriverLauncher launcher', () => {
])
})

test('should set correct capabilities when using multiremote', async () => {
const Launcher = new ChromeDriverLauncher({}, { myCustomChromeBrowser: { browserName: 'chrome' }, myCustomFirefoxBrowser: { browserName: 'firefox' }}, {})
test('should set capabilities when using multiremote', async () => {
const Launcher = new ChromeDriverLauncher(options, multiremoteCaps, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -161,8 +182,13 @@ describe('ChromeDriverLauncher launcher', () => {
})
})

test('should set correct capabilities when the browserName is not lowercase', async () => {
const Launcher = new ChromeDriverLauncher({}, [{ browserName: 'Chrome' }, { browserName: 'firefox' }], {})
test('should set capabilities when the browserName is not lowercase', async () => {
capabilities.map(cap => {
if (cap.browserName === 'chrome') {
cap.browserName = 'Chrome'
}
})
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -182,96 +208,70 @@ describe('ChromeDriverLauncher launcher', () => {
})

test('should set correct config properties', async () => {
const Launcher = new ChromeDriverLauncher({}, [], { outputDir: 'dummy'})
config.outputDir = 'dummy'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()

expect(Launcher.outputDir).toEqual('dummy')
})

test('should set correct chromeDriverArgs', async () => {
const Launcher = new ChromeDriverLauncher({}, [], {})
test('should set correct port and path', async () => {
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

const config = {
port: 9515,
path: '/'
}

await Launcher.onPrepare(config)
await Launcher.onPrepare()

expect(Launcher.chromeDriverArgs).toEqual([`--port=${config.port}`, `--url-base=${config.path}`])
expect(Launcher.args).toEqual(['--port=9515', '--url-base=/'])
})

test('should set correct config properties when empty', async () => {
const Launcher = new ChromeDriverLauncher({}, [], {})
test('should set correct args', async () => {
options.args = ['--silent']
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare({})
await Launcher.onPrepare()

expect(Launcher.chromeDriverArgs).toBeUndefined
expect(Launcher.args).toEqual(['--silent', '--port=9515', '--url-base=/'])
})

test('should call ChromeDriver start', async () => {
const Launcher = new ChromeDriverLauncher({}, [], {})
test('should throw if the argument "--port" is passed', async () => {
options.args = ['--port=9616']
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
await expect(Launcher.onPrepare()).rejects.toThrow(new Error('Argument "--port" already exists'))
})

expect(ChromeDriver.start.mock.calls[0][0]).toEqual(['--port=9515', '--url-base=/'])
test('should throw if the argument "--url-base" is passed', async () => {
options.args = ['--url-base=/dummy']
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await expect(Launcher.onPrepare()).rejects.toThrow(new Error('Argument "--url-base" already exists'))
})

test('should map the capabilities', async () => {
const Launcher = new ChromeDriverLauncher({}, [{ browserName: 'chrome' }, { browserName: 'chrome' }], {})
test('should set correct config properties when empty', async () => {
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
await Launcher.onPrepare({})

expect(Launcher.capabilities).toEqual([
{
browserName: 'chrome',
protocol: 'http',
hostname: 'localhost',
port: 9515,
path: '/'
},
{
browserName: 'chrome',
protocol: 'http',
hostname: 'localhost',
port: 9515,
path: '/'
},
])
expect(Launcher.args).toBeUndefined
})

test('should map the capabilities using multiremote', async () => {
const Launcher = new ChromeDriverLauncher({}, { dummy1: { browserName: 'chrome' }, dummy2: { browserName: 'chrome' } }, {})
test('should call ChromeDriver start', async () => {
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()

expect(Launcher.capabilities).toEqual({
dummy1: {
browserName: 'chrome',
protocol: 'http',
hostname: 'localhost',
port: 9515,
path: '/'
},
dummy2: {
browserName: 'chrome',
protocol: 'http',
hostname: 'localhost',
port: 9515,
path: '/'
},
})
expect(ChromeDriver.start.mock.calls[0][0]).toEqual(['--port=9515', '--url-base=/'])
})

test('should not output the log file', async () => {
const Launcher = new ChromeDriverLauncher({}, [], {})
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare({})
Expand All @@ -280,7 +280,8 @@ describe('ChromeDriverLauncher launcher', () => {
})

test('should output the log file', async () => {
const Launcher = new ChromeDriverLauncher({ outputDir: 'dummy' }, [], {})
options.outputDir = 'dummy'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare()
Expand All @@ -291,7 +292,7 @@ describe('ChromeDriverLauncher launcher', () => {

describe('onComplete', () => {
test('should call ChromeDriver.stop', async () => {
const Launcher = new ChromeDriverLauncher({}, [], {})
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher._redirectLogStream = jest.fn()

await Launcher.onPrepare({})
Expand All @@ -302,7 +303,7 @@ describe('ChromeDriverLauncher launcher', () => {
})

test('should not call process.kill', () => {
const Launcher = new ChromeDriverLauncher({}, [], {})
const Launcher = new ChromeDriverLauncher(options, capabilities, config)
Launcher.onComplete()

expect(Launcher.process).toBeFalsy()
Expand All @@ -311,7 +312,8 @@ describe('ChromeDriverLauncher launcher', () => {

describe('_redirectLogStream', () => {
test('should write output to file', async () => {
const Launcher = new ChromeDriverLauncher({}, [], { outputDir: 'dummy'})
config.outputDir = 'dummy'
const Launcher = new ChromeDriverLauncher(options, capabilities, config)

await Launcher.onPrepare()

Expand Down

0 comments on commit 59ba575

Please sign in to comment.