chore: add WaitForRequest and WaitForResponse (#10257)
This commit is contained in:
parent
ff22ba8679
commit
f342c26d95
@ -25,7 +25,9 @@ import {
|
|||||||
WaitForOptions,
|
WaitForOptions,
|
||||||
} from '../../api/Page.js';
|
} from '../../api/Page.js';
|
||||||
import {assert} from '../../util/assert.js';
|
import {assert} from '../../util/assert.js';
|
||||||
|
import {createDeferred} from '../../util/Deferred.js';
|
||||||
import {ConsoleMessage, ConsoleMessageLocation} from '../ConsoleMessage.js';
|
import {ConsoleMessage, ConsoleMessageLocation} from '../ConsoleMessage.js';
|
||||||
|
import {TargetCloseError} from '../Errors.js';
|
||||||
import {Handler} from '../EventEmitter.js';
|
import {Handler} from '../EventEmitter.js';
|
||||||
import {FrameManagerEmittedEvents} from '../FrameManager.js';
|
import {FrameManagerEmittedEvents} from '../FrameManager.js';
|
||||||
import {FrameTree} from '../FrameTree.js';
|
import {FrameTree} from '../FrameTree.js';
|
||||||
@ -36,6 +38,8 @@ import {TimeoutSettings} from '../TimeoutSettings.js';
|
|||||||
import {EvaluateFunc, HandleFor} from '../types.js';
|
import {EvaluateFunc, HandleFor} from '../types.js';
|
||||||
import {
|
import {
|
||||||
debugError,
|
debugError,
|
||||||
|
isString,
|
||||||
|
waitForEvent,
|
||||||
waitWithTimeout,
|
waitWithTimeout,
|
||||||
withSourcePuppeteerURLIfNone,
|
withSourcePuppeteerURLIfNone,
|
||||||
} from '../util.js';
|
} from '../util.js';
|
||||||
@ -43,6 +47,7 @@ import {
|
|||||||
import {BrowsingContext, getBidiHandle} from './BrowsingContext.js';
|
import {BrowsingContext, getBidiHandle} from './BrowsingContext.js';
|
||||||
import {Connection} from './Connection.js';
|
import {Connection} from './Connection.js';
|
||||||
import {Frame} from './Frame.js';
|
import {Frame} from './Frame.js';
|
||||||
|
import {HTTPRequest} from './HTTPRequest.js';
|
||||||
import {HTTPResponse} from './HTTPResponse.js';
|
import {HTTPResponse} from './HTTPResponse.js';
|
||||||
import {NetworkManager} from './NetworkManager.js';
|
import {NetworkManager} from './NetworkManager.js';
|
||||||
import {BidiSerializer} from './Serializer.js';
|
import {BidiSerializer} from './Serializer.js';
|
||||||
@ -56,7 +61,7 @@ export class Page extends PageBase {
|
|||||||
#frameTree = new FrameTree<Frame>();
|
#frameTree = new FrameTree<Frame>();
|
||||||
#networkManager: NetworkManager;
|
#networkManager: NetworkManager;
|
||||||
#viewport: Viewport | null = null;
|
#viewport: Viewport | null = null;
|
||||||
#closed = false;
|
#closedDeferred = createDeferred<TargetCloseError>();
|
||||||
#subscribedEvents = new Map<string, Handler<any>>([
|
#subscribedEvents = new Map<string, Handler<any>>([
|
||||||
['log.entryAdded', this.#onLogEntryAdded.bind(this)],
|
['log.entryAdded', this.#onLogEntryAdded.bind(this)],
|
||||||
[
|
[
|
||||||
@ -258,10 +263,10 @@ export class Page extends PageBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override async close(): Promise<void> {
|
override async close(): Promise<void> {
|
||||||
if (this.#closed) {
|
if (this.#closedDeferred.finished()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#closed = true;
|
this.#closedDeferred.resolve(new TargetCloseError('Page closed!'));
|
||||||
this.removeAllListeners();
|
this.removeAllListeners();
|
||||||
this.#networkManager.dispose();
|
this.#networkManager.dispose();
|
||||||
|
|
||||||
@ -445,6 +450,52 @@ export class Page extends PageBase {
|
|||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override waitForRequest(
|
||||||
|
urlOrPredicate: string | ((req: HTTPRequest) => boolean | Promise<boolean>),
|
||||||
|
options: {timeout?: number} = {}
|
||||||
|
): Promise<HTTPRequest> {
|
||||||
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
||||||
|
return waitForEvent(
|
||||||
|
this.#networkManager,
|
||||||
|
NetworkManagerEmittedEvents.Request,
|
||||||
|
async request => {
|
||||||
|
if (isString(urlOrPredicate)) {
|
||||||
|
return urlOrPredicate === request.url();
|
||||||
|
}
|
||||||
|
if (typeof urlOrPredicate === 'function') {
|
||||||
|
return !!(await urlOrPredicate(request));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
timeout,
|
||||||
|
this.#closedDeferred.valueOrThrow()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
override waitForResponse(
|
||||||
|
urlOrPredicate:
|
||||||
|
| string
|
||||||
|
| ((res: HTTPResponse) => boolean | Promise<boolean>),
|
||||||
|
options: {timeout?: number} = {}
|
||||||
|
): Promise<HTTPResponse> {
|
||||||
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
||||||
|
return waitForEvent(
|
||||||
|
this.#networkManager,
|
||||||
|
NetworkManagerEmittedEvents.Response,
|
||||||
|
async response => {
|
||||||
|
if (isString(urlOrPredicate)) {
|
||||||
|
return urlOrPredicate === response.url();
|
||||||
|
}
|
||||||
|
if (typeof urlOrPredicate === 'function') {
|
||||||
|
return !!(await urlOrPredicate(response));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
timeout,
|
||||||
|
this.#closedDeferred.valueOrThrow()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isConsoleLogEntry(
|
function isConsoleLogEntry(
|
||||||
|
@ -129,7 +129,7 @@
|
|||||||
"testIdPattern": "[network.spec] network Response.text *",
|
"testIdPattern": "[network.spec] network Response.text *",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
"parameters": ["webDriverBiDi"],
|
"parameters": ["webDriverBiDi"],
|
||||||
"expectations": ["FAIL"]
|
"expectations": ["FAIL", "TIMEOUT"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"testIdPattern": "[NetworkManager.spec] NetworkManager *",
|
"testIdPattern": "[NetworkManager.spec] NetworkManager *",
|
||||||
@ -167,6 +167,18 @@
|
|||||||
"parameters": ["webDriverBiDi"],
|
"parameters": ["webDriverBiDi"],
|
||||||
"expectations": ["PASS"]
|
"expectations": ["PASS"]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"testIdPattern": "[page.spec] Page Page.waitForRequest *",
|
||||||
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
|
"parameters": ["webDriverBiDi"],
|
||||||
|
"expectations": ["PASS"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"testIdPattern": "[page.spec] Page Page.waitForResponse *",
|
||||||
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
|
"parameters": ["webDriverBiDi"],
|
||||||
|
"expectations": ["PASS"]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"testIdPattern": "[queryhandler.spec] *",
|
"testIdPattern": "[queryhandler.spec] *",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
|
Loading…
Reference in New Issue
Block a user