-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Typings for chromium driver (#22688)
* Typings for chromium driver * buffer => base64 * remove unused param. consolidate imports
- Loading branch information
1 parent
54ab5a7
commit c9b1d7a
Showing
10 changed files
with
142 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as Chrome from 'puppeteer'; | ||
import { | ||
ElementPosition, | ||
EvalArgs, | ||
EvalFn, | ||
EvaluateOptions, | ||
Logger, | ||
ViewZoomWidthHeight, | ||
} from '../../../../types'; | ||
|
||
export interface ChromiumDriverOptions { | ||
logger: Logger; | ||
} | ||
|
||
const WAIT_FOR_DELAY_MS: number = 100; | ||
|
||
export class HeadlessChromiumDriver { | ||
private readonly page: Chrome.Page; | ||
private readonly logger: Logger; | ||
|
||
constructor(page: Chrome.Page, { logger }: ChromiumDriverOptions) { | ||
this.page = page; | ||
this.logger = logger; | ||
} | ||
|
||
public async open( | ||
url: string, | ||
{ headers, waitForSelector }: { headers: Record<string, string>; waitForSelector: string } | ||
) { | ||
this.logger.debug(`HeadlessChromiumDriver:opening url ${url}`); | ||
|
||
await this.page.setExtraHTTPHeaders(headers); | ||
await this.page.goto(url, { waitUntil: 'networkidle0' }); | ||
await this.page.waitFor(waitForSelector); | ||
} | ||
|
||
public async screenshot(elementPosition: ElementPosition) { | ||
let clip; | ||
if (elementPosition) { | ||
const { boundingClientRect, scroll = { x: 0, y: 0 } } = elementPosition; | ||
clip = { | ||
x: boundingClientRect.left + scroll.x, | ||
y: boundingClientRect.top + scroll.y, | ||
height: boundingClientRect.height, | ||
width: boundingClientRect.width, | ||
}; | ||
} | ||
|
||
const screenshot = await this.page.screenshot({ | ||
clip, | ||
}); | ||
|
||
return screenshot.toString('base64'); | ||
} | ||
|
||
public async evaluate({ fn, args = [] }: EvaluateOptions) { | ||
const result = await this.page.evaluate(fn, ...args); | ||
return result; | ||
} | ||
|
||
public waitForSelector(selector: string) { | ||
return this.page.waitFor(selector); | ||
} | ||
|
||
public async waitFor<T>({ fn, args, toEqual }: { fn: EvalFn<T>; args: EvalArgs; toEqual: T }) { | ||
while (true) { | ||
const result = await this.evaluate({ fn, args }); | ||
if (result === toEqual) { | ||
return; | ||
} | ||
|
||
await new Promise(r => setTimeout(r, WAIT_FOR_DELAY_MS)); | ||
} | ||
} | ||
|
||
public async setViewport({ width, height, zoom }: ViewZoomWidthHeight) { | ||
this.logger.debug(`Setting viewport to width: ${width}, height: ${height}, zoom: ${zoom}`); | ||
|
||
await this.page.setViewport({ | ||
width: Math.floor(width / zoom), | ||
height: Math.floor(height / zoom), | ||
deviceScaleFactor: zoom, | ||
isMobile: false, | ||
}); | ||
} | ||
} |
78 changes: 0 additions & 78 deletions
78
x-pack/plugins/reporting/server/browsers/chromium/driver/index.js
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/reporting/server/browsers/chromium/driver/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export { HeadlessChromiumDriver } from './chromium_driver'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters