Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/third_party/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type * from 'puppeteer-core';
export {PipeTransport} from 'puppeteer-core/internal/node/PipeTransport.js';
export {CdpFrame} from 'puppeteer-core/internal/cdp/Frame.js';
export {CdpPage} from 'puppeteer-core/internal/cdp/Page.js';
export {CdpExtension} from 'puppeteer-core/internal/cdp/Extension.js';
export type {CdpWebWorker} from 'puppeteer-core/internal/cdp/WebWorker.js';
export type {Realm} from 'puppeteer-core/internal/api/Realm.js';
export {FrameEvent} from 'puppeteer-core/internal/api/Frame.js';
Expand Down
109 changes: 109 additions & 0 deletions tests/McpContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ import {type HTTPResponse} from '../src/third_party/index.js';
import type {TraceResult} from '../src/processors/PerformanceTrace.js';
import {resolveCanonicalPath} from '../src/utils/files.js';

import {serverHooks} from './server.js';
import {
assertNoServiceWorkerReported,
getMockRequest,
html,
withBrowser,
withMcpContext,
stabilizeStructuredContent,
} from './utils.js';

const EXTENSION_WITH_SW_PATH = path.join(
import.meta.dirname,
'../../tests/tools/fixtures/extension-sw',
);
const EXTENSION_CONTENT_SCRIPT_PATH = path.join(
import.meta.dirname,
'../../tests/tools/fixtures/extension-content-script',
);

describe('McpContext', () => {
afterEach(() => {
sinon.restore();
Expand Down Expand Up @@ -909,4 +920,102 @@ describe('McpContext', () => {
});
});
});

describe('extensions', () => {
const server = serverHooks();

it('manages extension lifecycle (install, list, get, reload, triggerAction, and uninstall)', async () => {
await withMcpContext(
async (_response, context) => {
const extensionId = await context.installExtension(
EXTENSION_WITH_SW_PATH,
);

let extensions = await context.listExtensions();
assert.ok(
extensions.has(extensionId),
`Extension with ID "${extensionId}" should be installed`,
);

const extension = await context.getExtension(extensionId);
assert.ok(extension, 'Extension should be returned');
assert.strictEqual(extension.id, extensionId);
assert.strictEqual(extension.path, EXTENSION_WITH_SW_PATH);

await context.installExtension(extension.path);
extensions = await context.listExtensions();
assert.ok(
extensions.has(extensionId),
'Extension should still be installed after reload',
);

const targetsBefore = context.browser.targets();
const pageTargetBefore = targetsBefore.find(
t => t.type() === 'page' && t.url().includes(extensionId),
);
assert.ok(!pageTargetBefore, 'Page should not exist before action');

await context.triggerExtensionAction(extensionId);

const pageTargetAfter = await context.browser.waitForTarget(
t => t.type() === 'page' && t.url().includes(extensionId),
);
assert.ok(pageTargetAfter, 'Page should exist after action');

await context.uninstallExtension(extensionId);
extensions = await context.listExtensions();
assert.ok(
!extensions.has(extensionId),
`Extension with ID "${extensionId}" should NOT be installed`,
);

const targets = context.browser.targets();
assertNoServiceWorkerReported(targets, extensionId);
},
{},
{
categoryExtensions: true,
},
);
});

it('verifies that content script console logs are received', async () => {
await withMcpContext(
async (_response, context) => {
server.addHtmlRoute(
'/test-content-script',
html`<h1>Test Content Script</h1>`,
);
const url = server.getRoute('/test-content-script');

const extensionId = await context.installExtension(
EXTENSION_CONTENT_SCRIPT_PATH,
);

const mcpPage = context.getSelectedMcpPage();
const page = mcpPage.pptrPage;

await page.goto(url);

const messages = mcpPage.getConsoleData(true);
const hasContentScriptLog = messages.some(
message =>
'text' in message &&
typeof message.text === 'function' &&
message.text().includes('from content script!'),
);
assert.ok(
hasContentScriptLog,
'Console output should contain message from content script.',
);

await context.uninstallExtension(extensionId);
},
{},
{
categoryExtensions: true,
},
);
});
});
});
29 changes: 27 additions & 2 deletions tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ import sinon from 'sinon';
import {McpContext} from '../src/McpContext.js';
import {McpPage} from '../src/McpPage.js';
import {McpResponse} from '../src/McpResponse.js';
import {CdpFrame, CdpPage, DevTools} from '../src/third_party/index.js';
import type {Page} from '../src/third_party/index.js';
import {
CdpExtension,
CdpFrame,
CdpPage,
DevTools,
} from '../src/third_party/index.js';
import type {Extension, Page} from '../src/third_party/index.js';

export type MockMcpPage = sinon.SinonStubbedInstance<McpPage> & {
pptrPage: sinon.SinonStubbedInstance<Page>;
Expand Down Expand Up @@ -636,3 +641,23 @@ export function createMockCSSMatchedStyles(

return mock;
}

export function createMockExtension(
options: {
id?: string;
path?: string;
name?: string;
version?: string;
enabled?: boolean;
} = {},
): Extension {
const extension = sinon.createStubInstance(CdpExtension);
sinon.stub(extension, 'id').value(options.id ?? 'mock-extension-id');
sinon
.stub(extension, 'path')
.value(options.path ?? '/path/to/mock/extension');
sinon.stub(extension, 'name').value(options.name ?? 'Mock Extension');
sinon.stub(extension, 'version').value(options.version ?? '1.0.0');
sinon.stub(extension, 'enabled').value(options.enabled ?? true);
return extension as unknown as Extension;
}
Loading