fix: waitForNavigation issue with aborted events (#9883)

This commit is contained in:
Nikolay Vitkov 2023-03-21 10:22:57 +01:00 committed by GitHub
parent 3ea15dc395
commit 36c029b38d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 14 deletions

View File

@ -302,11 +302,7 @@ export class NetworkManager extends EventEmitter {
}
#onAuthRequired(event: Protocol.Fetch.AuthRequiredEvent): void {
/* TODO(jacktfranklin): This is defined in protocol.d.ts but not
* in an easily referrable way - we should look at exposing it.
*/
type AuthResponse = 'Default' | 'CancelAuth' | 'ProvideCredentials';
let response: AuthResponse = 'Default';
let response: Protocol.Fetch.AuthChallengeResponse['response'] = 'Default';
if (this.#attemptedAuthentications.has(event.requestId)) {
response = 'CancelAuth';
} else if (this.#credentials) {

View File

@ -991,10 +991,7 @@ export class CDPPage extends Page {
const networkManager = this.#frameManager.networkManager;
let idleResolveCallback: () => void;
const idlePromise = new Promise<void>(resolve => {
idleResolveCallback = resolve;
});
const idlePromise = createDeferredPromise<void>();
let abortRejectCallback: (error: Error) => void;
const abortPromise = new Promise<Error>((_, reject) => {
@ -1002,10 +999,6 @@ export class CDPPage extends Page {
});
let idleTimer: NodeJS.Timeout;
const onIdle = () => {
return idleResolveCallback();
};
const cleanup = () => {
idleTimer && clearTimeout(idleTimer);
abortRejectCallback(new Error('abort'));
@ -1014,7 +1007,7 @@ export class CDPPage extends Page {
const evaluate = () => {
idleTimer && clearTimeout(idleTimer);
if (networkManager.numRequestsInProgress() === 0) {
idleTimer = setTimeout(onIdle, idleTime);
idleTimer = setTimeout(idlePromise.resolve, idleTime);
}
};
@ -1038,6 +1031,7 @@ export class CDPPage extends Page {
const eventPromises = [
listenToEvent(NetworkManagerEmittedEvents.Request),
listenToEvent(NetworkManagerEmittedEvents.Response),
listenToEvent(NetworkManagerEmittedEvents.RequestFailed),
];
await Promise.race([

View File

@ -0,0 +1,13 @@
<button id="abort"></button>
<script>
const button = document.getElementById('abort');
button.addEventListener('click', getJson)
async function getJson() {
const abort = new AbortController();
const result = fetch("/simple.json", {
signal: abort.signal
});
abort.abort();
}
</script>

View File

@ -1163,6 +1163,21 @@ describe('Page', function () {
]);
expect(result).toBe(undefined);
});
it('should work with aborted requests', async () => {
const {page, server} = getTestState();
await page.goto(server.PREFIX + '/abort-request.html');
const element = await page.$(`#abort`);
await element!.click();
let error = false;
await page.waitForNetworkIdle().catch(() => {
return (error = true);
});
expect(error).toBe(false);
});
});
describe('Page.exposeFunction', function () {