Skip to content

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

Revert "feat(dashboard): save screenshot/recording to Downloads in app mode" (#40279)

Pavel Feldman273aeb5bafaf
diff --git a/packages/dashboard/src/dashboard.tsx b/packages/dashboard/src/dashboard.tsx
index ae6e233c92..214f7e35b3 100644
--- a/packages/dashboard/src/dashboard.tsx
+++ b/packages/dashboard/src/dashboard.tsx
@@ -52,26 +52,9 @@ export const Dashboard: React.FC = () => {
const [frame, setFrame] = React.useState<DashboardChannelEvents['frame']>();
const [picking, setPicking] = React.useState(false);
const [recording, setRecording] = React.useState(false);
- const [screenshotIcon, setScreenshotIcon] = React.useState<'device-camera' | 'check'>('device-camera');
+ const [screenshotIcon, setScreenshotIcon] = React.useState<'device-camera' | 'clippy'>('device-camera');
const [flashTick, setFlashTick] = React.useState(0);
- const downloadArtifact = React.useCallback(async (id: string) => {
- const appMode = window.matchMedia('(display-mode: standalone)').matches
- || (window as any).__dashboardAppModeForTest === true;
- if (appMode)
- return await client!.saveAndReveal({ id });
-
- const a = document.createElement('a');
- a.href = `/artifact/${encodeURIComponent(id)}`;
- a.style.display = 'none';
- document.body.appendChild(a);
- try {
- a.click();
- } finally {
- a.remove();
- }
- }, [client]);
-
const displayRef = React.useRef<HTMLImageElement>(null);
const screenRef = React.useRef<HTMLDivElement>(null);
const toolbarRef = React.useRef<HTMLDivElement>(null);
@@ -329,9 +312,9 @@ export const Dashboard: React.FC = () => {
if (!client)
return;
if (recording) {
- const { id } = await client.stopRecording();
+ const { path } = await client.stopRecording();
+ await client.reveal({ path });
setRecording(false);
- await downloadArtifact(id);
} else {
await client.startRecording();
setRecording(true);
@@ -341,15 +324,16 @@ export const Dashboard: React.FC = () => {
</ToolbarButton>
<ToolbarButton
className='screenshot'
- title='Take screenshot'
+ title='Copy screenshot to clipboard'
icon={screenshotIcon}
disabled={!ready}
onClick={async () => {
if (!client)
return;
- const { id } = await client.screenshot();
- await downloadArtifact(id);
- setScreenshotIcon('check');
+ const screenshot = await client.screenshot();
+ const blob = await (await fetch('data:image/png;base64,' + screenshot)).blob();
+ await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]);
+ setScreenshotIcon('clippy');
setTimeout(() => setScreenshotIcon('device-camera'), 3000);
}}
/>
diff --git a/packages/dashboard/src/dashboardChannel.ts b/packages/dashboard/src/dashboardChannel.ts
index 54cd26b8bb..3f82ec25ca 100644
--- a/packages/dashboard/src/dashboardChannel.ts
+++ b/packages/dashboard/src/dashboardChannel.ts
@@ -45,7 +45,7 @@ export interface DashboardChannel {
closeSession(params: { browser: string }): Promise<void>;
deleteSessionData(params: { browser: string }): Promise<void>;
setVisible(params: { visible: boolean }): Promise<void>;
- saveAndReveal(params: { id: string }): Promise<void>;
+ reveal(params: { path: string }): Promise<void>;
navigate(params: { url: string }): Promise<void>;
back(): Promise<void>;
@@ -60,8 +60,8 @@ export interface DashboardChannel {
pickLocator(): Promise<void>;
cancelPickLocator(): Promise<void>;
startRecording(): Promise<void>;
- stopRecording(): Promise<{ id: string }>;
- screenshot(): Promise<{ id: string }>;
+ stopRecording(): Promise<{ path: string }>;
+ screenshot(): Promise<string>;
on<K extends keyof DashboardChannelEvents>(event: K, listener: (params: DashboardChannelEvents[K]) => void): void;
off<K extends keyof DashboardChannelEvents>(event: K, listener: (params: DashboardChannelEvents[K]) => void): void;
diff --git a/packages/playwright-core/src/tools/dashboard/dashboardApp.ts b/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
index 3fa98c7ba1..89a918d122 100644
--- a/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
+++ b/packages/playwright-core/src/tools/dashboard/dashboardApp.ts
@@ -35,7 +35,6 @@ type RevealOptions = { sessionName?: string; workspaceDir?: string };
async function startDashboardServer(options: { port?: number; host?: string; reveal?: RevealOptions } = {}): Promise<{ url: string; reveal: (options: RevealOptions) => void }> {
const httpServer = new HttpServer();
const dashboardDir = libPath('vite', 'dashboard');
- const artifacts = new Map<string, string>();
const connections = new Set<DashboardConnection>();
let currentReveal: RevealOptions = options.reveal ?? {};
@@ -43,28 +42,13 @@ async function startDashboardServer(options: { port?: number; host?: string; rev
httpServer.createWebSocket(() => {
let connection: DashboardConnection;
// eslint-disable-next-line prefer-const
- connection = new DashboardConnection(() => connections.delete(connection), artifacts);
+ connection = new DashboardConnection(() => connections.delete(connection));
if (currentReveal.sessionName)
connection.revealSession(currentReveal.sessionName, currentReveal.workspaceDir);
connections.add(connection);
return connection;
}, 'ws');
- httpServer.routePrefix('/artifact/', (request: http.IncomingMessage, response: http.ServerResponse) => {
- const pathname = new URL(request.url!, `http://${request.headers.host}`).pathname;
- const id = decodeURIComponent(pathname.substring('/artifact/'.length));
- const artifactPath = artifacts.get(id);
- if (!artifactPath) {
- response.statusCode = 404;
- response.end();
- return true;
- }
- // we're not deleting the artifact on purpose, so that the user can restart the download from the omnibox
- return httpServer.serveFile(request, response, artifactPath, {
- 'Content-Disposition': `attachment; filename="${path.basename(artifactPath)}"`,
- });
- });
-
httpServer.routePrefix('/', (request: http.IncomingMessage, response: http.ServerResponse) => {
const pathname = new URL(request.url!, `http://${request.headers.host}`).pathname;
const filePath = pathname === '/' ? 'index.html' : pathname.substring(1);
diff --git a/packages/playwright-core/src/tools/dashboard/dashboardController.ts b/packages/playwright-core/src/tools/dashboard/dashboardController.ts
index 0a5740ab5e..b13c2173b0 100644
--- a/packages/playwright-core/src/tools/dashboard/dashboardController.ts
+++ b/packages/playwright-core/src/tools/dashboard/dashboardController.ts
@@ -17,13 +17,11 @@
import path from 'path';
import os from 'os';
import fs from 'fs';
-import crypto from 'crypto';
import { execFile } from 'child_process';
import { eventsHelper } from '@utils/eventsHelper';
import { connectToBrowserAcrossVersions } from '../utils/connect';
import { serverRegistry } from '../../serverRegistry';
import { createClientInfo } from '../cli-client/registry';
-import { resolveDashboardDownloadsDir } from './dashboardDownloads';
import type * as api from '../../..';
import type { Transport } from '@utils/httpServer';
@@ -53,13 +51,11 @@ export class DashboardConnection implements Transport {
private _visible = true;
private _pendingReveal: { sessionName: string; workspaceDir?: string } | undefined;
- _artifactDir: string;
- _artifacts: Map<string, string>;
+ _recordingDir: string;
- constructor(onclose: () => void, artifacts: Map<string, string>) {
+ constructor(onclose: () => void) {
this._onclose = onclose;
- this._artifactDir = fs.mkdtempSync(path.join(os.tmpdir(), 'playwright-dashboard-'));
- this._artifacts = artifacts;
+ this._recordingDir = fs.mkdtempSync(path.join(os.tmpdir(), 'playwright-recordings-'));
}
onconnect() {
@@ -163,21 +159,18 @@ export class DashboardConnection implements Transport {
this._pushTabs();
}
- async saveAndReveal(params: { id: string }) {
- const source = this._artifacts.get(params.id);
- if (!source)
- throw new Error(`Unknown artifact id: ${params.id}`);
- this._artifacts.delete(params.id);
- const destination = path.join(resolveDashboardDownloadsDir(), path.basename(source));
- // Rename avoids copying bytes when source and destination are on the same
- // filesystem. On Linux, tmpdir and Downloads may be on different devices —
- // rename fails with EXDEV, so fall back to copy + unlink.
- await fs.promises.rename(source, destination).catch(async () => {
- await fs.promises.copyFile(source, destination);
- await fs.promises.unlink(source).catch(() => {});
- });
- if (!process.env.PLAYWRIGHT_DASHBOARD_DOWNLOADS_DIR_FOR_TEST)
- revealInFinder(destination);
+ async reveal(params: { path: string }) {
+ switch (os.platform()) {
+ case 'darwin':
+ execFile('open', ['-R', params.path]);
+ break;
+ case 'win32':
+ execFile('explorer', ['/select,', params.path]);
+ break;
+ case 'linux':
+ execFile('xdg-open', [path.dirname(params.path)]);
+ break;
+ }
}
visible(): boolean {
@@ -364,6 +357,7 @@ class AttachedBrowser {
get browserGuid(): string { return this._slot.guid; }
get contextGuid(): string { return this._slot.contextGuid; }
private get _context(): api.BrowserContext { return this._slot.context; }
+ private get _descriptor(): BrowserDescriptor { return this._slot.descriptor; }
async init() {
this._contextListeners.push(
@@ -482,32 +476,28 @@ class AttachedBrowser {
const page = this._selectedPage;
if (!page)
return;
- this._recordingPath = path.join(this._owner._artifactDir, `playwright-recording-${Date.now()}.webm`);
+ const artifactsDir = this._descriptor.browser.launchOptions.artifactsDir ?? this._owner._recordingDir;
+ this._recordingPath = path.join(artifactsDir, `recording-${Date.now()}.webm`);
if (this._screencastRunning)
await this._restartScreencast(page);
}
- async stopRecording(): Promise<{ id: string }> {
+ async stopRecording(): Promise<{ path: string }> {
const p = this._recordingPath;
if (!p)
throw new Error('No recording in progress');
this._recordingPath = null;
if (this._selectedPage && this._screencastRunning)
await this._restartScreencast(this._selectedPage);
- const id = crypto.randomUUID();
- this._owner._artifacts.set(id, p);
- return { id };
+ return { path: p };
}
- async screenshot(): Promise<{ id: string }> {
+ async screenshot(): Promise<string> {
const page = this._selectedPage;
if (!page)
throw new Error('No page selected');
- const absolutePath = path.join(this._owner._artifactDir, `playwright-screenshot-${Date.now()}.png`);
- await page.screenshot({ type: 'png', path: absolutePath });
- const id = crypto.randomUUID();
- this._owner._artifacts.set(id, absolutePath);
- return { id };
+ const buffer = await page.screenshot({ type: 'png' });
+ return buffer.toString('base64');
}
private async _selectPage(page: api.Page) {
@@ -578,20 +568,6 @@ function pageId(p: api.Page): string {
return (p as any)._guid;
}
-function revealInFinder(target: string) {
- switch (os.platform()) {
- case 'darwin':
- execFile('open', ['-R', target]);
- break;
- case 'win32':
- execFile('explorer', ['/select,', target]);
- break;
- case 'linux':
- execFile('xdg-open', [path.dirname(target)]);
- break;
- }
-}
-
async function faviconUrl(page: api.Page): Promise<string | undefined> {
const url = page.evaluate(async () => {
const response = await fetch(document.querySelector<HTMLLinkElement>('link[rel~="icon"]')?.href ?? '/favicon.ico');
diff --git a/packages/playwright-core/src/tools/dashboard/dashboardDownloads.ts b/packages/playwright-core/src/tools/dashboard/dashboardDownloads.ts
deleted file mode 100644
index 15d7aee63e..0000000000
--- a/packages/playwright-core/src/tools/dashboard/dashboardDownloads.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-/**
- * Copyright (c) Microsoft Corporation.
- *
- * 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.
- */
-
-import fs from 'fs';
-import os from 'os';
-import path from 'path';
-
-// Returns the directory the dashboard should drop screenshots and recordings
-// into when running in "app" mode. We use ~/Downloads, which is the physical
-// on-disk path on macOS and Windows (both display a localized name via
-// Finder/Explorer) and on English-locale Linux. Non-English Linux locales
-// localize the directory (~/Téléchargements, ~/下载, ...) via XDG's
-// user-dirs.dirs; we don't parse that file and fall back to a tmpdir subdir
-// there. If that becomes a pain point, read ~/.config/user-dirs.dirs.
-export function resolveDashboardDownloadsDir(): string {
- const override = process.env.PLAYWRIGHT_DASHBOARD_DOWNLOADS_DIR_FOR_TEST;
- if (override) {
- fs.mkdirSync(override, { recursive: true });
- return override;
- }
- const primary = path.join(os.homedir(), 'Downloads');
- if (fs.existsSync(primary))
- return primary;
- const fallback = path.join(os.tmpdir(), 'playwright-dashboard-downloads');
- fs.mkdirSync(fallback, { recursive: true });
- return fallback;
-}
diff --git a/tests/mcp/cli-fixtures.ts b/tests/mcp/cli-fixtures.ts
index ecb1abd9e9..673705307d 100644
--- a/tests/mcp/cli-fixtures.ts
+++ b/tests/mcp/cli-fixtures.ts
@@ -28,7 +28,6 @@ export { expect } from './fixtures';
export const test = baseTest.extend<{
cliEnv: Record<string, string>,
openDashboard: (options?: { cwd?: string, session?: string }) => Promise<Page>,
- openDashboardApp: (options?: { cwd?: string, session?: string }) => Promise<Page>,
cli: (...args: any[]) => Promise<{
output: string,
error: string,
@@ -56,35 +55,6 @@ export const test = baseTest.extend<{
return page;
});
},
- openDashboardApp: async ({ cli, playwright, findFreePort, waitForPort }, use) => {
- const dashboards: { dashboard: Page, browser: import('playwright-core').Browser }[] = [];
- await use(async (options?: { cwd?: string, session?: string }) => {
- const debugPort = await findFreePort();
- const showArgs = options?.session ? [`-s=${options.session}`, 'show'] : ['show'];
- await cli(...showArgs, {
- cwd: options?.cwd,
- env: { PLAYWRIGHT_DASHBOARD_DEBUG_PORT: String(debugPort) },
- });
- await waitForPort(debugPort);
- const browser = await playwright.chromium.connectOverCDP(`http://127.0.0.1:${debugPort}`);
- const dashboard = browser.contexts()[0].pages()[0];
- // Headless Chromium with `--app=` does not report display-mode: standalone,
- // and CDP's Emulation.setEmulatedMedia doesn't honor display-mode features.
- // Set the test flag directly; downloadArtifact re-reads it on every click.
- await dashboard.addInitScript(() => { (window as any).__dashboardAppModeForTest = true; });
- await dashboard.evaluate(() => { (window as any).__dashboardAppModeForTest = true; }).catch(() => {});
- dashboards.push({ dashboard, browser });
- return dashboard;
- });
- for (const { dashboard, browser } of dashboards) {
- if (!browser.isConnected())
- continue;
- await Promise.all([
- new Promise(r => browser.on('disconnected', r)),
- dashboard.close(),
- ]).catch(e => console.error('Error during dashboard close', e));
- }
- },
cli: async ({ mcpBrowser, mcpHeadless, childProcess }, use) => {
const sessions: { name: string, pid: number }[] = [];
await fs.promises.mkdir(test.info().outputPath('.playwright'), { recursive: true });
@@ -114,7 +84,6 @@ function cliEnv() {
return {
PLAYWRIGHT_SERVER_REGISTRY: test.info().outputPath('registry'),
PLAYWRIGHT_DASHBOARD_SETTINGS_FILE_FOR_TEST: test.info().outputPath('dashboard.settings.json'),
- PLAYWRIGHT_DASHBOARD_DOWNLOADS_DIR_FOR_TEST: test.info().outputPath('dashboard-downloads'),
PLAYWRIGHT_DAEMON_SESSION_DIR: test.info().outputPath('daemon'),
PLAYWRIGHT_SOCKETS_DIR: path.join(test.info().project.outputDir, 'ds', String(test.info().parallelIndex)),
PLAYWRIGHT_CLI_CHANNEL_SCAN_DISABLED_FOR_TEST: '1',
diff --git a/tests/mcp/dashboard.spec.ts b/tests/mcp/dashboard.spec.ts
index a5eb4f9373..be1dc2f985 100644
--- a/tests/mcp/dashboard.spec.ts
+++ b/tests/mcp/dashboard.spec.ts
@@ -131,56 +131,3 @@ test('should pick locator from browser', async ({ cli, server, openDashboard })
const { output } = await pickPromise;
expect(output).toContain(`getByRole('button', { name: 'Submit' })`);
});
-
-test('screenshot triggers a browser download in browser mode', async ({ cli, server, openDashboard }) => {
- await cli('open', server.EMPTY_PAGE);
-
- const dashboard = await openDashboard();
- await dashboard.locator('.sidebar-tab').first().click();
- await expect(dashboard.locator('img#display')).toBeVisible();
- await expect(dashboard.locator('.screenshot')).toBeEnabled();
-
- const [download] = await Promise.all([
- dashboard.waitForEvent('download'),
- dashboard.locator('.screenshot').click(),
- ]);
- expect(download.suggestedFilename()).toMatch(/^playwright-screenshot-\d+\.png$/);
-});
-
-test('recording triggers a browser download in browser mode', async ({ cli, server, openDashboard }) => {
- await cli('open', server.EMPTY_PAGE);
-
- const dashboard = await openDashboard();
- await dashboard.locator('.sidebar-tab').first().click();
- await expect(dashboard.locator('img#display')).toBeVisible();
-
- const recordBtn = dashboard.locator('.recording');
- await expect(recordBtn).toBeEnabled();
- await recordBtn.click();
- await expect(dashboard.locator('.recording-label')).toBeVisible();
-
- const [download] = await Promise.all([
- dashboard.waitForEvent('download'),
- recordBtn.click(),
- ]);
- expect(download.suggestedFilename()).toMatch(/^playwright-recording-\d+\.webm$/);
-});
-
-test('screenshot lands in the Downloads dir in app mode', async ({ cli, server, openDashboardApp }, testInfo) => {
- const downloadsDir = testInfo.outputPath('dashboard-downloads');
- await fs.promises.mkdir(downloadsDir, { recursive: true });
-
- await cli('open', server.EMPTY_PAGE);
-
- const dashboard = await openDashboardApp();
- await dashboard.locator('.sidebar-tab').first().click();
- await expect(dashboard.locator('img#display')).toBeVisible();
- await expect(dashboard.locator('.screenshot')).toBeEnabled();
-
- await dashboard.locator('.screenshot').click();
-
- await expect.poll(async () => {
- const entries = await fs.promises.readdir(downloadsDir).catch(() => []);
- return entries.filter(f => /^playwright-screenshot-\d+\.png$/.test(f));
- }).toHaveLength(1);
-});