Chrome DevTools for coding agents
fix(lighthouse): restore the emulated viewport after a navigation audit (#3189)
Musa Toktasc777ece9a8be
diff --git a/src/McpPage.ts b/src/McpPage.tsindex 46375fb1..086de9b0 100644--- a/src/McpPage.ts+++ b/src/McpPage.ts@@ -1115,6 +1115,19 @@ export class McpPage implements ContextPage {async restoreEmulation(): Promise<void> {const currentSetting = this.emulationSettings;+ const {viewport} = currentSetting;+ if (viewport) {+ // Lighthouse's CDP session replaces the device metrics override and+ // clears it on detach, and Chrome ignores an override identical to the+ // last one this session sent. A different width first makes the real+ // call take effect without a reload (`isMobile` and `hasTouch` stay the+ // same). This also runs after snapshot audits and fires a resize event,+ // so do not remove it as redundant.+ await this.pptrPage.setViewport({+ ...viewport,+ width: Math.max(1, viewport.width - 1),+ });+ }await this.emulate(currentSetting);}diff --git a/tests/McpPage.test.ts b/tests/McpPage.test.tsindex 5220948a..0f23d436 100644--- a/tests/McpPage.test.ts+++ b/tests/McpPage.test.ts@@ -777,8 +777,18 @@ describe('McpPage', () => {await mcpPage.restoreEmulation();- sinon.assert.calledTwice(pptrPage.setViewport);+ // A different width first, because Chrome ignores an override that is+ // identical to the last one of this session (see restoreEmulation).+ sinon.assert.calledThrice(pptrPage.setViewport);sinon.assert.calledWithExactly(pptrPage.setViewport.secondCall, {+ width: 399,+ height: 400,+ deviceScaleFactor: 1,+ isMobile: false,+ hasTouch: false,+ isLandscape: false,+ });+ sinon.assert.calledWithExactly(pptrPage.setViewport.thirdCall, {width: 400,height: 400,deviceScaleFactor: 1,@@ -788,6 +798,16 @@ describe('McpPage', () => {});});+ it('does not send a scratch viewport when none was configured', async () => {+ const {mcpPage, pptrPage} = await createMcpPage();+ await mcpPage.emulate({cpuThrottlingRate: 4});+ pptrPage.setViewport.resetHistory();++ await mcpPage.restoreEmulation();++ sinon.assert.calledOnceWithExactly(pptrPage.setViewport, null);+ });+it('re-applies previously configured network and cpu throttling emulation', async () => {const {mcpPage, pptrPage} = await createMcpPage();await mcpPage.emulate({diff --git a/tests/tools/lighthouse.test.ts b/tests/tools/lighthouse.test.tsindex 376deee9..ec0348d9 100644--- a/tests/tools/lighthouse.test.ts+++ b/tests/tools/lighthouse.test.ts@@ -28,8 +28,32 @@ describe('lighthouse', () => {server.addHtmlRoute('/test', html`<div>Test</div>`);await withMcpContext(async (response, context, args) => {- const page = context.getSelectedMcpPage().pptrPage;+ const mcpPage = context.getSelectedMcpPage();+ const page = mcpPage.pptrPage;await page.goto(server.getRoute('/test'));+ await mcpPage.emulate({+ viewport: {+ width: 390,+ height: 844,+ deviceScaleFactor: 3,+ isMobile: true,+ hasTouch: true,+ isLandscape: false,+ },+ });+ const readViewport = () =>+ page.evaluate(() => ({+ width: window.innerWidth,+ height: window.innerHeight,+ deviceScaleFactor: window.devicePixelRatio,+ screenWidth: screen.width,+ orientation: screen.orientation.type,+ maxTouchPoints: navigator.maxTouchPoints,+ }));+ const emulated = await readViewport();+ assert.equal(emulated.width, 390);+ assert.equal(emulated.deviceScaleFactor, 3);+ assert.ok(emulated.maxTouchPoints > 0);await lighthouseAudit(args).handler({@@ -37,12 +61,16 @@ describe('lighthouse', () => {mode: 'navigation',device: 'desktop',},- page: context.getSelectedMcpPage(),+ page: mcpPage,},response,context,);+ // Lighthouse replaces and then clears the device metrics override, the+ // emulated viewport must be back afterwards.+ assert.deepEqual(await readViewport(), emulated);+const data = response.attachedLighthouseResult;assert.ok(data);