Skip to content

Commit 86ffd58

Browse files
authored
test: add test for console log from content script (#1920)
This PR adds a test to prove extension content script can be captured in logs for pages
1 parent 57648b7 commit 86ffd58

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

tests/tools/extensions.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ import {afterEach, describe, it} from 'node:test';
1111
import sinon from 'sinon';
1212

1313
import type {ParsedArguments} from '../../src/bin/chrome-devtools-mcp-cli-options.js';
14+
import {listConsoleMessages} from '../../src/tools/console.js';
1415
import {
1516
installExtension,
1617
uninstallExtension,
1718
listExtensions,
1819
reloadExtension,
1920
triggerExtensionAction,
2021
} from '../../src/tools/extensions.js';
22+
import {serverHooks} from '../server.js';
2123
import {
2224
assertNoServiceWorkerReported,
2325
extractExtensionId,
2426
withMcpContext,
27+
html,
28+
getTextContent,
2529
} from '../utils.js';
2630

2731
const EXTENSION_WITH_SW_PATH = path.join(
@@ -32,8 +36,14 @@ const EXTENSION_PATH = path.join(
3236
import.meta.dirname,
3337
'../../../tests/tools/fixtures/extension',
3438
);
39+
const EXTENSION_CONTENT_SCRIPT_PATH = path.join(
40+
import.meta.dirname,
41+
'../../../tests/tools/fixtures/extension-content-script',
42+
);
3543

3644
describe('extension', () => {
45+
const server = serverHooks();
46+
3747
afterEach(() => {
3848
sinon.restore();
3949
});
@@ -168,4 +178,44 @@ describe('extension', () => {
168178
} as ParsedArguments,
169179
);
170180
});
181+
182+
it('verifies that content script console logs are received', async () => {
183+
await withMcpContext(
184+
async (response, context) => {
185+
server.addHtmlRoute(
186+
'/test-content-script',
187+
html`<h1>Test Content Script</h1>`,
188+
);
189+
const url = server.getRoute('/test-content-script');
190+
191+
const extensionId = await context.installExtension(
192+
EXTENSION_CONTENT_SCRIPT_PATH,
193+
);
194+
195+
const mcpPage = context.getSelectedMcpPage();
196+
const page = mcpPage.pptrPage;
197+
198+
await page.goto(url);
199+
200+
await listConsoleMessages.handler(
201+
{params: {includePreservedMessages: true}, page: mcpPage},
202+
response,
203+
context,
204+
);
205+
206+
const result = await response.handle('list_console_messages', context);
207+
const consoleOutput = getTextContent(result.content[0]);
208+
assert.ok(
209+
consoleOutput.includes('from content script!'),
210+
`Console output should contain message from content script. Got: ${consoleOutput}`,
211+
);
212+
213+
await context.uninstallExtension(extensionId);
214+
},
215+
{},
216+
{
217+
categoryExtensions: true,
218+
} as ParsedArguments,
219+
);
220+
});
171221
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Hello from content script!');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "Test Extension with Content Script",
4+
"version": "1.0",
5+
"permissions": ["tabs", "scripting"],
6+
"host_permissions": ["<all_urls>"],
7+
"content_scripts": [
8+
{
9+
"matches": ["<all_urls>"],
10+
"js": ["content.js"],
11+
"run_at": "document_start",
12+
"all_frames": true
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)