Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: revert api back to nullable video (#39933)

Dmitry Gozman5f74738c96f4
diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.md
index 7affcf3bf3..c06b6a58b6 100644
--- a/docs/src/api/class-page.md
+++ b/docs/src/api/class-page.md
@@ -4455,7 +4455,7 @@ Optional handler function to route the request.
## method: Page.video
* since: v1.8
-- returns: <[Video]>
+- returns: <[null]|[Video]>
Video object associated with this page. Can be used to access the video file when using the `recordVideo` context option.
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index 2f12814e21..5aec053f0a 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -4800,7 +4800,7 @@ export interface Page {
* Video object associated with this page. Can be used to access the video file when using the `recordVideo` context
* option.
*/
- video(): Video;
+ video(): null|Video;
viewportSize(): null|{
/**
diff --git a/packages/playwright-core/src/client/page.ts b/packages/playwright-core/src/client/page.ts
index 4123cc586e..392d982f12 100644
--- a/packages/playwright-core/src/client/page.ts
+++ b/packages/playwright-core/src/client/page.ts
@@ -281,7 +281,12 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
this._timeoutSettings.setDefaultTimeout(timeout);
}
- video(): Video {
+ video(): Video | null {
+ // Note: we are creating Video object lazily, because we do not know
+ // BrowserContextOptions when constructing the page - it is assigned
+ // too late during launchPersistentContext.
+ if (!this._browserContext._options.recordVideo)
+ return null;
return this._video;
}
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 2f12814e21..5aec053f0a 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -4800,7 +4800,7 @@ export interface Page {
* Video object associated with this page. Can be used to access the video file when using the `recordVideo` context
* option.
*/
- video(): Video;
+ video(): null|Video;
viewportSize(): null|{
/**
diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts
index 62b68d9d12..f79a96ae58 100644
--- a/packages/playwright/src/index.ts
+++ b/packages/playwright/src/index.ts
@@ -404,7 +404,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
const preserveVideo = captureVideo && (videoMode === 'on' || (testFailed && videoMode === 'retain-on-failure') || (videoMode === 'on-first-retry' && testInfo.retry === 1));
if (preserveVideo) {
const { pagesWithVideo: pagesForVideo } = contexts.get(context)!;
- const videos = pagesForVideo.map(p => p.video());
+ const videos = pagesForVideo.map(p => p.video()).filter(video => !!video);
await Promise.all(videos.map(async v => {
try {
const savedPath = testInfo.outputPath(`video${counter ? '-' + counter : ''}.webm`);
diff --git a/tests/library/video.spec.ts b/tests/library/video.spec.ts
index 9c854afdc4..0aea8f0990 100644
--- a/tests/library/video.spec.ts
+++ b/tests/library/video.spec.ts
@@ -164,6 +164,10 @@ it.describe('screencast', () => {
it.slow();
it.skip(({ mode }) => mode !== 'default', 'video.path() is not available in remote mode');
+ it('should not have video by default', async ({ page }) => {
+ expect(page.video()).toBeNull();
+ });
+
it('videoSize should require videosPath', async ({ browser }) => {
const error = await browser.newContext({ videoSize: { width: 100, height: 100 } }).catch(e => e);
expect(error.message).toContain('"videoSize" option requires "videosPath" to be specified');