Skip to content

Commit 432f03d

Browse files
mxschmittdgozman
andauthored
chore: throw pretty error if launchApp is launched using channel (microsoft#36881)
Signed-off-by: Max Schmitt <[email protected]> Co-authored-by: Dmitry Gozman <[email protected]>
1 parent d0e6b0d commit 432f03d

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

packages/playwright-core/src/server/launchApp.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import fs from 'fs';
1818
import path from 'path';
1919

20-
import { isUnderTest } from '../utils';
20+
import { isUnderTest, rewriteErrorMessage, wrapInASCIIBox } from '../utils';
2121
import { serverSideCallMetadata } from './instrumentation';
22-
import { findChromiumChannel } from './registry';
22+
import { buildPlaywrightCLICommand, findChromiumChannelBestEffort } from './registry';
2323
import { registryDirectory } from './registry';
2424
import { ProgressController } from './progress';
2525

@@ -46,19 +46,32 @@ export async function launchApp(browserType: BrowserType, options: {
4646
'--test-type=',
4747
);
4848
if (!channel && !options.persistentContextOptions?.executablePath)
49-
channel = findChromiumChannel(options.sdkLanguage);
49+
channel = findChromiumChannelBestEffort(options.sdkLanguage);
5050
}
5151

5252
const controller = new ProgressController(serverSideCallMetadata(), browserType);
53-
const context = await controller.run(progress => browserType.launchPersistentContext(progress, '', {
54-
ignoreDefaultArgs: ['--enable-automation'],
55-
...options?.persistentContextOptions,
56-
channel,
57-
noDefaultViewport: options.persistentContextOptions?.noDefaultViewport ?? true,
58-
acceptDownloads: options?.persistentContextOptions?.acceptDownloads ?? (isUnderTest() ? 'accept' : 'internal-browser-default'),
59-
colorScheme: options?.persistentContextOptions?.colorScheme ?? 'no-override',
60-
args,
61-
}), 0); // Deliberately no timeout for our apps.
53+
let context;
54+
try {
55+
context = await controller.run(progress => browserType.launchPersistentContext(progress, '', {
56+
ignoreDefaultArgs: ['--enable-automation'],
57+
...options?.persistentContextOptions,
58+
channel,
59+
noDefaultViewport: options.persistentContextOptions?.noDefaultViewport ?? true,
60+
acceptDownloads: options?.persistentContextOptions?.acceptDownloads ?? (isUnderTest() ? 'accept' : 'internal-browser-default'),
61+
colorScheme: options?.persistentContextOptions?.colorScheme ?? 'no-override',
62+
args,
63+
}), 0); // Deliberately no timeout for our apps.
64+
} catch (error) {
65+
if (channel) {
66+
error = rewriteErrorMessage(error, [
67+
`Failed to launch "${channel}" channel.`,
68+
'Using custom channels could lead to unexpected behavior due to Enterprise policies (chrome://policy).',
69+
'Install the default browser instead:',
70+
wrapInASCIIBox(`${buildPlaywrightCLICommand(options.sdkLanguage, 'install')}`, 2),
71+
].join('\n'));
72+
}
73+
throw error;
74+
}
6275
const [page] = context.pages();
6376
// Chromium on macOS opens a new tab when clicking on the dock icon.
6477
// See https://github.com/microsoft/playwright/issues/9434

packages/playwright-core/src/server/registry/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,8 @@ export async function installBrowsersForNpmInstall(browsers: string[]) {
13631363
await registry.install(executables, false /* forceReinstall */);
13641364
}
13651365

1366-
export function findChromiumChannel(sdkLanguage: string): string | undefined {
1366+
// for launchApp -> UI Mode / Trace Viewer
1367+
export function findChromiumChannelBestEffort(sdkLanguage: string): string | undefined {
13671368
// Fall back to the stable channels of popular vendors to work out of the box.
13681369
// Null means no installation and no channels found.
13691370
let channel = null;

0 commit comments

Comments
 (0)