Skip to content

Commit

Permalink
api(video): simplify video api (#3924)
Browse files Browse the repository at this point in the history
- This leaves just `recordVideos` and `videoSize` options on the context.
- Videos are saved to `artifactsPath`. We also save their ids to trace.
- `context.close()` waits for the processed videos.
  • Loading branch information
dgozman authored Sep 19, 2020
1 parent 4e2d75d commit df77734
Show file tree
Hide file tree
Showing 27 changed files with 239 additions and 493 deletions.
45 changes: 6 additions & 39 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ Indicates that the browser is connected.
- `colorScheme` <"light"|"dark"|"no-preference"> Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`. See [page.emulateMedia(options)](#pageemulatemediaoptions) for more details. Defaults to '`light`'.
- `logger` <[Logger]> Logger sink for Playwright logging.
- `relativeArtifactsPath` <[string]> Specifies a folder for artifacts like downloads, videos and traces, relative to `artifactsPath` from [`browserType.launch`](#browsertypelaunchoptions). Defaults to `.`.
- `_recordVideos` <[boolean]> **experimental** Enables automatic video recording for new pages.
- `_videoSize` <[Object]> **experimental** Specifies dimensions of the automatically recorded video. Can only be used if `_recordVideos` is true. If not specified the size will be equal to `viewport`. If `viewport` is not configured explicitly the video size defaults to 1280x720. Actual picture of the page will be scaled down if necessary to fit specified size.
- `recordVideos` <[boolean]> Enables video recording for all pages to the `relativeArtifactsPath` folder.
- `videoSize` <[Object]> Specifies dimensions of the automatically recorded video. Can only be used if `recordVideos` is true. If not specified the size will be equal to `viewport`. If `viewport` is not configured explicitly the video size defaults to 1280x720. Actual picture of the page will be scaled down if necessary to fit specified size.
- `width` <[number]> Video frame width.
- `height` <[number]> Video frame height.
- `recordTrace` <[boolean]> Enables trace recording to the `relativeArtifactsPath` folder.
Expand Down Expand Up @@ -269,8 +269,8 @@ Creates a new browser context. It won't share cookies/cache with other browser c
- `colorScheme` <"light"|"dark"|"no-preference"> Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`. See [page.emulateMedia(options)](#pageemulatemediaoptions) for more details. Defaults to '`light`'.
- `logger` <[Logger]> Logger sink for Playwright logging.
- `relativeArtifactsPath` <[string]> Specifies a folder for artifacts like downloads, videos and traces, relative to `artifactsPath` from [`browserType.launch`](#browsertypelaunchoptions). Defaults to `.`.
- `_recordVideos` <[boolean]> **experimental** Enables automatic video recording for the new page.
- `_videoSize` <[Object]> **experimental** Specifies dimensions of the automatically recorded video. Can only be used if `_recordVideos` is true. If not specified the size will be equal to `viewport`. If `viewport` is not configured explicitly the video size defaults to 1280x720. Actual picture of the page will be scaled down if necessary to fit specified size.
- `recordVideos` <[boolean]> Enables video recording for all pages to the `relativeArtifactsPath` folder.
- `videoSize` <[Object]> Specifies dimensions of the automatically recorded video. Can only be used if `recordVideos` is true. If not specified the size will be equal to `viewport`. If `viewport` is not configured explicitly the video size defaults to 1280x720. Actual picture of the page will be scaled down if necessary to fit specified size.
- `width` <[number]> Video frame width.
- `height` <[number]> Video frame height.
- `recordTrace` <[boolean]> Enables trace recording to the `relativeArtifactsPath` folder.
Expand Down Expand Up @@ -701,7 +701,6 @@ page.removeListener('request', logRequest);
```

<!-- GEN:toc -->
- [event: '_videostarted'](#event-_videostarted)
- [event: 'close'](#event-close-1)
- [event: 'console'](#event-console)
- [event: 'crash'](#event-crash)
Expand Down Expand Up @@ -788,35 +787,6 @@ page.removeListener('request', logRequest);
- [page.workers()](#pageworkers)
<!-- GEN:stop -->

#### event: '_videostarted'
- <[Object]> Video object. Provides access to the video after it has been written to a file.

**experimental**
Emitted when video recording has started for this page. The event will fire only if [`_recordVideos`](#browsernewcontextoptions) option is configured on the parent context.

An example of recording a video for single page.
```js
const { webkit } = require('playwright'); // Or 'chromium' or 'firefox'.

(async () => {
const browser = await webkit.launch({
_videosPath: __dirname // Save videos to custom directory
});
const context = await browser.newContext({
_recordVideos: true,
_videoSize: { width: 640, height: 360 }
});
const page = await context.newPage();
const video = await page.waitForEvent('_videostarted');
await page.goto('https://github.com/microsoft/playwright');
// Video recording will stop automaticall when the page closes.
await page.close();
// Wait for the path to the video. It will become available
// after the video has been completely written to the the file.
console.log('Recorded video: ' + await video.path());
})();
```

#### event: 'close'

Emitted when the page closes.
Expand Down Expand Up @@ -4205,7 +4175,6 @@ This methods attaches Playwright to an existing browser instance.
- `password` <[string]> Optional password to use if HTTP proxy requires authentication.
- `downloadsPath` <[string]> If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
- `artifactsPath` <[string]> Specifies a folder for various artifacts like downloads, videos and traces. If not specified, artifacts are not collected.
- `_videosPath` <[string]> **experimental** If specified, recorded videos are saved into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
- `chromiumSandbox` <[boolean]> Enable Chromium sandboxing. Defaults to `true`.
- `firefoxUserPrefs` <[Object]<[string], [string]|[number]|[boolean]>> Firefox user preferences. Learn more about the Firefox user preferences at [`about:config`](https://support.mozilla.org/en-US/kb/about-config-editor-firefox).
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
Expand Down Expand Up @@ -4282,9 +4251,8 @@ const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
- `password` <[string]>
- `colorScheme` <"light"|"dark"|"no-preference"> Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`. See [page.emulateMedia(options)](#pageemulatemediaoptions) for more details. Defaults to '`light`'.
- `relativeArtifactsPath` <[string]> Specifies a folder for artifacts like downloads, videos and traces, relative to `artifactsPath`. Defaults to `.`.
- `_videosPath` <[string]> **experimental** If specified, recorded videos are saved into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
- `_recordVideos` <[boolean]> **experimental** Enables automatic video recording for new pages.
- `_videoSize` <[Object]> **experimental** Specifies dimensions of the automatically recorded video. Can only be used if `_recordVideos` is true. If not specified the size will be equal to `viewport`. If `viewport` is not configured explicitly the video size defaults to 1280x720. Actual picture of the page will be scaled down if necessary to fit specified size.
- `recordVideos` <[boolean]> Enables video recording for all pages to the `relativeArtifactsPath` folder.
- `videoSize` <[Object]> Specifies dimensions of the automatically recorded video. Can only be used if `recordVideos` is true. If not specified the size will be equal to `viewport`. If `viewport` is not configured explicitly the video size defaults to 1280x720. Actual picture of the page will be scaled down if necessary to fit specified size.
- `width` <[number]> Video frame width.
- `height` <[number]> Video frame height.
- `recordTrace` <[boolean]> Enables trace recording to the `relativeArtifactsPath` folder.
Expand All @@ -4306,7 +4274,6 @@ Launches browser that uses persistent storage located at `userDataDir` and retur
- `password` <[string]> Optional password to use if HTTP proxy requires authentication.
- `downloadsPath` <[string]> If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
- `artifactsPath` <[string]> Specifies a folder for various artifacts like downloads, videos and traces. If not specified, artifacts are not collected.
- `_videosPath` <[string]> **experimental** If specified, recorded videos are saved into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
- `chromiumSandbox` <[boolean]> Enable Chromium sandboxing. Defaults to `true`.
- `firefoxUserPrefs` <[Object]<[string], [string]|[number]|[boolean]>> Firefox user preferences. Learn more about the Firefox user preferences at [`about:config`](https://support.mozilla.org/en-US/kb/about-config-editor-firefox).
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
Expand Down
17 changes: 7 additions & 10 deletions packages/installation-tests/screencast.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ const fs = require('fs');
for (const browserType of success) {
try {
const browser = await playwright[browserType].launch({
_videosPath: __dirname,
artifactsPath: __dirname,
});
const context = await browser.newContext({
_recordVideos: true,
_videoSize: {width: 320, height: 240},
recordVideos: true,
videoSize: {width: 320, height: 240},
});
const page = await context.newPage();
const video = await page.waitForEvent('_videostarted');
await context.newPage();
// Wait fo 1 second to actually record something.
await new Promise(x => setTimeout(x, 1000));
const [videoFile] = await Promise.all([
video.path(),
context.close(),
]);
await context.close();
await browser.close();
if (!fs.existsSync(videoFile)) {
const videoFile = fs.readdirSync(__dirname).find(name => name.endsWith('webm'));
if (!videoFile) {
console.error(`ERROR: Package "${requireName}", browser "${browserType}" should have created screencast!`);
process.exit(1);
}
Expand Down
4 changes: 0 additions & 4 deletions src/client/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import { WebKitBrowser } from './webkitBrowser';
import { FirefoxBrowser } from './firefoxBrowser';
import { debugLogger } from '../utils/debugLogger';
import { SelectorsOwner } from './selectors';
import { Video } from './video';
import { isUnderTest } from '../utils/utils';

class Root extends ChannelOwner<channels.Channel, {}> {
Expand Down Expand Up @@ -221,9 +220,6 @@ export class Connection {
case 'Route':
result = new Route(parent, type, guid, initializer);
break;
case 'Video':
result = new Video(parent, type, guid, initializer);
break;
case 'Stream':
result = new Stream(parent, type, guid, initializer);
break;
Expand Down
1 change: 0 additions & 1 deletion src/client/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const Events = {
Load: 'load',
Popup: 'popup',
Worker: 'worker',
_VideoStarted: '_videostarted',
},

Worker: {
Expand Down
6 changes: 0 additions & 6 deletions src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import * as util from 'util';
import { Size, URLMatch, Headers, LifecycleEvent, WaitForEventOptions, SelectOption, SelectOptionOptions, FilePayload, WaitForFunctionOptions } from './types';
import { evaluationScript, urlMatches } from './clientHelper';
import { isString, isRegExp, isObject, mkdirIfNeeded, headersObjectToArray } from '../utils/utils';
import { Video } from './video';

type PDFOptions = Omit<channels.PagePdfParams, 'width' | 'height' | 'margin'> & {
width?: string | number,
Expand Down Expand Up @@ -123,7 +122,6 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
this._channel.on('response', ({ response }) => this.emit(Events.Page.Response, Response.from(response)));
this._channel.on('route', ({ route, request }) => this._onRoute(Route.from(route), Request.from(request)));
this._channel.on('worker', ({ worker }) => this._onWorker(Worker.from(worker)));
this._channel.on('videoStarted', params => this._onVideoStarted(params));

if (this._browserContext._browserName === 'chromium') {
this.coverage = new ChromiumCoverage(this._channel);
Expand Down Expand Up @@ -177,10 +175,6 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
this.emit(Events.Page.Worker, worker);
}

private _onVideoStarted(params: channels.PageVideoStartedEvent): void {
this.emit(Events.Page._VideoStarted, Video.from(params.video));
}

_onClose() {
this._closed = true;
this._browserContext._pages.delete(this);
Expand Down
1 change: 0 additions & 1 deletion src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export type LaunchServerOptions = {
},
downloadsPath?: string,
artifactsPath?: string,
_videosPath?: string,
chromiumSandbox?: boolean,
port?: number,
logger?: Logger,
Expand Down
70 changes: 0 additions & 70 deletions src/client/video.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/dispatchers/pageDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { serializeResult, parseArgument } from './jsHandleDispatcher';
import { ElementHandleDispatcher, createHandle } from './elementHandlerDispatcher';
import { FileChooser } from '../server/fileChooser';
import { CRCoverage } from '../server/chromium/crCoverage';
import { VideoDispatcher } from './videoDispatcher';

export class PageDispatcher extends Dispatcher<Page, channels.PageInitializer> implements channels.PageChannel {
private _page: Page;
Expand Down Expand Up @@ -66,7 +65,6 @@ export class PageDispatcher extends Dispatcher<Page, channels.PageInitializer> i
}));
page.on(Page.Events.RequestFinished, request => this._dispatchEvent('requestFinished', { request: RequestDispatcher.from(scope, request) }));
page.on(Page.Events.Response, response => this._dispatchEvent('response', { response: new ResponseDispatcher(this._scope, response) }));
page.on(Page.Events.VideoStarted, screencast => this._dispatchEvent('videoStarted', { video: new VideoDispatcher(this._scope, screencast) }));
page.on(Page.Events.Worker, worker => this._dispatchEvent('worker', { worker: new WorkerDispatcher(this._scope, worker) }));
}

Expand Down
47 changes: 0 additions & 47 deletions src/dispatchers/videoDispatcher.ts

This file was deleted.

Loading

0 comments on commit df77734

Please sign in to comment.