Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(extension): do not treat orphaned preferences entries as an installed extension (#42224)
Yury Semikhatskyc27d0621f163
diff --git a/packages/playwright-core/src/tools/utils/extension.ts b/packages/playwright-core/src/tools/utils/extension.tsindex 94a459766c..9e2ed64d6a 100644--- a/packages/playwright-core/src/tools/utils/extension.ts+++ b/packages/playwright-core/src/tools/utils/extension.ts@@ -66,16 +66,28 @@ export async function isPlaywrightExtensionInstalled(userDataDir: string): Promi}async function isExtensionInstalledInProfile(profileDir: string): Promise<boolean> {- // Covers two install shapes: web store drops the extension into <profile>/Extensions/<id>;- // `--load-extension` does not, and only shows up as the id inside <profile>/Preferences.+ // Web store installs unpack into <profile>/Extensions/<id>; `--load-extension` only+ // leaves a settings record in the preferences.if (await pathExists(path.join(profileDir, 'Extensions', playwrightExtensionId)))return true;+ // `extensions.settings` lives in Preferences or Secure Preferences depending on the platform.+ for (const fileName of ['Preferences', 'Secure Preferences']) {+ if (await hasExtensionSettingsRecord(path.join(profileDir, fileName)))+ return true;+ }+ return false;+}++async function hasExtensionSettingsRecord(prefsPath: string): Promise<boolean> {+ let prefs: any;try {- const prefs = await fs.promises.readFile(path.join(profileDir, 'Preferences'), 'utf-8');- return prefs.includes(`"${playwrightExtensionId}"`);+ prefs = JSON.parse(await fs.promises.readFile(prefsPath, 'utf-8'));} catch {return false;}+ // Uninstalling leaves an orphaned empty settings record behind, so require a populated one.+ const record = prefs?.extensions?.settings?.[playwrightExtensionId];+ return !!record && typeof record === 'object' && Object.keys(record).length > 0;}async function pathExists(p: string): Promise<boolean> {diff --git a/tests/extension/extension.spec.ts b/tests/extension/extension.spec.tsindex ded650508a..d0145b7a1d 100644--- a/tests/extension/extension.spec.ts+++ b/tests/extension/extension.spec.ts@@ -307,6 +307,42 @@ test(`launches the profile that has the extension`, {}).toPass();});+test(`ignores orphaned preferences entries of an uninstalled extension`, {+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright-mcp/issues/1712' },+}, async ({ startClient, server }, testInfo) => {+ // Default has orphaned entries of an uninstalled extension; they must not win over the+ // actual installation in Profile 1.+ const userDataDir = testInfo.outputPath('multi-profile');+ await fs.mkdir(path.join(userDataDir, 'Default'), { recursive: true });+ await fs.writeFile(path.join(userDataDir, 'Default', 'Preferences'), JSON.stringify({+ extensions: { settings: { [extensionId]: {} } },+ protection: { macs: { extensions: { settings: { [extensionId]: 'DEADBEEF' } } } },+ updateclientdata: { apps: { [extensionId]: { pv: '0.3.0' } } },+ }));+ await fs.mkdir(path.join(userDataDir, 'Profile 1'), { recursive: true });+ await fs.writeFile(path.join(userDataDir, 'Profile 1', 'Preferences'), JSON.stringify({+ extensions: { settings: { [extensionId]: { path: '/tmp/extension', location: 4 } } },+ }));+ // Make the profile with the orphaned entries the preferred, last used one.+ await fs.writeFile(path.join(userDataDir, 'Local State'), JSON.stringify({+ profile: { last_used: 'Default' },+ }));++ const executablePath = testInfo.outputPath('echo.sh');+ await fs.writeFile(executablePath, '#!/bin/bash\necho "Custom exec args: $@" > "$(dirname "$0")/output.txt"', { mode: 0o755 });++ const { client } = await startClient({+ args: [`--extension`, `--executable-path=${executablePath}`],+ env: { PWTEST_EXTENSION_USER_DATA_DIR: userDataDir },+ });++ client.callTool({ name: 'browser_navigate', arguments: { url: server.HELLO_WORLD } }).catch(() => {});+ await expect(async () => {+ const output = await fs.readFile(testInfo.outputPath('output.txt'), 'utf8');+ expect(output).toContain(`--profile-directory=Profile 1`);+ }).toPass();+});+test(`fails when extension is missing in custom userDataDir`, async ({ startClient, server }) => {const userDataDir = test.info().outputPath('empty-profile');