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

chore: move pdf, tracing, coverage, a11y into featuress/ #28

Merged
merged 1 commit into from
Nov 20, 2019
Merged
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
77 changes: 77 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
* [page.mainFrame()](#pagemainframe)
* [page.metrics()](#pagemetrics)
* [page.mouse](#pagemouse)
* [page.pdf](#pagepdf)
* [page.queryObjects(prototypeHandle)](#pagequeryobjectsprototypehandle)
* [page.reload([options])](#pagereloadoptions)
* [page.screenshot([options])](#pagescreenshotoptions)
Expand Down Expand Up @@ -175,6 +176,8 @@
* [mouse.move(x, y[, options])](#mousemovex-y-options)
* [mouse.tripleclick(x, y[, options])](#mousetripleclickx-y-options)
* [mouse.up([options])](#mouseupoptions)
- [class: PDF](#class-pdf)
* [pdf.generate([options])](#pdfgenerateoptions)
- [class: Touchscreen](#class-touchscreen)
* [touchscreen.tap(x, y)](#touchscreentapx-y)
- [class: Tracing](#class-tracing)
Expand Down Expand Up @@ -1656,6 +1659,9 @@ Page is guaranteed to have a main frame which persists during navigations.

- returns: <[Mouse]>

#### page.pdf
- returns: <[PDF]>

#### page.queryObjects(prototypeHandle)
- `prototypeHandle` <[JSHandle]> A handle to the object prototype.
- returns: <[Promise]<[JSHandle]>> Promise which resolves to a handle to an array of objects with this prototype.
Expand Down Expand Up @@ -2459,6 +2465,77 @@ Shortcut for [`mouse.move`](#mousemovex-y-options), [`mouse.down`](#mousedownopt

Dispatches a `mouseup` event.

### class: PDF

#### pdf.generate([options])
- `options` <[Object]> Options object which might have the following properties:
- `path` <[string]> The file path to save the PDF to. If `path` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). If no path is provided, the PDF won't be saved to the disk.
- `scale` <[number]> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.
- `displayHeaderFooter` <[boolean]> Display header and footer. Defaults to `false`.
- `headerTemplate` <[string]> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:
- `date` formatted print date
- `title` document title
- `url` document location
- `pageNumber` current page number
- `totalPages` total pages in the document
- `footerTemplate` <[string]> HTML template for the print footer. Should use the same format as the `headerTemplate`.
- `printBackground` <[boolean]> Print background graphics. Defaults to `false`.
- `landscape` <[boolean]> Paper orientation. Defaults to `false`.
- `pageRanges` <[string]> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
- `format` <[string]> Paper format. If set, takes priority over `width` or `height` options. Defaults to 'Letter'.
- `width` <[string]|[number]> Paper width, accepts values labeled with units.
- `height` <[string]|[number]> Paper height, accepts values labeled with units.
- `margin` <[Object]> Paper margins, defaults to none.
- `top` <[string]|[number]> Top margin, accepts values labeled with units.
- `right` <[string]|[number]> Right margin, accepts values labeled with units.
- `bottom` <[string]|[number]> Bottom margin, accepts values labeled with units.
- `left` <[string]|[number]> Left margin, accepts values labeled with units.
- `preferCSSPageSize` <[boolean]> Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format` options. Defaults to `false`, which will scale the content to fit the paper size.
- returns: <[Promise]<[Buffer]>> Promise which resolves with PDF buffer.

> **NOTE** Generating a pdf is currently only supported in Chrome headless.

`page.pdf()` generates a pdf of the page with `print` css media. To generate a pdf with `screen` media, call [page.emulateMedia('screen')](#pageemulatemediamediatype) before calling `page.pdf()`:

> **NOTE** By default, `page.pdf()` generates a pdf with modified colors for printing. Use the [`-webkit-print-color-adjust`](https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-print-color-adjust) property to force rendering of exact colors.

```js
// Generates a PDF with 'screen' media type.
await page.emulateMedia('screen');
await page.pdf({path: 'page.pdf'});
```

The `width`, `height`, and `margin` options accept values labeled with units. Unlabeled values are treated as pixels.

A few examples:
- `page.pdf({width: 100})` - prints with width set to 100 pixels
- `page.pdf({width: '100px'})` - prints with width set to 100 pixels
- `page.pdf({width: '10cm'})` - prints with width set to 10 centimeters.

All possible units are:
- `px` - pixel
- `in` - inch
- `cm` - centimeter
- `mm` - millimeter

The `format` options are:
- `Letter`: 8.5in x 11in
- `Legal`: 8.5in x 14in
- `Tabloid`: 11in x 17in
- `Ledger`: 17in x 11in
- `A0`: 33.1in x 46.8in
- `A1`: 23.4in x 33.1in
- `A2`: 16.54in x 23.4in
- `A3`: 11.7in x 16.54in
- `A4`: 8.27in x 11.7in
- `A5`: 5.83in x 8.27in
- `A6`: 4.13in x 5.83in

> **NOTE** `headerTemplate` and `footerTemplate` markup have the following limitations:
> 1. Script tags inside templates are not evaluated.
> 2. Page styles are not visible inside templates.


### class: Touchscreen

#### touchscreen.tap(x, y)
Expand Down
7 changes: 4 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
export = {
Chromium: {
Accessibility: require('./chromium/Accessibility').Accessibility,
Accessibility: require('./chromium/features/accessibility').Accessibility,
Browser: require('./chromium/Browser').Browser,
BrowserContext: require('./chromium/BrowserContext').BrowserContext,
BrowserFetcher: require('./chromium/BrowserFetcher').BrowserFetcher,
CDPSession: require('./chromium/Connection').CDPSession,
ConsoleMessage: require('./chromium/Page').ConsoleMessage,
Coverage: require('./chromium/Coverage').Coverage,
Coverage: require('./chromium/features/coverage').Coverage,
Dialog: require('./chromium/Dialog').Dialog,
ElementHandle: require('./chromium/JSHandle').ElementHandle,
ExecutionContext: require('./chromium/ExecutionContext').ExecutionContext,
Expand All @@ -31,6 +31,7 @@ export = {
JSHandle: require('./chromium/JSHandle').JSHandle,
Keyboard: require('./chromium/Input').Keyboard,
Mouse: require('./chromium/Input').Mouse,
PDF: require('./chromium/features/pdf').PDF,
Page: require('./chromium/Page').Page,
Playwright: require('./chromium/Playwright').Playwright,
Request: require('./chromium/NetworkManager').Request,
Expand All @@ -39,7 +40,7 @@ export = {
Target: require('./chromium/Target').Target,
TimeoutError: require('./Errors').TimeoutError,
Touchscreen: require('./chromium/Input').Touchscreen,
Tracing: require('./chromium/Tracing').Tracing,
Tracing: require('./chromium/features/tracing').Tracing,
Worker: require('./chromium/Worker').Worker,
},
Firefox: {
Expand Down
53 changes: 22 additions & 31 deletions src/chromium/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,28 @@ import { EventEmitter } from 'events';
import * as fs from 'fs';
import * as mime from 'mime';
import * as path from 'path';
import { Accessibility } from './Accessibility';
import { Events } from '../Events';
import { assert, debugError, helper } from '../helper';
import { TimeoutSettings } from '../TimeoutSettings';
import { Accessibility } from './features/accessibility';
import { Browser } from './Browser';
import { BrowserContext } from './BrowserContext';
import { CDPSession, CDPSessionEvents, Connection } from './Connection';
import { Coverage } from './Coverage';
import { Coverage } from './features/coverage';
import { Dialog, DialogType } from './Dialog';
import { EmulationManager } from './EmulationManager';
import { Events } from '../Events';
import { PDF } from './features/pdf';
import { Frame } from './Frame';
import { FrameManager, FrameManagerEvents } from './FrameManager';
import { assert, debugError, helper } from '../helper';
import { releaseObject, getExceptionMessage, valueFromRemoteObject } from './protocolHelper';
import { Keyboard, Mouse, Touchscreen } from './Input';
import { createJSHandle, ElementHandle, JSHandle, ClickOptions, PointerActionOptions, MultiClickOptions } from './JSHandle';
import { Response, NetworkManagerEvents } from './NetworkManager';
import { ClickOptions, createJSHandle, ElementHandle, JSHandle, MultiClickOptions, PointerActionOptions } from './JSHandle';
import { NetworkManagerEvents, Response } from './NetworkManager';
import { Protocol } from './protocol';
import { getExceptionMessage, releaseObject, valueFromRemoteObject } from './protocolHelper';
import { Target } from './Target';
import { TaskQueue } from './TaskQueue';
import { TimeoutSettings } from '../TimeoutSettings';
import { Tracing } from './Tracing';
import { Tracing } from './features/tracing';
import { Worker } from './Worker';
import { Target } from './Target';
import { Browser } from './Browser';
import { BrowserContext } from './BrowserContext';
import { Protocol } from './protocol';

const writeFileAsync = helper.promisify(fs.writeFile);

Expand All @@ -60,12 +61,13 @@ export class Page extends EventEmitter {
private _mouse: Mouse;
private _timeoutSettings: TimeoutSettings;
private _touchscreen: Touchscreen;
private _accessibility: Accessibility;
private _frameManager: FrameManager;
private _emulationManager: EmulationManager;
private _tracing: Tracing;
readonly accessibility: Accessibility;
readonly coverage: Coverage;
readonly pdf: PDF;
readonly tracing: Tracing;
private _pageBindings = new Map<string, Function>();
private _coverage: Coverage;
_javascriptEnabled = true;
private _viewport: Viewport | null = null;
private _screenshotTaskQueue: TaskQueue;
Expand All @@ -90,11 +92,12 @@ export class Page extends EventEmitter {
this._mouse = new Mouse(client, this._keyboard);
this._timeoutSettings = new TimeoutSettings();
this._touchscreen = new Touchscreen(client, this._keyboard);
this._accessibility = new Accessibility(client);
this.accessibility = new Accessibility(client);
this._frameManager = new FrameManager(client, this, ignoreHTTPSErrors, this._timeoutSettings);
this._emulationManager = new EmulationManager(client);
this._tracing = new Tracing(client);
this._coverage = new Coverage(client);
this.tracing = new Tracing(client);
this.coverage = new Coverage(client);
this.pdf = new PDF(client);

this._screenshotTaskQueue = screenshotTaskQueue;

Expand Down Expand Up @@ -231,18 +234,6 @@ export class Page extends EventEmitter {
return this._touchscreen;
}

get coverage(): Coverage {
return this._coverage;
}

get tracing(): Tracing {
return this._tracing;
}

get accessibility(): Accessibility {
return this._accessibility;
}

frames(): Frame[] {
return this._frameManager.frames();
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

import { CDPSession } from './Connection';
import { ElementHandle } from './JSHandle';
import { Protocol } from './protocol';
import { CDPSession } from '../Connection';
import { ElementHandle } from '../JSHandle';
import { Protocol } from '../protocol';

type SerializedAXNode = {
role: string,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
* limitations under the License.
*/

import { CDPSession } from './Connection';
import { assert, debugError, helper, RegisteredListener } from '../helper';
import { Protocol } from './protocol';
import { CDPSession } from '../Connection';
import { assert, debugError, helper, RegisteredListener } from '../../helper';
import { Protocol } from '../protocol';

const {EVALUATION_SCRIPT_URL} = require('./ExecutionContext');
import { EVALUATION_SCRIPT_URL } from '../ExecutionContext';

type CoverageEntry = {
url: string,
Expand Down
34 changes: 34 additions & 0 deletions src/chromium/features/pdf.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const fs = require('fs');
const path = require('path');

module.exports.addTests = function({testRunner, expect, headless, ASSETS_DIR}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;

// Printing to pdf is currently only supported in headless
describe.skip(!headless)('Page.pdf', function() {
it('should be able to save file', async({page, server}) => {
const outputFile = path.join(ASSETS_DIR, 'output.pdf');
await page.pdf.generate({path: outputFile});
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
fs.unlinkSync(outputFile);
});
});
};
Loading