Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: make execution context delegate public (#34894)

Yury Semikhatskye091baad79b2
diff --git a/packages/playwright-core/src/server/bidi/bidiPage.ts b/packages/playwright-core/src/server/bidi/bidiPage.ts
index 33e3f58931..13cb21e36f 100644
--- a/packages/playwright-core/src/server/bidi/bidiPage.ts
+++ b/packages/playwright-core/src/server/bidi/bidiPage.ts
@@ -142,7 +142,6 @@ export class BidiPage implements PageDelegate {
}
const delegate = new BidiExecutionContext(this._session, realmInfo);
const context = new dom.FrameExecutionContext(delegate, frame, worldName);
- (context as any)[contextDelegateSymbol] = delegate;
frame._contextCreated(worldName, context);
this._realmToContext.set(realmInfo.realm, context);
}
@@ -579,7 +578,5 @@ function addMainBinding(callback: (arg: any) => void) {
}
function toBidiExecutionContext(executionContext: dom.FrameExecutionContext): BidiExecutionContext {
- return (executionContext as any)[contextDelegateSymbol] as BidiExecutionContext;
+ return executionContext.delegate as BidiExecutionContext;
}
-
-const contextDelegateSymbol = Symbol('delegate');
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts
index 24aca2da9f..bc0d9e5e51 100644
--- a/packages/playwright-core/src/server/chromium/crPage.ts
+++ b/packages/playwright-core/src/server/chromium/crPage.ts
@@ -675,7 +675,6 @@ class FrameSession {
else if (contextPayload.name === UTILITY_WORLD_NAME)
worldName = 'utility';
const context = new dom.FrameExecutionContext(delegate, frame, worldName);
- (context as any)[contextDelegateSymbol] = delegate;
if (worldName)
frame._contextCreated(worldName, context);
this._contextIdToContext.set(contextPayload.id, context);
@@ -1163,7 +1162,7 @@ class FrameSession {
async _adoptBackendNodeId(backendNodeId: Protocol.DOM.BackendNodeId, to: dom.FrameExecutionContext): Promise<dom.ElementHandle> {
const result = await this._client._sendMayFail('DOM.resolveNode', {
backendNodeId,
- executionContextId: ((to as any)[contextDelegateSymbol] as CRExecutionContext)._contextId,
+ executionContextId: (to.delegate as CRExecutionContext)._contextId,
});
if (!result || result.object.subtype === 'null')
throw new Error(dom.kUnableToAdoptErrorMessage);
@@ -1196,8 +1195,6 @@ async function emulateTimezone(session: CRSession, timezoneId: string) {
}
}
-const contextDelegateSymbol = Symbol('delegate');
-
// Chromium reference: https://source.chromium.org/chromium/chromium/src/+/main:components/embedder_support/user_agent_utils.cc;l=434;drc=70a6711e08e9f9e0d8e4c48e9ba5cab62eb010c2
function calculateUserAgentMetadata(options: types.BrowserContextOptions) {
const ua = options.userAgent;
diff --git a/packages/playwright-core/src/server/firefox/ffPage.ts b/packages/playwright-core/src/server/firefox/ffPage.ts
index 16a0562f84..0ac7489c21 100644
--- a/packages/playwright-core/src/server/firefox/ffPage.ts
+++ b/packages/playwright-core/src/server/firefox/ffPage.ts
@@ -150,7 +150,6 @@ export class FFPage implements PageDelegate {
else if (!auxData.name)
worldName = 'main';
const context = new dom.FrameExecutionContext(delegate, frame, worldName);
- (context as any)[contextDelegateSymbol] = delegate;
if (worldName)
frame._contextCreated(worldName, context);
this._contextIdToContext.set(executionContextId, context);
@@ -527,7 +526,7 @@ export class FFPage implements PageDelegate {
const result = await this._session.send('Page.adoptNode', {
frameId: handle._context.frame._id,
objectId: handle._objectId,
- executionContextId: ((to as any)[contextDelegateSymbol] as FFExecutionContext)._executionContextId
+ executionContextId: (to.delegate as FFExecutionContext)._executionContextId
});
if (!result.remoteObject)
throw new Error(dom.kUnableToAdoptErrorMessage);
diff --git a/packages/playwright-core/src/server/javascript.ts b/packages/playwright-core/src/server/javascript.ts
index 74a94f1652..edb0d95bc9 100644
--- a/packages/playwright-core/src/server/javascript.ts
+++ b/packages/playwright-core/src/server/javascript.ts
@@ -56,7 +56,7 @@ export interface ExecutionContextDelegate {
}
export class ExecutionContext extends SdkObject {
- private _delegate: ExecutionContextDelegate;
+ readonly delegate: ExecutionContextDelegate;
private _utilityScriptPromise: Promise<JSHandle> | undefined;
private _contextDestroyedScope = new LongStandingScope();
readonly worldNameForTest: string;
@@ -64,7 +64,7 @@ export class ExecutionContext extends SdkObject {
constructor(parent: SdkObject, delegate: ExecutionContextDelegate, worldNameForTest: string) {
super(parent, 'execution-context');
this.worldNameForTest = worldNameForTest;
- this._delegate = delegate;
+ this.delegate = delegate;
}
contextDestroyed(reason: string) {
@@ -76,24 +76,24 @@ export class ExecutionContext extends SdkObject {
}
rawEvaluateJSON(expression: string): Promise<any> {
- return this._raceAgainstContextDestroyed(this._delegate.rawEvaluateJSON(expression));
+ return this._raceAgainstContextDestroyed(this.delegate.rawEvaluateJSON(expression));
}
rawEvaluateHandle(expression: string): Promise<ObjectId> {
- return this._raceAgainstContextDestroyed(this._delegate.rawEvaluateHandle(expression));
+ return this._raceAgainstContextDestroyed(this.delegate.rawEvaluateHandle(expression));
}
async evaluateWithArguments(expression: string, returnByValue: boolean, values: any[], objectIds: ObjectId[]): Promise<any> {
const utilityScript = await this._utilityScript();
- return this._raceAgainstContextDestroyed(this._delegate.evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds));
+ return this._raceAgainstContextDestroyed(this.delegate.evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds));
}
getProperties(object: JSHandle): Promise<Map<string, JSHandle>> {
- return this._raceAgainstContextDestroyed(this._delegate.getProperties(object));
+ return this._raceAgainstContextDestroyed(this.delegate.getProperties(object));
}
releaseHandle(objectId: ObjectId): Promise<void> {
- return this._delegate.releaseHandle(objectId);
+ return this.delegate.releaseHandle(objectId);
}
adoptIfNeeded(handle: JSHandle): Promise<JSHandle> | null {
@@ -108,7 +108,7 @@ export class ExecutionContext extends SdkObject {
${utilityScriptSource.source}
return new (module.exports.UtilityScript())(${isUnderTest()});
})();`;
- this._utilityScriptPromise = this._raceAgainstContextDestroyed(this._delegate.rawEvaluateHandle(source).then(objectId => new JSHandle(this, 'object', 'UtilityScript', objectId)));
+ this._utilityScriptPromise = this._raceAgainstContextDestroyed(this.delegate.rawEvaluateHandle(source).then(objectId => new JSHandle(this, 'object', 'UtilityScript', objectId)));
}
return this._utilityScriptPromise;
}
diff --git a/packages/playwright-core/src/server/webkit/wkPage.ts b/packages/playwright-core/src/server/webkit/wkPage.ts
index 0e57b0f6a9..9fae9d0002 100644
--- a/packages/playwright-core/src/server/webkit/wkPage.ts
+++ b/packages/playwright-core/src/server/webkit/wkPage.ts
@@ -496,7 +496,6 @@ export class WKPage implements PageDelegate {
else if (contextPayload.type === 'user' && contextPayload.name === UTILITY_WORLD_NAME)
worldName = 'utility';
const context = new dom.FrameExecutionContext(delegate, frame, worldName);
- (context as any)[contextDelegateSymbol] = delegate;
if (worldName)
frame._contextCreated(worldName, context);
this._contextIdToContext.set(contextPayload.id, context);
@@ -954,7 +953,7 @@ export class WKPage implements PageDelegate {
async adoptElementHandle<T extends Node>(handle: dom.ElementHandle<T>, to: dom.FrameExecutionContext): Promise<dom.ElementHandle<T>> {
const result = await this._session.sendMayFail('DOM.resolveNode', {
objectId: handle._objectId,
- executionContextId: ((to as any)[contextDelegateSymbol] as WKExecutionContext)._contextId
+ executionContextId: (to.delegate as WKExecutionContext)._contextId
});
if (!result || result.object.subtype === 'null')
throw new Error(dom.kUnableToAdoptErrorMessage);
@@ -978,7 +977,7 @@ export class WKPage implements PageDelegate {
const context = await parent._mainContext();
const result = await this._session.send('DOM.resolveNode', {
frameId: frame._id,
- executionContextId: ((context as any)[contextDelegateSymbol] as WKExecutionContext)._contextId
+ executionContextId: (context.delegate as WKExecutionContext)._contextId
});
if (!result || result.object.subtype === 'null')
throw new Error('Frame has been detached.');
@@ -1253,5 +1252,3 @@ function isLoadedSecurely(url: string, timing: network.ResourceTiming) {
return true;
} catch (_) {}
}
-
-const contextDelegateSymbol = Symbol('delegate');