-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typings for chromium driver #22688
Typings for chromium driver #22688
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import path from 'path'; | |
import { Size } from '../../../../../types'; | ||
import { Layout, PageSizeParams } from './layout'; | ||
|
||
// We use a zoom of two to bump up the resolution of the screenshot a bit. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :D |
||
const ZOOM: number = 2; | ||
|
||
export class PreserveLayout extends Layout { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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 { | ||
public documentNode?: Chrome.JSHandle; | ||
|
||
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' }); | ||
|
||
this.documentNode = await this.page.evaluateHandle(() => document); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch |
||
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, | ||
}); | ||
} | ||
} |
This file was deleted.
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { Size, ViewZoomWidthHeight } from '../../../../../types';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huh, how the heck did that pass the linter...