chore: implement throwIfDisposed (#10819)

This commit is contained in:
jrandolf 2023-08-31 14:04:40 +02:00 committed by GitHub
parent f4ba479292
commit d91c3ed675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 7 deletions

View File

@ -19,6 +19,7 @@ import {Protocol} from 'devtools-protocol';
import {AutofillData, ElementHandle, Point} from '../api/ElementHandle.js'; import {AutofillData, ElementHandle, Point} from '../api/ElementHandle.js';
import {Page, ScreenshotOptions} from '../api/Page.js'; import {Page, ScreenshotOptions} from '../api/Page.js';
import {assert} from '../util/assert.js'; import {assert} from '../util/assert.js';
import {throwIfDisposed} from '../util/decorators.js';
import {CDPSession} from './Connection.js'; import {CDPSession} from './Connection.js';
import {ExecutionContext} from './ExecutionContext.js'; import {ExecutionContext} from './ExecutionContext.js';
@ -81,6 +82,7 @@ export class CDPElementHandle<
return this.#frame; return this.#frame;
} }
@throwIfDisposed()
override async $<Selector extends string>( override async $<Selector extends string>(
selector: Selector selector: Selector
): Promise<CDPElementHandle<NodeFor<Selector>> | null> { ): Promise<CDPElementHandle<NodeFor<Selector>> | null> {
@ -89,6 +91,7 @@ export class CDPElementHandle<
> | null>; > | null>;
} }
@throwIfDisposed()
override async $$<Selector extends string>( override async $$<Selector extends string>(
selector: Selector selector: Selector
): Promise<Array<CDPElementHandle<NodeFor<Selector>>>> { ): Promise<Array<CDPElementHandle<NodeFor<Selector>>>> {
@ -97,6 +100,7 @@ export class CDPElementHandle<
>; >;
} }
@throwIfDisposed()
override async waitForSelector<Selector extends string>( override async waitForSelector<Selector extends string>(
selector: Selector, selector: Selector,
options?: WaitForSelectorOptions options?: WaitForSelectorOptions
@ -109,6 +113,7 @@ export class CDPElementHandle<
override async contentFrame( override async contentFrame(
this: ElementHandle<HTMLIFrameElement> this: ElementHandle<HTMLIFrameElement>
): Promise<CDPFrame>; ): Promise<CDPFrame>;
@throwIfDisposed()
override async contentFrame(): Promise<CDPFrame | null> { override async contentFrame(): Promise<CDPFrame | null> {
const nodeInfo = await this.client.send('DOM.describeNode', { const nodeInfo = await this.client.send('DOM.describeNode', {
objectId: this.id, objectId: this.id,
@ -119,6 +124,7 @@ export class CDPElementHandle<
return this.#frameManager.frame(nodeInfo.node.frameId); return this.#frameManager.frame(nodeInfo.node.frameId);
} }
@throwIfDisposed()
override async scrollIntoView( override async scrollIntoView(
this: CDPElementHandle<Element> this: CDPElementHandle<Element>
): Promise<void> { ): Promise<void> {
@ -137,6 +143,7 @@ export class CDPElementHandle<
/** /**
* This method creates and captures a dragevent from the element. * This method creates and captures a dragevent from the element.
*/ */
@throwIfDisposed()
override async drag( override async drag(
this: CDPElementHandle<Element>, this: CDPElementHandle<Element>,
target: Point target: Point
@ -150,6 +157,7 @@ export class CDPElementHandle<
return await this.#page.mouse.drag(start, target); return await this.#page.mouse.drag(start, target);
} }
@throwIfDisposed()
override async dragEnter( override async dragEnter(
this: CDPElementHandle<Element>, this: CDPElementHandle<Element>,
data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1} data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1}
@ -159,6 +167,7 @@ export class CDPElementHandle<
await this.#page.mouse.dragEnter(target, data); await this.#page.mouse.dragEnter(target, data);
} }
@throwIfDisposed()
override async dragOver( override async dragOver(
this: CDPElementHandle<Element>, this: CDPElementHandle<Element>,
data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1} data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1}
@ -168,6 +177,7 @@ export class CDPElementHandle<
await this.#page.mouse.dragOver(target, data); await this.#page.mouse.dragOver(target, data);
} }
@throwIfDisposed()
override async drop( override async drop(
this: CDPElementHandle<Element>, this: CDPElementHandle<Element>,
data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1} data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1}
@ -177,6 +187,7 @@ export class CDPElementHandle<
await this.#page.mouse.drop(destination, data); await this.#page.mouse.drop(destination, data);
} }
@throwIfDisposed()
override async dragAndDrop( override async dragAndDrop(
this: CDPElementHandle<Element>, this: CDPElementHandle<Element>,
target: CDPElementHandle<Node>, target: CDPElementHandle<Node>,
@ -192,6 +203,7 @@ export class CDPElementHandle<
await this.#page.mouse.dragAndDrop(startPoint, targetPoint, options); await this.#page.mouse.dragAndDrop(startPoint, targetPoint, options);
} }
@throwIfDisposed()
override async uploadFile( override async uploadFile(
this: CDPElementHandle<HTMLInputElement>, this: CDPElementHandle<HTMLInputElement>,
...filePaths: string[] ...filePaths: string[]
@ -249,6 +261,7 @@ export class CDPElementHandle<
} }
} }
@throwIfDisposed()
override async screenshot( override async screenshot(
this: CDPElementHandle<Element>, this: CDPElementHandle<Element>,
options: ScreenshotOptions = {} options: ScreenshotOptions = {}
@ -307,6 +320,7 @@ export class CDPElementHandle<
return imageData; return imageData;
} }
@throwIfDisposed()
override async autofill(data: AutofillData): Promise<void> { override async autofill(data: AutofillData): Promise<void> {
const nodeInfo = await this.client.send('DOM.describeNode', { const nodeInfo = await this.client.send('DOM.describeNode', {
objectId: this.handle.id, objectId: this.handle.id,

View File

@ -29,6 +29,13 @@ export interface Moveable {
move(): this; move(): this;
} }
/**
* @internal
*/
export interface Disposed {
get disposed(): boolean;
}
/** /**
* @internal * @internal
*/ */

View File

@ -15,7 +15,7 @@
*/ */
import {Symbol} from '../../third_party/disposablestack/disposablestack.js'; import {Symbol} from '../../third_party/disposablestack/disposablestack.js';
import {Moveable} from '../common/types.js'; import {Disposed, Moveable} from '../common/types.js';
const instances = new WeakSet<object>(); const instances = new WeakSet<object>();
@ -57,3 +57,19 @@ export function moveable<
} }
return Class; return Class;
} }
export function throwIfDisposed(message?: string) {
return <This extends Disposed, Args extends unknown[], Ret>(
target: (this: This, ...args: Args) => Ret,
_: unknown
) => {
return function (this: This, ...args: Args): Ret {
if (this.disposed) {
throw new Error(
message ?? `Attempted to use disposed ${this.constructor.name}.`
);
}
return target.call(this, ...args);
};
};
}

View File

@ -17,6 +17,12 @@
"parameters": ["firefox"], "parameters": ["firefox"],
"expectations": ["FAIL"] "expectations": ["FAIL"]
}, },
{
"testIdPattern": "[bfcache.spec] *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["SKIP"]
},
{ {
"testIdPattern": "[chromiumonly.spec] Chromium-Specific Launcher tests *", "testIdPattern": "[chromiumonly.spec] Chromium-Specific Launcher tests *",
"platforms": ["darwin", "linux", "win32"], "platforms": ["darwin", "linux", "win32"],
@ -3899,12 +3905,6 @@
"parameters": ["firefox", "headful"], "parameters": ["firefox", "headful"],
"expectations": ["PASS", "TIMEOUT"] "expectations": ["PASS", "TIMEOUT"]
}, },
{
"testIdPattern": "[bfcache.spec] *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["SKIP"]
},
{ {
"testIdPattern": "[prerender.spec] Prerender can navigate to a prerendered page via input", "testIdPattern": "[prerender.spec] Prerender can navigate to a prerendered page via input",
"platforms": ["darwin", "linux", "win32"], "platforms": ["darwin", "linux", "win32"],