Repository: GoogleChrome/lighthouse
Pull request #12632 (2021-06-08 19:00:24)

core(fr): fix main-document-content
Fixes https://github.com/GoogleChrome/lighthouse/issues/12631



Patch:
diff --git a/lighthouse-core/gather/driver/prepare.js b/lighthouse-core/gather/driver/prepare.js
index 7c8c4eba3..3cc1fc260 100644
--- a/lighthouse-core/gather/driver/prepare.js
+++ b/lighthouse-core/gather/driver/prepare.js
@@ -127,6 +127,9 @@ async function prepareNetworkForNavigation(session, settings, navigation) {
  * @param {LH.Config.Settings} settings
  */
 async function prepareTargetForNavigationMode(driver, settings) {
+  // Enable network domain here so future calls to `emulate()` don't clear cache (#12631)
+  await driver.defaultSession.sendCommand('Network.enable');
+
   // Emulate our target device screen and user agent.
   await emulation.emulate(driver.defaultSession, settings);
 
diff --git a/lighthouse-core/gather/fetcher.js b/lighthouse-core/gather/fetcher.js
index 51eaa023a..42bf7caa9 100644
--- a/lighthouse-core/gather/fetcher.js
+++ b/lighthouse-core/gather/fetcher.js
@@ -146,7 +146,6 @@ class Fetcher {
    * @return {Promise<{stream: LH.Crdp.IO.StreamHandle|null, status: number|null}>}
    */
   async _loadNetworkResource(url) {
-    await this.session.sendCommand('Network.enable');
     const frameTreeResponse = await this.session.sendCommand('Page.getFrameTree');
     const networkResponse = await this.session.sendCommand('Network.loadNetworkResource', {
       frameId: frameTreeResponse.frameTree.frame.id,
@@ -156,7 +155,6 @@ class Fetcher {
         includeCredentials: true,
       },
     });
-    await this.session.sendCommand('Network.disable');
 
     return {
       stream: networkResponse.resource.success ? (networkResponse.resource.stream || null) : null,
diff --git a/lighthouse-core/lib/emulation.js b/lighthouse-core/lib/emulation.js
index 798050849..e1a93d7c6 100644
--- a/lighthouse-core/lib/emulation.js
+++ b/lighthouse-core/lib/emulation.js
@@ -23,8 +23,6 @@ const NO_CPU_THROTTLE_METRICS = {
  */
 async function emulate(session, settings) {
   if (settings.emulatedUserAgent !== false) {
-    // Network.enable must be called for UA overriding to work
-    await session.sendCommand('Network.enable');
     await session.sendCommand('Network.setUserAgentOverride', {
       userAgent: /** @type {string} */ (settings.emulatedUserAgent),
     });


Test patch:
diff --git a/lighthouse-core/test/fraggle-rock/api-test-pptr.js b/lighthouse-core/test/fraggle-rock/api-test-pptr.js
index 7a6f08ba1..0b56eec3e 100644
--- a/lighthouse-core/test/fraggle-rock/api-test-pptr.js
+++ b/lighthouse-core/test/fraggle-rock/api-test-pptr.js
@@ -160,7 +160,7 @@ describe('Fraggle Rock API', () => {
       const {auditResults, failedAudits, erroredAudits} = getAuditsBreakdown(lhr);
       // TODO(FR-COMPAT): This assertion can be removed when full compatibility is reached.
       expect(auditResults.length).toMatchInlineSnapshot(`151`);
-      expect(erroredAudits).toHaveLength(1); // FIXME: MainDocumentContent broken from merge
+      expect(erroredAudits).toHaveLength(0);
 
       const failedAuditIds = failedAudits.map(audit => audit.id);
       expect(failedAuditIds).toContain('label');
diff --git a/lighthouse-core/test/gather/fetcher-test.js b/lighthouse-core/test/gather/fetcher-test.js
index 139201c39..a6482eff3 100644
--- a/lighthouse-core/test/gather/fetcher-test.js
+++ b/lighthouse-core/test/gather/fetcher-test.js
@@ -125,12 +125,10 @@ describe('._fetchResourceOverProtocol', () => {
 
   it('fetches a file', async () => {
     connectionStub.sendCommand = createMockSendCommandFn()
-      .mockResponse('Network.enable')
       .mockResponse('Page.getFrameTree', {frameTree: {frame: {id: 'FRAME'}}})
       .mockResponse('Network.loadNetworkResource', {
         resource: {success: true, httpStatusCode: 200, stream: '1'},
-      })
-      .mockResponse('Network.disable');
+      });
 
     const data = await fetcher._fetchResourceOverProtocol('https://example.com', {timeout: 500});
     expect(data).toEqual({content: streamContents, status: 200});
@@ -138,12 +136,10 @@ describe('._fetchResourceOverProtocol', () => {
 
   it('returns null when resource could not be fetched', async () => {
     connectionStub.sendCommand = createMockSendCommandFn()
-      .mockResponse('Network.enable')
       .mockResponse('Page.getFrameTree', {frameTree: {frame: {id: 'FRAME'}}})
       .mockResponse('Network.loadNetworkResource', {
         resource: {success: false, httpStatusCode: 404},
-      })
-      .mockResponse('Network.disable');
+      });
 
     const data = await fetcher._fetchResourceOverProtocol('https://example.com', {timeout: 500});
     expect(data).toEqual({content: null, status: 404});
@@ -151,12 +147,10 @@ describe('._fetchResourceOverProtocol', () => {
 
   it('throws on timeout', async () => {
     connectionStub.sendCommand = createMockSendCommandFn()
-      .mockResponse('Network.enable')
       .mockResponse('Page.getFrameTree', {frameTree: {frame: {id: 'FRAME'}}})
       .mockResponse('Network.loadNetworkResource', {
         resource: {success: false, httpStatusCode: 404},
-      }, 100)
-      .mockResponse('Network.disable');
+      }, 100);
 
     const dataPromise = fetcher._fetchResourceOverProtocol('https://example.com', {timeout: 50});
     await expect(dataPromise).rejects.toThrowError(/Timed out fetching resource/);
@@ -164,12 +158,10 @@ describe('._fetchResourceOverProtocol', () => {
 
   it('uses remaining time on _readIOStream', async () => {
     connectionStub.sendCommand = createMockSendCommandFn()
-      .mockResponse('Network.enable')
       .mockResponse('Page.getFrameTree', {frameTree: {frame: {id: 'FRAME'}}})
       .mockResponse('Network.loadNetworkResource', {
         resource: {success: true, httpStatusCode: 200, stream: '1'},
-      }, 500)
-      .mockResponse('Network.disable');
+      }, 500);
 
     let timeout;
     fetcher._readIOStream = jest.fn().mockImplementation((_, options) => {
diff --git a/lighthouse-core/test/lib/emulation-test.js b/lighthouse-core/test/lib/emulation-test.js
index 0aef39d85..8b6096bf8 100644
--- a/lighthouse-core/test/lib/emulation-test.js
+++ b/lighthouse-core/test/lib/emulation-test.js
@@ -26,7 +26,6 @@ describe('emulation', () => {
       driver = new Driver(connectionStub);
 
       connectionStub.sendCommand = createMockSendCommandFn()
-        .mockResponse('Network.enable')
         .mockResponse('Network.setUserAgentOverride')
         .mockResponse('Emulation.setDeviceMetricsOverride')
         .mockResponse('Emulation.setTouchEmulationEnabled');