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

Commit

Permalink
Add pollTimeout to options (#193)
Browse files Browse the repository at this point in the history
* Add pollTimeout to options

* Fix Readme and Comment

* Apply suggestions from code review

Co-authored-by: Christian Bromann <git@bromann.dev>

* Add default options and pollTimeout test

---------

Co-authored-by: Mahdyar Safarianbarmi <mahdyar.safarianbarmi@adesso.de>
Co-authored-by: Christian Bromann <git@bromann.dev>
  • Loading branch information
3 people committed Feb 13, 2023
1 parent 8d14f98 commit 2c738d3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 29 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export declare interface ChromedriverServiceOptions {
hostname?: string;
port?: string;
path?: string;
pollTimeout?: number;
outputDir?: string;
logFileName?: string;
args?: string[];
Expand All @@ -13,7 +14,7 @@ export declare interface ChromedriverServiceOptions {

export declare class ChromedriverServiceLauncher {
public chromedriverCustomPath: string;
public options: Pick<ChromedriverServiceOptions, 'protocol' | 'hostname' | 'port' | 'path'>;
public options: Pick<ChromedriverServiceOptions, 'protocol' | 'hostname' | 'port' | 'path' | 'pollTimeout'>;
public outputDir: string;
public logFileName: string;
public capabilities: Capabilities.Capabilities;
Expand Down
9 changes: 5 additions & 4 deletions src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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` +
Expand All @@ -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` +
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
87 changes: 63 additions & 24 deletions tests/launcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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'
},
{
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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',
}
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 2c738d3

Please sign in to comment.