Skip to content

Commit

Permalink
test(visual): Fix e2e tests in new headless mode
Browse files Browse the repository at this point in the history
- Don't use intersection observer in the test code, instead just poll.
- Patch visual-regression to focus tab before taking a screenshot.
  • Loading branch information
melink14 committed Dec 18, 2024
1 parent 2560055 commit 5a6bac1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
27 changes: 17 additions & 10 deletions extension/test/e2e_visual_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,23 @@ async function takeSnapshot(name: string) {
}

function waitForVisiblePopup(): Promise<void> {
return new Promise((resolve) => {
const popup = document.querySelector<HTMLDivElement>('#rikaichan-window');
if (!popup) {
return;
}
const o = new IntersectionObserver(() => {
resolve();
o.disconnect();
});
o.observe(popup);
return new Promise((resolve, reject) => {
const startTime = Date.now();
const checkElement = () => {
if (document.querySelector<HTMLDivElement>('#rikaichan-window')) {
resolve();
return;
}
// Timing out here gives a better error message than waiting for
// the global test timeout.
if (Date.now() - startTime > 2000) {
reject(new Error("Rikaikun popup wasn't visible for 2000ms"));
return;
}

requestAnimationFrame(checkElement);
};
checkElement();
});
}

Expand Down
13 changes: 13 additions & 0 deletions patches/@web+test-runner-visual-regression+0.9.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/@web/test-runner-visual-regression/dist/visualRegressionPlugin.js b/node_modules/@web/test-runner-visual-regression/dist/visualRegressionPlugin.js
index bd4e733..416bd33 100644
--- a/node_modules/@web/test-runner-visual-regression/dist/visualRegressionPlugin.js
+++ b/node_modules/@web/test-runner-visual-regression/dist/visualRegressionPlugin.js
@@ -42,6 +42,8 @@ function visualRegressionPlugin(options = {}) {
if (!element) {
throw new VisualRegressionError_1.VisualRegressionError('Something went wrong diffing element, the browser could not find it.');
}
+ // Screenshots must be done in the foreground.
+ page.bringToFront();
const screenshot = (await element.screenshot({ encoding: 'binary' }));
return (0, visualDiffCommand_1.visualDiffCommand)(mergedOptions, screenshot, payload.name, context);
}

0 comments on commit 5a6bac1

Please sign in to comment.