Skip to content

Commit

Permalink
faster test
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Mar 8, 2024
1 parent 74e8015 commit 468c7f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
25 changes: 16 additions & 9 deletions core/gather/gatherers/full-page-screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ class FullPageScreenshot extends BaseGatherer {
supportedModes: ['snapshot', 'timespan', 'navigation'],
};

/**
* @param {LH.Gatherer.Context} context
*/
waitForNetworkIdle(context) {
const session = context.driver.defaultSession;
const networkMonitor = context.driver.networkMonitor;
return waitForNetworkIdle(session, networkMonitor, {
pretendDCLAlreadyFired: true,
networkQuietThresholdMs: 1000,
busyEvent: 'network-critical-busy',
idleEvent: 'network-critical-idle',
isIdle: recorder => recorder.isCriticalIdle(),
});
}

/**
* @param {LH.Gatherer.Context} context
* @param {{height: number, width: number, mobile: boolean}} deviceMetrics
Expand All @@ -75,15 +90,7 @@ class FullPageScreenshot extends BaseGatherer {
);
const height = Math.min(fullHeight, MAX_WEBP_SIZE);

// Setup network monitor before we change the viewport.
const networkMonitor = context.driver.networkMonitor;
const waitForNetworkIdleResult = waitForNetworkIdle(session, networkMonitor, {
pretendDCLAlreadyFired: true,
networkQuietThresholdMs: 1000,
busyEvent: 'network-critical-busy',
idleEvent: 'network-critical-idle',
isIdle: recorder => recorder.isCriticalIdle(),
});
const waitForNetworkIdleResult = this.waitForNetworkIdle(context);

await session.sendCommand('Emulation.setDeviceMetricsOverride', {
mobile: deviceMetrics.mobile,
Expand Down
15 changes: 10 additions & 5 deletions core/test/gather/gatherers/full-page-screenshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {createMockContext} from '../../gather/mock-driver.js';
import FullPageScreenshotGatherer from '../../../gather/gatherers/full-page-screenshot.js';
import {fnAny} from '../../test-utils.js';

/** @type {{width: number, height: number}} */
let contentSize;
Expand All @@ -15,8 +16,17 @@ let screenshotSize;
/** @type {string[]} */
let screenshotData;
let mockContext = createMockContext();
let fpsGatherer = new FullPageScreenshotGatherer();

beforeEach(() => {
fpsGatherer = new FullPageScreenshotGatherer();

// Prevent `waitForNetworkIdle` from stalling the tests
fpsGatherer.waitForNetworkIdle = fnAny().mockImplementation(() => ({
promise: Promise.resolve(),
cancel: fnAny(),
}));

contentSize = {width: 100, height: 100};
screenSize = {width: 100, height: 100, dpr: 1};
screenshotSize = contentSize;
Expand Down Expand Up @@ -63,7 +73,6 @@ beforeEach(() => {

describe('FullPageScreenshot gatherer', () => {
it('captures a full-page screenshot', async () => {
const fpsGatherer = new FullPageScreenshotGatherer();
contentSize = {width: 412, height: 2000};
screenSize = {width: 412, height: 412};
screenshotSize = contentSize;
Expand Down Expand Up @@ -92,7 +101,6 @@ describe('FullPageScreenshot gatherer', () => {
});

it('resets the emulation correctly when Lighthouse controls it', async () => {
const fpsGatherer = new FullPageScreenshotGatherer();
contentSize = {width: 412, height: 2000};
screenSize = {width: 412, height: 412};
screenshotSize = contentSize;
Expand Down Expand Up @@ -126,7 +134,6 @@ describe('FullPageScreenshot gatherer', () => {
});

it('resets the emulation correctly when Lighthouse does not control it', async () => {
const fpsGatherer = new FullPageScreenshotGatherer();
contentSize = {width: 500, height: 1500};
screenSize = {width: 500, height: 500, dpr: 2};
screenshotSize = contentSize;
Expand Down Expand Up @@ -172,8 +179,6 @@ describe('FullPageScreenshot gatherer', () => {
});

it('limits the screenshot height to the max Chrome can capture', async () => {
const fpsGatherer = new FullPageScreenshotGatherer();

contentSize = {width: 412, height: 100000};
screenSize = {width: 412, height: 412, dpr: 1};
screenshotSize = contentSize;
Expand Down

0 comments on commit 468c7f9

Please sign in to comment.