Skip to content

Commit af58c8a

Browse files
authored
fix(screencast): ensure that _videostarted is fired after newPage (microsoft#3807)
1 parent a5a5636 commit af58c8a

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

src/server/browser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { EventEmitter } from 'events';
2121
import { Download } from './download';
2222
import { ProxySettings } from './types';
2323
import { ChildProcess } from 'child_process';
24+
import { makeWaitForNextTask } from '../utils/utils';
2425

2526
export interface BrowserProcess {
2627
onclose: ((exitCode: number | null, signal: string | null) => void) | undefined;
@@ -87,10 +88,14 @@ export abstract class Browser extends EventEmitter {
8788
this._downloads.delete(uuid);
8889
}
8990

90-
_videoStarted(videoId: string, file: string): Video {
91+
_videoStarted(videoId: string, file: string, pageOrError: Promise<Page | Error>) {
9192
const video = new Video(file);
9293
this._idToVideo.set(videoId, video);
93-
return video;
94+
pageOrError.then(pageOrError => {
95+
// Emit the event in another task to ensure that newPage response is handled before.
96+
if (pageOrError instanceof Page)
97+
makeWaitForNextTask()(() => pageOrError.emit(Page.Events.VideoStarted, video));
98+
});
9499
}
95100

96101
_videoFinished(videoId: string) {

src/server/chromium/crPage.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,11 +762,7 @@ class FrameSession {
762762
this._screencastState = 'started';
763763
this._videoRecorder = videoRecorder;
764764
this._screencastId = screencastId;
765-
const video = this._crPage._browserContext._browser._videoStarted(screencastId, options.outputFile);
766-
this._crPage.pageOrError().then(pageOrError => {
767-
if (pageOrError instanceof Page)
768-
pageOrError.emit(Page.Events.VideoStarted, video);
769-
}).catch(() => {});
765+
this._crPage._browserContext._browser._videoStarted(screencastId, options.outputFile, this._crPage.pageOrError());
770766
} catch (e) {
771767
videoRecorder.stop().catch(() => {});
772768
throw e;

src/server/firefox/ffPage.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,7 @@ export class FFPage implements PageDelegate {
258258
}
259259

260260
_onScreencastStarted(event: Protocol.Page.screencastStartedPayload) {
261-
const video = this._browserContext._browser._videoStarted(event.screencastId, event.file);
262-
this.pageOrError().then(pageOrError => {
263-
if (pageOrError instanceof Page)
264-
pageOrError.emit(Page.Events.VideoStarted, video);
265-
}).catch(() => {});
261+
this._browserContext._browser._videoStarted(event.screencastId, event.file, this.pageOrError());
266262
}
267263

268264
async exposeBinding(binding: PageBinding) {

src/server/webkit/wkPage.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,7 @@ export class WKPage implements PageDelegate {
721721
width: options.width,
722722
height: options.height,
723723
}) as any;
724-
const video = this._browserContext._browser._videoStarted(screencastId, options.outputFile);
725-
this.pageOrError().then(pageOrError => {
726-
if (pageOrError instanceof Page)
727-
pageOrError.emit(Page.Events.VideoStarted, video);
728-
}).catch(() => {});
724+
this._browserContext._browser._videoStarted(screencastId, options.outputFile, this.pageOrError());
729725
} catch (e) {
730726
this._recordingVideoFile = null;
731727
throw e;

test/screencast.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ describe('screencast', suite => {
281281
await browser.close();
282282
});
283283

284+
it('should fire striclty after context.newPage', async ({browser, tmpDir}) => {
285+
const context = await browser.newContext({_recordVideos: {width: 320, height: 240}});
286+
const page = await context.newPage();
287+
// Should not hang.
288+
await page.waitForEvent('_videostarted');
289+
await context.close();
290+
});
291+
284292
it('should fire start event for popups', async ({browserType, tmpDir, server}) => {
285293
const browser = await browserType.launch({_videosPath: tmpDir});
286294
const context = await browser.newContext({_recordVideos: {width: 320, height: 240}});

0 commit comments

Comments
 (0)