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
50 changes: 50 additions & 0 deletions tests/tools/extensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import {afterEach, describe, it} from 'node:test';
import sinon from 'sinon';

import type {ParsedArguments} from '../../src/bin/chrome-devtools-mcp-cli-options.js';
import {listConsoleMessages} from '../../src/tools/console.js';
import {
installExtension,
uninstallExtension,
listExtensions,
reloadExtension,
triggerExtensionAction,
} from '../../src/tools/extensions.js';
import {serverHooks} from '../server.js';
import {
assertNoServiceWorkerReported,
extractExtensionId,
withMcpContext,
html,
getTextContent,
} from '../utils.js';

const EXTENSION_WITH_SW_PATH = path.join(
Expand All @@ -32,8 +36,14 @@ const EXTENSION_PATH = path.join(
import.meta.dirname,
'../../../tests/tools/fixtures/extension',
);
const EXTENSION_CONTENT_SCRIPT_PATH = path.join(
import.meta.dirname,
'../../../tests/tools/fixtures/extension-content-script',
);

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

afterEach(() => {
sinon.restore();
});
Expand Down Expand Up @@ -168,4 +178,44 @@ describe('extension', () => {
} as ParsedArguments,
);
});

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);

await listConsoleMessages.handler(
{params: {includePreservedMessages: true}, page: mcpPage},
response,
context,
);

const result = await response.handle('list_console_messages', context);
const consoleOutput = getTextContent(result.content[0]);
assert.ok(
consoleOutput.includes('from content script!'),
`Console output should contain message from content script. Got: ${consoleOutput}`,
);

await context.uninstallExtension(extensionId);
},
{},
{
categoryExtensions: true,
} as ParsedArguments,
);
});
});
1 change: 1 addition & 0 deletions tests/tools/fixtures/extension-content-script/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello from content script!');
15 changes: 15 additions & 0 deletions tests/tools/fixtures/extension-content-script/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"manifest_version": 3,
"name": "Test Extension with Content Script",
"version": "1.0",
"permissions": ["tabs", "scripting"],
"host_permissions": ["<all_urls>"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_start",
"all_frames": true
}
]
}
Loading