Skip to content

Commit

Permalink
chore: make execution context frame-independent
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Aug 25, 2022
1 parent 2f33237 commit f0a7e18
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 26 deletions.
10 changes: 8 additions & 2 deletions src/common/ElementHandle.ts
Expand Up @@ -92,6 +92,13 @@ export class ElementHandle<
return this.#frame.page();
}

/**
* @internal
*/
get frame(): Frame {
return this.#frame;
}

/**
* Queries the current element for an element matching the given selector.
*
Expand Down Expand Up @@ -294,8 +301,7 @@ export class ElementHandle<
selector: Selector,
options: Exclude<WaitForSelectorOptions, 'root'> = {}
): Promise<ElementHandle<NodeFor<Selector>> | null> {
const frame = this.executionContext().frame();
assert(frame);
const frame = this.#frame;
const adoptedRoot = await frame.worlds[PUPPETEER_WORLD].adoptHandle(this);
const handle = await frame.worlds[PUPPETEER_WORLD].waitForSelector(
selector,
Expand Down
13 changes: 0 additions & 13 deletions src/common/ExecutionContext.ts
Expand Up @@ -17,7 +17,6 @@
import {Protocol} from 'devtools-protocol';
import {assert} from '../util/assert.js';
import {CDPSession} from './Connection.js';
import {Frame} from './Frame.js';
import {IsolatedWorld} from './IsolatedWorld.js';
import {JSHandle} from './JSHandle.js';
import {EvaluateFunc, HandleFor} from './types.js';
Expand Down Expand Up @@ -88,18 +87,6 @@ export class ExecutionContext {
this._contextName = contextPayload.name;
}

/**
* @returns The frame associated with this execution context.
*
* @remarks
* Not every execution context is associated with a frame. For example,
* {@link WebWorker | workers} have execution contexts that are not associated
* with frames.
*/
frame(): Frame | null {
return this._world ? this._world.frame() : null;
}

/**
* Evaluates the given function.
*
Expand Down
6 changes: 2 additions & 4 deletions src/common/util.ts
Expand Up @@ -25,7 +25,6 @@ import {ElementHandle} from './ElementHandle.js';
import {TimeoutError} from './Errors.js';
import {CommonEventEmitter} from './EventEmitter.js';
import {ExecutionContext} from './ExecutionContext.js';
import {Frame} from './Frame.js';
import {JSHandle} from './JSHandle.js';

/**
Expand Down Expand Up @@ -218,9 +217,8 @@ export function createJSHandle(
context: ExecutionContext,
remoteObject: Protocol.Runtime.RemoteObject
): JSHandle | ElementHandle<Node> {
const frame = context.frame();
if (remoteObject.subtype === 'node' && frame instanceof Frame) {
return new ElementHandle(context, remoteObject, frame);
if (remoteObject.subtype === 'node' && context._world) {
return new ElementHandle(context, remoteObject, context._world.frame());
}
return new JSHandle(context, remoteObject);
}
Expand Down
4 changes: 2 additions & 2 deletions test/src/ariaqueryhandler.spec.ts
Expand Up @@ -334,7 +334,7 @@ describeChromeOnly('AriaQueryHandler', () => {
await otherFrame!.evaluate(addElement, 'button');
await page.evaluate(addElement, 'button');
const elementHandle = await watchdog;
expect(elementHandle!.executionContext().frame()).toBe(page.mainFrame());
expect(elementHandle!.frame).toBe(page.mainFrame());
});

it('should run in specified frame', async () => {
Expand All @@ -350,7 +350,7 @@ describeChromeOnly('AriaQueryHandler', () => {
await frame1!.evaluate(addElement, 'button');
await frame2!.evaluate(addElement, 'button');
const elementHandle = await waitForSelectorPromise;
expect(elementHandle!.executionContext().frame()).toBe(frame2);
expect(elementHandle!.frame).toBe(frame2);
});

it('should throw when frame is detached', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/src/frame.spec.ts
Expand Up @@ -42,8 +42,8 @@ describe('Frame specs', function () {
expect(context1).toBeTruthy();
expect(context2).toBeTruthy();
expect(context1 !== context2).toBeTruthy();
expect(context1.frame()).toBe(frame1);
expect(context2.frame()).toBe(frame2);
expect(context1._world?.frame()).toBe(frame1);
expect(context2._world?.frame()).toBe(frame2);

await Promise.all([
context1.evaluate(() => {
Expand Down
6 changes: 3 additions & 3 deletions test/src/waittask.spec.ts
Expand Up @@ -477,7 +477,7 @@ describe('waittask specs', function () {
await otherFrame.evaluate(addElement, 'div');
await page.evaluate(addElement, 'div');
const eHandle = await watchdog;
expect(eHandle?.executionContext().frame()).toBe(page.mainFrame());
expect(eHandle?.frame).toBe(page.mainFrame());
}
);

Expand All @@ -492,7 +492,7 @@ describe('waittask specs', function () {
await frame1.evaluate(addElement, 'div');
await frame2.evaluate(addElement, 'div');
const eHandle = await waitForSelectorPromise;
expect(eHandle?.executionContext().frame()).toBe(frame2);
expect(eHandle?.frame).toBe(frame2);
});

itFailsFirefox('should throw when frame is detached', async () => {
Expand Down Expand Up @@ -749,7 +749,7 @@ describe('waittask specs', function () {
await frame1.evaluate(addElement, 'div');
await frame2.evaluate(addElement, 'div');
const eHandle = await waitForXPathPromise;
expect(eHandle?.executionContext().frame()).toBe(frame2);
expect(eHandle?.frame).toBe(frame2);
});
itFailsFirefox('should throw when frame is detached', async () => {
const {page, server} = getTestState();
Expand Down

0 comments on commit f0a7e18

Please sign in to comment.