Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Enable chromeFlags and customize userDataDir #310

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default class Chromeless<T extends any> implements Promise<T> {
implicitWait: true,
scrollBeforeClick: false,
launchChrome: true,
chromeFlags: [],
userDataDir: undefined,

...options,

Expand Down
24 changes: 15 additions & 9 deletions src/chrome/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,27 @@ export default class LocalChrome implements Chrome {
}

private async startChrome(): Promise<Client> {
const { port } = this.options.cdp
this.chromeInstance = await launch({
logLevel: this.options.debug ? 'info' : 'silent',
port: this.options.cdp.port,
port,
chromeFlags: this.options.chromeFlags,
chromePath: this.options.chromePath,
userDataDir: this.options.userDataDir
})
const target = await CDP.New({
port: this.chromeInstance.port,
port,
})
return await CDP({ target })
return await CDP({ target, port })
}

private async connectToChrome(): Promise<Client> {
const { host, port } = this.options.cdp
const target = await CDP.New({
port: this.options.cdp.port,
host: this.options.cdp.host,
port,
host,
})
return await CDP({ target })
return await CDP({ target, host, port })
}

private async setViewport(client: Client) {
Expand All @@ -63,8 +68,8 @@ export default class LocalChrome implements Chrome {
fitWindow: false, // as we cannot resize the window, `fitWindow: false` is needed in order for the viewport to be resizable
}

const port = this.options.cdp.port
const versionResult = await CDP.Version({ port })
const { host, port } = this.options.cdp
const versionResult = await CDP.Version({ host, port })
const isHeadless = versionResult['User-Agent'].includes('Headless')

if (viewport.height && viewport.width) {
Expand Down Expand Up @@ -102,7 +107,8 @@ export default class LocalChrome implements Chrome {
const { client } = await this.runtimeClientPromise

if (this.options.cdp.closeTab) {
await CDP.Close({ id: client.target.id })
const { host, port } = this.options.cdp
await CDP.Close({ host, port, id: client.target.id })
}

if (this.chromeInstance) {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface Client {
target: {
id: string
}
port: any
host: any
}

export interface DeviceMetrics {
Expand Down Expand Up @@ -50,6 +52,9 @@ export interface ChromelessOptions {
launchChrome?: boolean // auto-launch chrome (local) `true`
cdp?: CDPOptions
remote?: RemoteOptions | boolean
chromeFlags?: Array<string> // ['--headless']
chromePath?: string
userDataDir?: string
}

export interface Chrome {
Expand Down
3 changes: 2 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export async function setViewport(
fitWindow: false, // as we cannot resize the window, `fitWindow: false` is needed in order for the viewport to be resizable
}

const versionResult = await CDP.Version()
const { host, port } = client
const versionResult = await CDP.Version({ host, port })
const isHeadless = versionResult['User-Agent'].includes('Headless')

if (viewport.height && viewport.width) {
Expand Down