Skip to content

Commit fc594b2

Browse files
loganjLarry
andauthored
test(browser): explain WebKit fetch refusals during a page reload (#650)
Signed-off-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
1 parent 04ab197 commit fc594b2

2 files changed

Lines changed: 104 additions & 9 deletions

File tree

‎tests/browser/page-errors.mjs‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,56 @@ const errorText = (error) =>
2525
export function watchPageErrors(page) {
2626
const webkit = page.context().browser()?.browserType().name() === "webkit";
2727
const errors = [];
28+
// Access-control logs that the old document reported while the main frame
29+
// was navigating away (see `unexplained`).
30+
const leaving = new Set();
2831
const cancelled = [];
29-
page.on("pageerror", (error) => errors.push(errorText(error)));
30-
if (webkit)
32+
// The main-frame navigation request that is replacing the current document.
33+
let navigation;
34+
page.on("pageerror", (error) => {
35+
errors.push(errorText(error));
36+
if (navigation) leaving.add(errors.length - 1);
37+
});
38+
if (webkit) {
39+
page.on("request", (request) => {
40+
if (request.isNavigationRequest() && request.frame() === page.mainFrame())
41+
navigation = request;
42+
});
43+
page.on("framenavigated", (frame) => {
44+
if (frame === page.mainFrame()) navigation = undefined;
45+
});
3146
page.on("requestfailed", (request) => {
47+
if (request === navigation) navigation = undefined;
3248
if (cancellations.has(request.failure()?.errorText))
3349
cancelled.push(request.url());
3450
});
51+
}
3552
return {
3653
errors,
3754
unexplained() {
3855
if (!webkit) return [...errors];
3956
const unmatched = [...cancelled];
40-
return errors.filter((message) => {
57+
return errors.filter((message, index) => {
4158
// A layout warning that WebKit reports as a page error.
4259
if (message === resizeObserverLoop) return false;
4360
// When a reload or navigation cancels a fetch, WebKit logs the fetch as
4461
// an access-control failure even though the caller handles the
4562
// rejection. An unhandled rejection is a separate page error named
46-
// "Unhandled Promise Rejection". Accept the log only for a request that
47-
// Playwright saw cancelled, once per cancellation.
63+
// "Unhandled Promise Rejection".
4864
const url = cancelledLoad.exec(message)?.[1];
49-
const match = url ? unmatched.indexOf(url) : -1;
50-
if (match < 0) return true;
51-
unmatched.splice(match, 1);
52-
return false;
65+
if (!url) return true;
66+
// Accept the log for a request that Playwright saw cancelled, once per
67+
// cancellation. Use up a matching cancellation first, even during a
68+
// navigation, so it cannot explain a later log for the same URL.
69+
const match = unmatched.indexOf(url);
70+
if (match >= 0) {
71+
unmatched.splice(match, 1);
72+
return false;
73+
}
74+
// A fetch that the old document starts after the main-frame navigation
75+
// request and before the new document commits is refused inside
76+
// fetch(). Playwright sees no request for it.
77+
return !leaving.has(index);
5378
});
5479
},
5580
};

‎tests/integration/page-errors.test.mjs‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { watchPageErrors } from "../browser/page-errors.mjs";
66
// A Playwright page reduced to the events and browser identity the watcher reads.
77
const fakePage = (engine) => {
88
const page = new EventEmitter();
9+
const mainFrame = {};
10+
page.mainFrame = () => mainFrame;
911
page.context = () => ({
1012
browser: () => ({ browserType: () => ({ name: () => engine }) }),
1113
});
@@ -68,6 +70,62 @@ test("the access-control log fails without a matching cancelled request", () =>
6870
}
6971
});
7072

73+
// Replayed from the Linux WebKit trace of channel-archive-delete-pane.spec.mjs:28
74+
// on main (run 37478134645 for 2bb37a55): a presence timer fired during the
75+
// reload, and WebKit refused both fetches without a request event.
76+
const presenceAuthors =
77+
"http://127.0.0.1:40091/api/relay/primary/stream-presence-authors";
78+
const navigate = (page, frame = page.mainFrame()) => {
79+
const request = {
80+
isNavigationRequest: () => true,
81+
frame: () => frame,
82+
url: () => "http://127.0.0.1:40091/",
83+
failure: () => ({ errorText: "Load request cancelled" }),
84+
};
85+
page.emit("request", request);
86+
return request;
87+
};
88+
89+
test("WebKit's log for a fetch refused while the document is replaced is explained", () => {
90+
const page = fakePage("webkit");
91+
const watched = watchPageErrors(page);
92+
navigate(page);
93+
page.emit("pageerror", reloadCancelLog(presenceAuthors));
94+
page.emit("framenavigated", page.mainFrame());
95+
assert.deepEqual(watched.errors, [
96+
`Fetch API cannot load ${presenceAuthors} due to access control checks.`,
97+
]);
98+
assert.deepEqual(watched.unexplained(), []);
99+
});
100+
101+
test("only the main frame's pending navigation explains a refused fetch", () => {
102+
for (const setup of [
103+
// The new document has committed.
104+
(page) => {
105+
navigate(page);
106+
page.emit("framenavigated", page.mainFrame());
107+
},
108+
// The navigation failed, so the old document stays.
109+
(page) => page.emit("requestfailed", navigate(page)),
110+
// A child frame navigates.
111+
(page) => navigate(page, {}),
112+
]) {
113+
const page = fakePage("webkit");
114+
const watched = watchPageErrors(page);
115+
setup(page);
116+
page.emit("pageerror", reloadCancelLog(presenceAuthors));
117+
assert.equal(watched.unexplained().length, 1);
118+
}
119+
});
120+
121+
test("a pending navigation does not explain other page errors", () => {
122+
const page = fakePage("webkit");
123+
const watched = watchPageErrors(page);
124+
navigate(page);
125+
page.emit("pageerror", new TypeError("a: b"));
126+
assert.deepEqual(watched.unexplained(), ["TypeError: a: b"]);
127+
});
128+
71129
test("one cancellation explains one log", () => {
72130
const page = fakePage("webkit");
73131
const watched = watchPageErrors(page);
@@ -77,6 +135,18 @@ test("one cancellation explains one log", () => {
77135
assert.equal(watched.unexplained().length, 1);
78136
});
79137

138+
test("a log during navigation uses up its cancellation", () => {
139+
const page = fakePage("webkit");
140+
const watched = watchPageErrors(page);
141+
navigate(page);
142+
cancel(page, query);
143+
page.emit("pageerror", reloadCancelLog());
144+
page.emit("framenavigated", page.mainFrame());
145+
// The new document reports the same URL without a new cancellation.
146+
page.emit("pageerror", reloadCancelLog());
147+
assert.equal(watched.unexplained().length, 1);
148+
});
149+
80150
test("an unhandled rejection is never an engine report", () => {
81151
const page = fakePage("webkit");
82152
const watched = watchPageErrors(page);

0 commit comments

Comments
 (0)