Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

test(webv2): reset the runner's outer page scroll before each browser test

Lincoln Steind873c899e9dc
diff --git a/invokeai/frontend/webv2/scripts/browser-test-viewport.ts b/invokeai/frontend/webv2/scripts/browser-test-viewport.ts
new file mode 100644
index 0000000000..1262cbb182
--- /dev/null
+++ b/invokeai/frontend/webv2/scripts/browser-test-viewport.ts
@@ -0,0 +1,25 @@
+import { beforeEach } from 'vitest';
+
+/**
+ * Start every browser test with the pages around the test iframe scrolled to the origin.
+ *
+ * The runner's outer page is a few pixels taller than its viewport, so a `scrollIntoView` or `focus()` inside a
+ * test scrolls it too, and that offset outlives the test file. Playwright judges hover and click targets against the
+ * top-level viewport, so a later file's elements at the iframe's top edge then sit outside it and every pointer
+ * action on them times out (FeatureHint's corner parking element fails all five of its tests this way). The test's
+ * own document is left alone: its scroll belongs to the test.
+ */
+beforeEach(() => {
+ let frame: Window = window;
+
+ while (frame.parent !== frame) {
+ frame = frame.parent;
+
+ try {
+ frame.scrollTo(0, 0);
+ } catch {
+ // A cross-origin ancestor cannot be scrolled from here, and nothing inside it can leak into this one.
+ return;
+ }
+ }
+});
diff --git a/invokeai/frontend/webv2/src/architecture/browserTestViewport.browser.test.ts b/invokeai/frontend/webv2/src/architecture/browserTestViewport.browser.test.ts
new file mode 100644
index 0000000000..b2f2c4bc00
--- /dev/null
+++ b/invokeai/frontend/webv2/src/architecture/browserTestViewport.browser.test.ts
@@ -0,0 +1,33 @@
+import { expect, it } from 'vitest';
+import { userEvent } from 'vitest/browser';
+
+/*
+ * Guards scripts/browser-test-viewport.ts. The two tests run in order: the first leaves the runner's outer page
+ * scrolled, as any scrollIntoView or focus() in a test can, and the second needs the reset before it to reach an
+ * element at the iframe's top edge. Without the reset the hover times out, as FeatureHint's suite did on CI.
+ */
+
+it('scrolls the outer page from inside the test iframe', () => {
+ const below = document.createElement('div');
+
+ below.style.cssText = 'position:absolute;left:0;top:1200px;width:10px;height:10px';
+ document.body.append(below);
+ below.scrollIntoView();
+ below.remove();
+ window.scrollTo(0, 0);
+
+ expect(window.parent.scrollY).toBeGreaterThan(0);
+});
+
+it('starts the next test with an element at the iframe corner reachable', async () => {
+ const corner = document.createElement('div');
+
+ corner.style.cssText = 'position:fixed;left:0;top:0;width:2px;height:2px;z-index:2147483647';
+ document.body.append(corner);
+
+ try {
+ await userEvent.hover(corner, { timeout: 2000 });
+ } finally {
+ corner.remove();
+ }
+});
diff --git a/invokeai/frontend/webv2/vitest.browser.config.mts b/invokeai/frontend/webv2/vitest.browser.config.mts
index b53bc413a2..dc98c71523 100644
--- a/invokeai/frontend/webv2/vitest.browser.config.mts
+++ b/invokeai/frontend/webv2/vitest.browser.config.mts
@@ -59,7 +59,7 @@ export default mergeConfig(
provider: playwright(),
},
include: ['src/**/*.browser.test.{ts,tsx}'],
- setupFiles: ['./scripts/browser-test-console.ts'],
+ setupFiles: ['./scripts/browser-test-console.ts', './scripts/browser-test-viewport.ts'],
},
})
);