diff --git a/README.md b/README.md index b2f8b8f..5db1333 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,13 @@ Example: `localhost` Type: `string` +### pollTimeout +The startup timeout in ms, it checks if the port is open before starting ChromeDriver and then checks again if the it is closed after starting it. + +Example: `10000` + +Type: `number` + ### outputDir The path where the output of the ChromeDriver server should be stored (uses the config.outputDir by default when not set). diff --git a/index.d.ts b/index.d.ts index 54b0e58..f2c807c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,6 +5,7 @@ export declare interface ChromedriverServiceOptions { hostname?: string; port?: string; path?: string; + pollTimeout?: number; outputDir?: string; logFileName?: string; args?: string[]; @@ -13,7 +14,7 @@ export declare interface ChromedriverServiceOptions { export declare class ChromedriverServiceLauncher { public chromedriverCustomPath: string; - public options: Pick; + public options: Pick; public outputDir: string; public logFileName: string; public capabilities: Capabilities.Capabilities; diff --git a/src/launcher.ts b/src/launcher.ts index 2b14fc9..86dd465 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -16,7 +16,7 @@ const log = logger('chromedriver') const DEFAULT_LOG_FILENAME = 'wdio-chromedriver.log' const POLL_INTERVAL = 100 -const POLL_TIMEOUT = 10000 +const DEFAULT_POLL_TIMEOUT = 10000 const DEFAULT_CONNECTION = { protocol: 'http' as const, hostname: 'localhost', @@ -28,7 +28,7 @@ const isMultiremote = (obj: Capabilities.Capabilities) => typeof obj === 'object const isChrome = (cap: Capabilities.Capabilities) => cap.browserName && cap.browserName.toLowerCase() === 'chrome' export default class ChromeDriverLauncher { - protected options: { protocol: 'http' | 'https', hostname: string, port: number, path: string } + protected options: { protocol: 'http' | 'https', hostname: string, port: number, path: string, pollTimeout: number } protected outputDir?: string protected logFileName: string protected capabilities: Capabilities.Capabilities @@ -47,6 +47,7 @@ export default class ChromeDriverLauncher { hostname: options.hostname || DEFAULT_CONNECTION.hostname, port: options.port || DEFAULT_CONNECTION.port, path: options.path || DEFAULT_CONNECTION.path, + pollTimeout: options.pollTimeout || DEFAULT_POLL_TIMEOUT } this.outputDir = options.outputDir || config.outputDir @@ -89,7 +90,7 @@ export default class ChromeDriverLauncher { * wait for port to be available before starting Chromedriver */ try { - await tcpPortUsed.waitUntilFree(this.options.port, POLL_INTERVAL, POLL_TIMEOUT) + await tcpPortUsed.waitUntilFree(this.options.port, POLL_INTERVAL, this.options.pollTimeout) } catch (err) { throw new SevereServiceError( `Couldn't start Chromedriver: ${err.message}\n` + @@ -107,7 +108,7 @@ export default class ChromeDriverLauncher { } try { - await tcpPortUsed.waitUntilUsed(this.options.port, POLL_INTERVAL, POLL_TIMEOUT) + await tcpPortUsed.waitUntilUsed(this.options.port, POLL_INTERVAL, this.options.pollTimeout) } catch (err) { throw new SevereServiceError( `Couldn't start Chromedriver: ${err.message}\n` + diff --git a/src/types.ts b/src/types.ts index 5e13923..ef71615 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,6 +7,10 @@ export interface ServiceOptions { * The path on which the driver should run on */ path?: string + /** + * The startup timeout in ms, it checks if the port is open before starting ChromeDriver Server and then checks again if the chromedriver port is closed after starting ChromeDriver Server. + */ + pollTimeout?: number /** * The protocol on which the driver should use */ diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 9c18442..524f3ce 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -29,6 +29,16 @@ vi.mock('child_process', () => { let config, options, capabilities, multiremoteCaps describe('ChromeDriverLauncher launcher', () => { + + const defaultOptions = { + defaultLogFileName: 'wdio-chromedriver.log', + defaultProtocol: 'http', + defaultHostname: 'localhost', + defaultPort: 9515, + defaultPath: '/', + defaultPollTimeOut: 10000, + } + beforeEach(() => { config = {} options = {} @@ -106,9 +116,10 @@ describe('ChromeDriverLauncher launcher', () => { expect(launcher['capabilities']).toEqual([ { browserName: 'chrome', - protocol: 'http', - hostname: 'localhost', - port: 9515, + protocol: defaultOptions.defaultProtocol, + hostname: defaultOptions.defaultHostname, + port: defaultOptions.defaultPort, + pollTimeout:defaultOptions.defaultPollTimeOut, path: 'options-path' }, { @@ -127,10 +138,11 @@ describe('ChromeDriverLauncher launcher', () => { expect(launcher['capabilities']).toEqual([ { browserName: 'chrome', - protocol: 'http', - hostname: 'localhost', + protocol: defaultOptions.defaultProtocol, + hostname: defaultOptions.defaultHostname, port: 7676, - path: '/' + pollTimeout:defaultOptions.defaultPollTimeOut, + path: defaultOptions.defaultPath }, { browserName: 'firefox' @@ -149,9 +161,10 @@ describe('ChromeDriverLauncher launcher', () => { { browserName: 'chrome', protocol: 'https', - hostname: 'localhost', - port: 9515, - path: '/' + hostname: defaultOptions.defaultHostname, + port: defaultOptions.defaultPort, + pollTimeout:defaultOptions.defaultPollTimeOut, + path: defaultOptions.defaultPath }, { browserName: 'firefox' @@ -169,10 +182,33 @@ describe('ChromeDriverLauncher launcher', () => { expect(launcher['capabilities']).toEqual([ { browserName: 'chrome', - protocol: 'http', + protocol: defaultOptions.defaultProtocol, hostname: 'dummy', - port: 9515, - path: '/' + port: defaultOptions.defaultPort, + pollTimeout:defaultOptions.defaultPollTimeOut, + path: defaultOptions.defaultPath + }, + { + browserName: 'firefox' + } + ]) + }) + + it('should set pollTimeout when passed in the options', async () => { + options.pollTimeout = 15000 + const launcher = new ChromeDriverLauncher(options, capabilities, config) + launcher._redirectLogStream = vi.fn() + + await launcher.onPrepare() + + expect(launcher['capabilities']).toEqual([ + { + browserName: 'chrome', + protocol: defaultOptions.defaultProtocol, + hostname: defaultOptions.defaultHostname, + port: defaultOptions.defaultPort, + pollTimeout: 15000, + path: defaultOptions.defaultPath }, { browserName: 'firefox' @@ -189,10 +225,11 @@ describe('ChromeDriverLauncher launcher', () => { expect(launcher['capabilities']).toEqual([ { browserName: 'chrome', - protocol: 'http', - hostname: 'localhost', - port: 9515, - path: '/' + protocol: defaultOptions.defaultProtocol, + hostname: defaultOptions.defaultHostname, + port: defaultOptions.defaultPort, + pollTimeout:defaultOptions.defaultPollTimeOut, + path: defaultOptions.defaultPath }, { browserName: 'firefox' @@ -208,10 +245,11 @@ describe('ChromeDriverLauncher launcher', () => { expect(launcher['capabilities']).toEqual({ myCustomChromeBrowser: { - protocol: 'http', - hostname: 'localhost', - port: 9515, - path: '/', + protocol: defaultOptions.defaultProtocol, + hostname: defaultOptions.defaultHostname, + port: defaultOptions.defaultPort, + pollTimeout:defaultOptions.defaultPollTimeOut, + path: defaultOptions.defaultPath, capabilities: { browserName: 'chrome', } @@ -243,10 +281,11 @@ describe('ChromeDriverLauncher launcher', () => { expect(launcher['capabilities']).toEqual([ { browserName: 'Chrome', - protocol: 'http', - hostname: 'localhost', - port: 9515, - path: '/' + protocol: defaultOptions.defaultProtocol, + hostname: defaultOptions.defaultHostname, + port: defaultOptions.defaultPort, + pollTimeout:defaultOptions.defaultPollTimeOut, + path: defaultOptions.defaultPath }, { browserName: 'firefox'