Skip to content

Commit 07f0139

Browse files
authored
Merge pull request #2366 from tmsns/fix/headless-workaround
fix: add workaround for headless issue
2 parents 9dbe970 + 1b639a7 commit 07f0139

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

.changeset/itchy-socks-rule.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@web/test-runner-chrome': patch
3+
---
4+
5+
fix: add workaround for headless issue
6+
7+
This will patch `window.requestAnimationFrame` and `window.requestIdleCallback` and make sure that the tab running the test code is brought back to the front.

packages/test-runner-chrome/src/ChromeLauncherPage.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ import { TestRunnerCoreConfig } from '@web/test-runner-core';
33
import { v8ToIstanbul } from '@web/test-runner-coverage-v8';
44
import { SessionResult } from '@web/test-runner-core';
55

6+
declare global {
7+
interface Window {
8+
__bringTabToFront: () => void;
9+
}
10+
}
11+
612
export class ChromeLauncherPage {
713
private config: TestRunnerCoreConfig;
814
private testFiles: string[];
915
private product: string;
1016
public puppeteerPage: Page;
1117
private nativeInstrumentationEnabledOnPage = false;
18+
private patchAdded = false;
1219

1320
constructor(
1421
config: TestRunnerCoreConfig,
@@ -37,6 +44,29 @@ export class ChromeLauncherPage {
3744
});
3845
}
3946

47+
// Patching the browser page to workaround an issue in the new headless mode of Chrome where some functions
48+
// with callbacks (requestAnimationFrame and requestIdleCallback) are not executing their callbacks.
49+
// https://github.com/puppeteer/puppeteer/issues/10350
50+
if (!this.patchAdded) {
51+
await this.puppeteerPage.exposeFunction('__bringTabToFront', () =>
52+
this.puppeteerPage.bringToFront(),
53+
);
54+
await this.puppeteerPage.evaluateOnNewDocument(() => {
55+
// eslint-disable-next-line @typescript-eslint/ban-types
56+
function patchFunction(name: string, fn: Function) {
57+
(window as any)[name] = (...args: unknown[]) => {
58+
fn.call(window, ...args);
59+
// Make sure that the tab running the test code is brought back to the front.
60+
window.__bringTabToFront();
61+
};
62+
}
63+
64+
patchFunction('requestAnimationFrame', window.requestAnimationFrame);
65+
patchFunction('requestIdleCallback', window.requestIdleCallback);
66+
});
67+
this.patchAdded = true;
68+
}
69+
4070
await this.puppeteerPage.setViewport({ height: 600, width: 800 });
4171
await this.puppeteerPage.goto(url);
4272
}

packages/test-runner-chrome/test/chromeLauncher.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ describe('test-runner-chrome', function testRunnerChrome() {
1313
runIntegrationTests(createConfig, {
1414
basic: true,
1515
many: true,
16-
// Focus tests are failing because of a puppeteer/chrome bug.
17-
// See https://github.com/puppeteer/puppeteer/issues/10350 and
18-
// https://bugs.chromium.org/p/chromium/issues/detail?id=1454012
19-
focus: false,
16+
focus: true,
2017
groups: true,
2118
parallel: true,
2219
testFailure: true,

packages/test-runner-puppeteer/test/puppeteerLauncher.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ describe('test-runner-puppeteer', function testRunnerPuppeteer() {
1313
runIntegrationTests(createConfig, {
1414
basic: true,
1515
many: true,
16-
// Focus tests are failing because of a puppeteer/chrome bug.
17-
// See https://github.com/puppeteer/puppeteer/issues/10350 and
18-
// https://bugs.chromium.org/p/chromium/issues/detail?id=1454012
19-
focus: false,
16+
focus: true,
2017
groups: true,
2118
parallel: true,
2219
testFailure: true,

0 commit comments

Comments
 (0)