Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
"@types/minimatch": "^2.0.29",
"@types/node": "^8.10.20",
"@types/prop-types": "^15.5.3",
"@types/puppeteer": "^1.6.2",
"@types/react": "^16.3.14",
"@types/react-dom": "^16.0.5",
"@types/react-redux": "^6.0.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { Size } from '../../../../../types';
import { ViewZoomWidthHeight } from './types';
import { ViewZoomWidthHeight } from '../../../../../types';
Copy link
Member

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';

Copy link
Contributor Author

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...


export interface PageSizeParams {
pageMarginTop: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D

const ZOOM: number = 2;

export class PreserveLayout extends Layout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/
import path from 'path';
import { KbnServer, Size } from '../../../../../types';
import { EvaluateOptions, KbnServer, Size } from '../../../../../types';
import { Layout } from './layout';
import { CaptureConfig } from './types';

type EvalArgs = any[];

interface EvaluateOptions {
// 'fn' is a function in string form to avoid tslint from auto formatting it into a version not
// underfood by transform_fn safeWrap.
fn: ((...evalArgs: EvalArgs) => any);
args: EvalArgs; // Arguments to be passed into the function defined by fn.
}

interface BrowserClient {
evaluate: (evaluateOptions: EvaluateOptions) => void;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ export interface CaptureConfig {
zoom: number;
viewport: Size;
}

export interface ViewZoomWidthHeight {
zoom: number;
width: number;
height: number;
}
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see the documentNode property getting assigned, but not used. Looks like that was the case in the previous code too. If that's intended, maybe clarify with a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
});
}
}
78 changes: 0 additions & 78 deletions x-pack/plugins/reporting/server/browsers/chromium/driver/index.js

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';
32 changes: 32 additions & 0 deletions x-pack/plugins/reporting/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,35 @@ export interface Logger {
error: (message: string) => void;
warning: (message: string) => void;
}

export interface ViewZoomWidthHeight {
zoom: number;
width: number;
height: number;
}

export type EvalArgs = any[];
export type EvalFn<T> = ((...evalArgs: EvalArgs) => T);

export interface EvaluateOptions {
fn: EvalFn<any>;
args: EvalArgs; // Arguments to be passed into the function defined by fn.
}

export interface ElementPosition {
boundingClientRect: {
// modern browsers support x/y, but older ones don't
top: number;
left: number;
width: number;
height: number;
};
scroll: {
x: number;
y: number;
};
}

export interface HeadlessElementInfo {
position: ElementPosition;
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@
version "15.5.3"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.3.tgz#bef071852dca2a2dbb65fecdb7bfb30cedae2de2"

"@types/puppeteer@^1.6.2":
version "1.6.2"
resolved "https://registry.yarnpkg.com/@types/puppeteer/-/puppeteer-1.6.2.tgz#9b4ba40a67abad3c729a021ffd259d929d00c5e0"
dependencies:
"@types/events" "*"
"@types/node" "*"

"@types/react-dom@^16.0.5":
version "16.0.5"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.0.5.tgz#a757457662e3819409229e8f86795ff37b371f96"
Expand Down