Skip to content

Commit

Permalink
Typings for chromium driver (#22688)
Browse files Browse the repository at this point in the history
* Typings for chromium driver

* buffer => base64

* remove unused param. consolidate imports
  • Loading branch information
stacey-gammon authored Sep 7, 2018
1 parent 54ab5a7 commit c9b1d7a
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 96 deletions.
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 @@ -3,8 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Size } from '../../../../../types';
import { ViewZoomWidthHeight } from './types';
import { Size, ViewZoomWidthHeight } from '../../../../../types';

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

0 comments on commit c9b1d7a

Please sign in to comment.