mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: start listening for navigation events before navigation starts (#32237)
There is a chance in case of cross-process navigation that the navigation event comes before `navigateFrame` finishes.
This commit is contained in:
parent
6512bccffd
commit
918dbe5e3a
@ -659,18 +659,24 @@ export class Frame extends SdkObject {
|
|||||||
}
|
}
|
||||||
url = helper.completeUserURL(url);
|
url = helper.completeUserURL(url);
|
||||||
|
|
||||||
const sameDocument = helper.waitForEvent(progress, this, Frame.Events.InternalNavigation, (e: NavigationEvent) => !e.newDocument);
|
const navigationEvents: NavigationEvent[] = [];
|
||||||
const navigateResult = await this._page._delegate.navigateFrame(this, url, referer);
|
const collectNavigations = (arg: NavigationEvent) => navigationEvents.push(arg);
|
||||||
|
this.on(Frame.Events.InternalNavigation, collectNavigations);
|
||||||
|
const navigateResult = await this._page._delegate.navigateFrame(this, url, referer).finally(
|
||||||
|
() => this.off(Frame.Events.InternalNavigation, collectNavigations));
|
||||||
|
|
||||||
let event: NavigationEvent;
|
let event: NavigationEvent;
|
||||||
if (navigateResult.newDocumentId) {
|
if (navigateResult.newDocumentId) {
|
||||||
sameDocument.dispose();
|
const predicate = (event: NavigationEvent) => {
|
||||||
event = await helper.waitForEvent(progress, this, Frame.Events.InternalNavigation, (event: NavigationEvent) => {
|
|
||||||
// We are interested either in this specific document, or any other document that
|
// We are interested either in this specific document, or any other document that
|
||||||
// did commit and replaced the expected document.
|
// did commit and replaced the expected document.
|
||||||
return event.newDocument && (event.newDocument.documentId === navigateResult.newDocumentId || !event.error);
|
return event.newDocument && (event.newDocument.documentId === navigateResult.newDocumentId || !event.error);
|
||||||
}).promise;
|
};
|
||||||
|
const events = navigationEvents.filter(predicate);
|
||||||
|
if (events.length)
|
||||||
|
event = events[0];
|
||||||
|
else
|
||||||
|
event = await helper.waitForEvent(progress, this, Frame.Events.InternalNavigation, predicate).promise;
|
||||||
if (event.newDocument!.documentId !== navigateResult.newDocumentId) {
|
if (event.newDocument!.documentId !== navigateResult.newDocumentId) {
|
||||||
// This is just a sanity check. In practice, new navigation should
|
// This is just a sanity check. In practice, new navigation should
|
||||||
// cancel the previous one and report "request cancelled"-like error.
|
// cancel the previous one and report "request cancelled"-like error.
|
||||||
@ -679,7 +685,13 @@ export class Frame extends SdkObject {
|
|||||||
if (event.error)
|
if (event.error)
|
||||||
throw event.error;
|
throw event.error;
|
||||||
} else {
|
} else {
|
||||||
event = await sameDocument.promise;
|
// Wait for same document navigation.
|
||||||
|
const predicate = (e: NavigationEvent) => !e.newDocument;
|
||||||
|
const events = navigationEvents.filter(predicate);
|
||||||
|
if (events.length)
|
||||||
|
event = events[0];
|
||||||
|
else
|
||||||
|
event = await helper.waitForEvent(progress, this, Frame.Events.InternalNavigation, predicate).promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this._firedLifecycleEvents.has(waitUntil))
|
if (!this._firedLifecycleEvents.has(waitUntil))
|
||||||
|
|||||||
@ -181,8 +181,9 @@ it('should work with Cross-Origin-Opener-Policy after redirect', async ({ page,
|
|||||||
|
|
||||||
it('should properly cancel Cross-Origin-Opener-Policy navigation', {
|
it('should properly cancel Cross-Origin-Opener-Policy navigation', {
|
||||||
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/32107' },
|
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/32107' },
|
||||||
}, async ({ page, server, browserName, isLinux }) => {
|
}, async ({ page, server, browserName, isLinux, headless }) => {
|
||||||
it.fixme(browserName === 'webkit' && isLinux, 'Started failing after https://commits.webkit.org/281488@main');
|
it.fixme(browserName === 'webkit' && isLinux, 'Started failing after https://commits.webkit.org/281488@main');
|
||||||
|
it.fixme(browserName === 'chromium' && headless, 'COOP navigation cancels the one that starts later');
|
||||||
server.setRoute('/empty.html', (req, res) => {
|
server.setRoute('/empty.html', (req, res) => {
|
||||||
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
|
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
|
||||||
res.end();
|
res.end();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user