chore: fix bidi reload (#10911)

This commit is contained in:
Nikolay Vitkov 2023-09-14 15:40:18 +02:00 committed by GitHub
parent 3c659e7170
commit e8640fff16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 35 deletions

View File

@ -245,8 +245,7 @@ export class BidiBrowser extends Browser {
if (this.#connection.closed) { if (this.#connection.closed) {
return; return;
} }
// TODO: implement browser.close. await this.#connection.send('browser.close', {});
// await this.#connection.send('browser.close', {});
this.#connection.dispose(); this.#connection.dispose();
await this.#closeCallback?.call(null); await this.#closeCallback?.call(null);
} }

View File

@ -236,14 +236,17 @@ export class BrowsingContext extends Realm {
} }
} }
async reload(options: WaitForOptions & {timeout: number}): Promise<void> { async reload(
options: WaitForOptions & {timeout: number}
): Promise<string | null> {
const {waitUntil = 'load', timeout} = options; const {waitUntil = 'load', timeout} = options;
const readinessState = lifeCycleToReadinessState.get( const readinessState = lifeCycleToReadinessState.get(
getWaitUntilSingle(waitUntil) getWaitUntilSingle(waitUntil)
) as Bidi.BrowsingContext.ReadinessState; ) as Bidi.BrowsingContext.ReadinessState;
await waitWithTimeout( try {
const {result} = await waitWithTimeout(
this.connection.send('browsingContext.reload', { this.connection.send('browsingContext.reload', {
context: this.#id, context: this.#id,
wait: readinessState, wait: readinessState,
@ -251,6 +254,16 @@ export class BrowsingContext extends Realm {
'Navigation', 'Navigation',
timeout timeout
); );
return result.navigation;
} catch (error) {
if (error instanceof ProtocolError) {
error.message += ` at ${this.url}`;
} else if (error instanceof TimeoutError) {
error.message = 'Navigation timeout of ' + timeout + ' ms exceeded';
}
throw error;
}
} }
async setContent( async setContent(

View File

@ -52,6 +52,11 @@ interface Commands {
returnType: Bidi.EmptyResult; returnType: Bidi.EmptyResult;
}; };
'browser.close': {
params: Bidi.EmptyParams;
returnType: Bidi.EmptyResult;
};
'browsingContext.activate': { 'browsingContext.activate': {
params: Bidi.BrowsingContext.ActivateParameters; params: Bidi.BrowsingContext.ActivateParameters;
returnType: Bidi.EmptyResult; returnType: Bidi.EmptyResult;
@ -74,7 +79,7 @@ interface Commands {
}; };
'browsingContext.reload': { 'browsingContext.reload': {
params: Bidi.BrowsingContext.ReloadParameters; params: Bidi.BrowsingContext.ReloadParameters;
returnType: Bidi.EmptyResult; returnType: Bidi.BrowsingContext.NavigateResult;
}; };
'browsingContext.print': { 'browsingContext.print': {
params: Bidi.BrowsingContext.PrintParameters; params: Bidi.BrowsingContext.PrintParameters;
@ -281,6 +286,7 @@ export class BidiConnection extends EventEmitter<BidiEvents> {
this.#closed = true; this.#closed = true;
this.#transport.onmessage = undefined; this.#transport.onmessage = undefined;
this.#transport.onclose = undefined; this.#transport.onclose = undefined;
this.#callbacks.clear(); this.#callbacks.clear();
} }

View File

@ -410,23 +410,13 @@ export class BidiPage extends Page {
override async reload( override async reload(
options?: WaitForOptions options?: WaitForOptions
): Promise<BidiHTTPResponse | null> { ): Promise<BidiHTTPResponse | null> {
const [response] = await Promise.all([ const navigationId = await this.mainFrame()
this.waitForResponse(response => {
return (
response.request().isNavigationRequest() &&
response.url() === this.url()
);
}),
this.mainFrame()
.context() .context()
.reload({ .reload({
...options, ...options,
timeout: timeout: options?.timeout ?? this.#timeoutSettings.navigationTimeout(),
options?.timeout ?? this.#timeoutSettings.navigationTimeout(), });
}), return this.getNavigationResponse(navigationId);
]);
return response;
} }
override setDefaultNavigationTimeout(timeout: number): void { override setDefaultNavigationTimeout(timeout: number): void {
@ -494,8 +484,7 @@ export class BidiPage extends Page {
await this.#cdpEmulationManager.emulateViewport(viewport); await this.#cdpEmulationManager.emulateViewport(viewport);
this.#viewport = viewport; this.#viewport = viewport;
if (needsReload) { if (needsReload) {
// TODO: reload seems to hang in BiDi. await this.reload();
// await this.reload();
} }
} }

View File

@ -231,8 +231,9 @@ export const withSourcePuppeteerURLIfNone = <T extends NonNullable<unknown>>(
} }
const original = Error.prepareStackTrace; const original = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => { Error.prepareStackTrace = (_, stack) => {
// First element is the function. Second element is the caller of this // First element is the function.
// function. Third element is the caller of the caller of this function // Second element is the caller of this function.
// Third element is the caller of the caller of this function
// which is precisely what we want. // which is precisely what we want.
return stack[2]; return stack[2];
}; };

View File

@ -1863,7 +1863,7 @@
"testIdPattern": "[emulation.spec] Emulation Page.viewport should detect touch when applying viewport with touches", "testIdPattern": "[emulation.spec] Emulation Page.viewport should detect touch when applying viewport with touches",
"platforms": ["darwin", "linux", "win32"], "platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome", "webDriverBiDi"], "parameters": ["chrome", "webDriverBiDi"],
"expectations": ["FAIL"] "expectations": ["PASS"]
}, },
{ {
"testIdPattern": "[emulation.spec] Emulation Page.viewport should get the proper viewport size", "testIdPattern": "[emulation.spec] Emulation Page.viewport should get the proper viewport size",
@ -1881,7 +1881,7 @@
"testIdPattern": "[emulation.spec] Emulation Page.viewport should support touch emulation", "testIdPattern": "[emulation.spec] Emulation Page.viewport should support touch emulation",
"platforms": ["darwin", "linux", "win32"], "platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome", "webDriverBiDi"], "parameters": ["chrome", "webDriverBiDi"],
"expectations": ["FAIL"] "expectations": ["PASS"]
}, },
{ {
"testIdPattern": "[evaluation.spec] Evaluation specs \"after each\" hook for \"should transfer 100Mb of data from page to node.js\"", "testIdPattern": "[evaluation.spec] Evaluation specs \"after each\" hook for \"should transfer 100Mb of data from page to node.js\"",