feat: support promise as return value for page.waitForResponse predicate (#6624)

Co-authored-by: Joni <josalmi@google.com>
This commit is contained in:
Michael Kebede 2020-11-25 13:35:47 +03:00 committed by GitHub
parent 3354aa7fa8
commit b57f3fcd53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 5 deletions

View File

@ -2255,6 +2255,7 @@ return firstRequest.url();
```js ```js
const firstResponse = await page.waitForResponse('https://example.com/resource'); const firstResponse = await page.waitForResponse('https://example.com/resource');
const finalResponse = await page.waitForResponse(response => response.url() === 'https://example.com' && response.status() === 200); const finalResponse = await page.waitForResponse(response => response.url() === 'https://example.com' && response.status() === 200);
const finalResponse = await page.waitForResponse(async response => { return (await response.text()).includes('<html>') })
return finalResponse.ok(); return finalResponse.ok();
``` ```

View File

@ -1298,11 +1298,11 @@ export class Page extends EventEmitter {
return helper.waitForEvent( return helper.waitForEvent(
this._frameManager.networkManager(), this._frameManager.networkManager(),
NetworkManagerEmittedEvents.Response, NetworkManagerEmittedEvents.Response,
(response) => { async (response) => {
if (helper.isString(urlOrPredicate)) if (helper.isString(urlOrPredicate))
return urlOrPredicate === response.url(); return urlOrPredicate === response.url();
if (typeof urlOrPredicate === 'function') if (typeof urlOrPredicate === 'function')
return !!urlOrPredicate(response); return !!(await urlOrPredicate(response));
return false; return false;
}, },
timeout, timeout,

View File

@ -124,7 +124,7 @@ function isNumber(obj: unknown): obj is number {
async function waitForEvent<T extends any>( async function waitForEvent<T extends any>(
emitter: CommonEventEmitter, emitter: CommonEventEmitter,
eventName: string | symbol, eventName: string | symbol,
predicate: (event: T) => boolean, predicate: (event: T) => Promise<boolean> | boolean,
timeout: number, timeout: number,
abortPromise: Promise<Error> abortPromise: Promise<Error>
): Promise<T> { ): Promise<T> {
@ -133,8 +133,8 @@ async function waitForEvent<T extends any>(
resolveCallback = resolve; resolveCallback = resolve;
rejectCallback = reject; rejectCallback = reject;
}); });
const listener = addEventListener(emitter, eventName, (event) => { const listener = addEventListener(emitter, eventName, async (event) => {
if (!predicate(event)) return; if (!(await predicate(event))) return;
resolveCallback(event); resolveCallback(event);
}); });
if (timeout) { if (timeout) {

View File

@ -712,6 +712,22 @@ describe('Page', function () {
.catch((error_) => (error = error_)); .catch((error_) => (error = error_));
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
}); });
it('should work with async predicate', async () => {
const { page, server } = getTestState();
await page.goto(server.EMPTY_PAGE);
const [response] = await Promise.all([
page.waitForResponse(async (response) => {
console.log(response.url());
return response.url() === server.PREFIX + '/digits/2.png';
}),
page.evaluate(() => {
fetch('/digits/1.png');
fetch('/digits/2.png');
fetch('/digits/3.png');
}),
]);
expect(response.url()).toBe(server.PREFIX + '/digits/2.png');
});
it('should work with no timeout', async () => { it('should work with no timeout', async () => {
const { page, server } = getTestState(); const { page, server } = getTestState();