gui tests: the masking-rules tests act on Settings' rows once the page has drawn its last read of the settings, not the first (#1653)

redact-rule-row failed in WebKit zh on a loaded machine with
"locator.scrollIntoViewIfNeeded: Element is not attached to the DOM".

Cause: opening Settings reads the settings twice, the page's own read
(showView) and the window's load() once the state is in, and each read
redraws every row (renderRedact replaces #redactList's children). The
test found the row after the first draw and scrolled it; when the second
read landed during Playwright's stability check, the element it held was
gone. Locator clicks retry on a detached element, but
scrollIntoViewIfNeeded and boundingBox act on the element they found.
A probe wrapping #redactList's replaceChildren showed the two draws
(renderSettings < loadSettings, then the same < async load).

Fix: reader.cjs gains watchSettingsReads (an init script counting the
page's GET /api/settings reads, each counted drawn a task after its body
is read, after loadSettings has drawn from it) and settingsDrawn (waits
for load.done, which load() sets in the same run that then asks for the
settings, and for every read asked to be drawn). redact-rule-row and its
two siblings on the same rows, redact-rule-edit and redact-kinds, wait
for it after finding their first row. The page is unchanged: its second
draw is how Settings catches up with the state.

Run (injected: the test's route holds the page's second settings read
until the row is found, and lets it go as the test acts on it):
- Injected, without the wait: 8 of 8 red in Chromium and WebKit, en and
  zh, 1100 and 640px, the issue's exact error. With the wait: 8 of 8
  green.
- redact-rule-row WebKit, 25 runs: 100 of 100 cases green at load ~115.
- redact-rule-row, redact-rule-edit and redact-kinds in both engines:
  50 of 50, and again 50 of 50 (injected 8 of 8) after rebasing
  onto 18e82adc6.

Not done here: 37 other Settings tests use boundingBox or
scrollIntoViewIfNeeded after finding a row and can meet the same second
draw; #1659 lists them, to move onto settingsDrawn.
This commit is contained in:
yetone
2026-10-12 00:57:27 +08:00
parent 751ebe8f5a
commit 5dfcf43465
4 changed files with 42 additions and 1 deletions
+33 -1
View File
@@ -31,4 +31,36 @@ async function click(page, locator) {
await locator.click();
}
module.exports = { inView, click };
// Settings is drawn from each read of the settings, and opening the page
// reads them twice: the page's own read, then the window's load() once the
// state is in. Every row is replaced by the second draw, so a row found
// after the first is detached under whatever the test does to it next
// (#1653: scrollIntoViewIfNeeded in WebKit on a busy machine). Call
// watchSettingsReads before page.goto; settingsDrawn then waits until load()
// has asked for its read and every read asked for has been drawn.
async function watchSettingsReads(page) {
await page.addInitScript(() => {
const reads = window.__settingsReads = { asked: 0, drawn: 0 };
const fetch0 = window.fetch;
window.fetch = function (input, init) {
const p = fetch0.apply(this, arguments);
const url = new URL(typeof input === "string" ? input : input.url, location.href);
if (url.pathname !== "/api/settings" || (init?.method || "GET") !== "GET") return p;
reads.asked++;
// drawn a task after its body is in: loadSettings draws in the
// microtasks that follow
const drawn = () => setTimeout(() => reads.drawn++);
return p.then((res) => {
const text = res.text;
res.text = function () { return text.call(this).finally(drawn); };
return res;
}, (e) => { drawn(); throw e; });
};
});
}
// load.done is set in the same run of load() that then asks for the
// settings, with no wait between, so once it is set the read is counted.
const settingsDrawn = (page) => page.waitForFunction(() => load.done && __settingsReads.asked === __settingsReads.drawn);
module.exports = { inView, click, watchSettingsReads, settingsDrawn };
+3
View File
@@ -10,6 +10,7 @@ const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const ids = ["ssn", "passport", "iban", "birthday", "mac", "serial", "home", "userhost", "ip", "bucket"];
@@ -64,8 +65,10 @@ async function open(engine, lang, width, personal, saved) {
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, personal, saved));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=privacy");
await page.locator("#redactList .row").first().waitFor();
await settingsDrawn(page); // the rows found are the ones that stay (#1653)
return { browser, page, errors };
}
@@ -12,6 +12,7 @@ const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const words = {
@@ -64,9 +65,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
const errors = [], calls = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, calls));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=privacy");
const head = page.locator("#redactList .row.rule-row").first();
await head.waitFor();
await settingsDrawn(page); // the rows found are the ones that stay (#1653)
// the rules, in order, from the row that adds one
const listed = () => head.evaluate((h) => {
const out = [];
@@ -9,6 +9,7 @@ const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const settings = (lang) => ({ lang, theme: "light", redact: true, redactRules: [{ kind: "GATEWAY_KEY", prefix: "oc_sk_" }] });
@@ -47,9 +48,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=privacy");
const row = page.locator("#redactList .row.rule-row");
await row.waitFor();
await settingsDrawn(page); // the row found is the one that stays (#1653)
await row.scrollIntoViewIfNeeded();
const m = await row.evaluate((r) => {