Skip to content

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

chore: follow up to the execution context changes (#104)

Pavel Feldman1a1732407f29
diff --git a/.npmignore b/.npmignore
index 39b253454b..f333d1d5f8 100644
--- a/.npmignore
+++ b/.npmignore
@@ -15,6 +15,7 @@ src
node_modules
.local-chromium
.local-browser
+.local-webkit
.dev_profile*
.DS_Store
*.swp
@@ -44,4 +45,4 @@ tsconfig.json
/index.d.ts
# install.js only does stuff for development
-/install.js
\ No newline at end of file
+/install.js
diff --git a/src/chromium/FrameManager.ts b/src/chromium/FrameManager.ts
index ebfe4014af..68fcb937f8 100644
--- a/src/chromium/FrameManager.ts
+++ b/src/chromium/FrameManager.ts
@@ -16,17 +16,17 @@
*/
import { EventEmitter } from 'events';
+import * as frames from '../frames';
import { assert, debugError } from '../helper';
+import * as js from '../javascript';
import { TimeoutSettings } from '../TimeoutSettings';
import { CDPSession } from './Connection';
-import { EVALUATION_SCRIPT_URL, ExecutionContextDelegate, ExecutionContext, JSHandle, toRemoteObject } from './ExecutionContext';
-import * as frames from '../frames';
-import * as js from '../javascript';
+import { EVALUATION_SCRIPT_URL, ExecutionContext, ExecutionContextDelegate, toRemoteObject } from './ExecutionContext';
+import { ElementHandle } from './JSHandle';
import { LifecycleWatcher } from './LifecycleWatcher';
import { NetworkManager, Response } from './NetworkManager';
import { Page } from './Page';
import { Protocol } from './protocol';
-import { ElementHandle, createJSHandle } from './JSHandle';
const UTILITY_WORLD_NAME = '__playwright_utility_world__';
diff --git a/src/firefox/FrameManager.ts b/src/firefox/FrameManager.ts
index da2c6e51c8..ab46a3f2a0 100644
--- a/src/firefox/FrameManager.ts
+++ b/src/firefox/FrameManager.ts
@@ -15,17 +15,17 @@
* limitations under the License.
*/
-import { JugglerSession } from './Connection';
-import { Page } from './Page';
-import {RegisteredListener, helper, assert} from '../helper';
-import {TimeoutError} from '../Errors';
-import {EventEmitter} from 'events';
-import { JSHandle, ExecutionContext, ExecutionContextDelegate } from './ExecutionContext';
-import {NavigationWatchdog, NextNavigationWatchdog} from './NavigationWatchdog';
-import { ElementHandle } from './JSHandle';
-import { TimeoutSettings } from '../TimeoutSettings';
+import { EventEmitter } from 'events';
+import { TimeoutError } from '../Errors';
import * as frames from '../frames';
+import { assert, helper, RegisteredListener } from '../helper';
import * as js from '../javascript';
+import { TimeoutSettings } from '../TimeoutSettings';
+import { JugglerSession } from './Connection';
+import { ExecutionContext, ExecutionContextDelegate } from './ExecutionContext';
+import { ElementHandle } from './JSHandle';
+import { NavigationWatchdog, NextNavigationWatchdog } from './NavigationWatchdog';
+import { Page } from './Page';
export const FrameManagerEvents = {
FrameNavigated: Symbol('FrameManagerEvents.FrameNavigated'),
diff --git a/src/webkit/ExecutionContext.ts b/src/webkit/ExecutionContext.ts
index 31ad9cc403..09fa3f5015 100644
--- a/src/webkit/ExecutionContext.ts
+++ b/src/webkit/ExecutionContext.ts
@@ -31,7 +31,7 @@ export type JSHandle = js.JSHandle<ElementHandle>;
export class ExecutionContextDelegate implements js.ExecutionContextDelegate<ElementHandle> {
private _globalObjectId?: string;
_session: TargetSession;
- private _contextId: number;
+ _contextId: number;
private _contextDestroyedCallback: () => void;
private _executionContextDestroyedPromise: Promise<unknown>;
@@ -157,7 +157,7 @@ export class ExecutionContextDelegate implements js.ExecutionContextDelegate<Ele
functionDeclaration: functionText + '\n' + suffix + '\n',
// TODO(yurys): support executionContextId in WebKit
objectId: thisObjectId,
- arguments: serializableArgs.map(convertArgument.bind(this)),
+ arguments: serializableArgs.map((arg: any) => this._convertArgument(arg)),
returnByValue: false,
emulateUserGesture: true
});
@@ -217,21 +217,6 @@ export class ExecutionContextDelegate implements js.ExecutionContextDelegate<Ele
return valueFromRemoteObject(response.result);
}).catch(rewriteError);
- function convertArgument(this: ExecutionContext, arg: JSHandle | any) : Protocol.Runtime.CallArgument{
- const objectHandle = arg && (arg instanceof js.JSHandle) ? arg : null;
- if (objectHandle) {
- if (objectHandle._context !== this)
- throw new Error('JSHandles can be evaluated only in the context they were created!');
- if (objectHandle._disposed)
- throw new Error('JSHandle is disposed!');
- const remoteObject = toRemoteObject(arg);
- if (!remoteObject.objectId)
- return { value: valueFromRemoteObject(remoteObject) };
- return { objectId: remoteObject.objectId };
- }
- return { value: arg };
- }
-
function unserializableToString(arg) {
if (Object.is(arg, -0))
return '-0';
@@ -332,6 +317,22 @@ export class ExecutionContextDelegate implements js.ExecutionContextDelegate<Ele
}
return 'JSHandle:' + valueFromRemoteObject(object);
}
+
+ private _convertArgument(arg: JSHandle | any) : Protocol.Runtime.CallArgument {
+ const objectHandle = arg && (arg instanceof js.JSHandle) ? arg : null;
+ if (objectHandle) {
+ if (objectHandle._context._delegate !== this)
+ throw new Error('JSHandles can be evaluated only in the context they were created!');
+ if (objectHandle._disposed)
+ throw new Error('JSHandle is disposed!');
+ const remoteObject = toRemoteObject(arg);
+ if (!remoteObject.objectId)
+ return { value: valueFromRemoteObject(remoteObject) };
+ return { objectId: remoteObject.objectId };
+ }
+ return { value: arg };
+ }
+
}
const remoteObjectSymbol = Symbol('RemoteObject');
diff --git a/src/webkit/FrameManager.ts b/src/webkit/FrameManager.ts
index a6fa88e9b0..9bdb425984 100644
--- a/src/webkit/FrameManager.ts
+++ b/src/webkit/FrameManager.ts
@@ -17,17 +17,17 @@
import * as EventEmitter from 'events';
import { TimeoutError } from '../Errors';
-import { Events } from './events';
+import * as frames from '../frames';
import { assert, debugError, helper, RegisteredListener } from '../helper';
+import * as js from '../javascript';
import { TimeoutSettings } from '../TimeoutSettings';
import { TargetSession } from './Connection';
-import { JSHandle, ExecutionContext, ExecutionContextDelegate } from './ExecutionContext';
+import { Events } from './events';
+import { ExecutionContext, ExecutionContextDelegate } from './ExecutionContext';
import { ElementHandle } from './JSHandle';
import { NetworkManager, NetworkManagerEvents, Request, Response } from './NetworkManager';
import { Page } from './Page';
import { Protocol } from './protocol';
-import * as frames from '../frames';
-import * as js from '../javascript';
export const FrameManagerEvents = {
FrameNavigatedWithinDocument: Symbol('FrameNavigatedWithinDocument'),
@@ -178,6 +178,7 @@ export class FrameManager extends EventEmitter implements frames.FrameDelegate<E
const data = this._frameData(frame);
this._frames.delete(data.id);
data.id = framePayload.id;
+ this._frames.set(data.id, frame);
}
} else if (isMainFrame) {
// Initial frame navigation.
@@ -199,7 +200,9 @@ export class FrameManager extends EventEmitter implements frames.FrameDelegate<E
frame._navigated(framePayload.url, framePayload.name);
for (const context of this._contextIdToContext.values()) {
if (context.frame() === frame) {
- (context._delegate as ExecutionContextDelegate)._dispose();
+ const delegate = context._delegate as ExecutionContextDelegate;
+ delegate._dispose();
+ this._contextIdToContext.delete(delegate._contextId);
frame._contextDestroyed(context);
}
}
@@ -223,6 +226,8 @@ export class FrameManager extends EventEmitter implements frames.FrameDelegate<E
}
_onExecutionContextCreated(contextPayload : Protocol.Runtime.ExecutionContextDescription) {
+ if (this._contextIdToContext.has(contextPayload.id))
+ return;
if (!contextPayload.isPageContext)
return;
const frameId = contextPayload.frameId;