Revert "chore(eval): merge internal evaluate functions (#7517)" (#7535)

This reverts commit 7a5ef0d157.
This commit is contained in:
Max Schmitt 2021-07-09 16:19:42 +02:00 committed by GitHub
parent efb21b9e9f
commit 894ff15e65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 102 additions and 89 deletions

View file

@ -67,22 +67,11 @@ export class FrameDispatcher extends Dispatcher<Frame, channels.FrameInitializer
} }
async evaluateExpression(params: channels.FrameEvaluateExpressionParams, metadata: CallMetadata): Promise<channels.FrameEvaluateExpressionResult> { async evaluateExpression(params: channels.FrameEvaluateExpressionParams, metadata: CallMetadata): Promise<channels.FrameEvaluateExpressionResult> {
return { value: serializeResult(await this._frame.eval(params.expression, { return { value: serializeResult(await this._frame.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, parseArgument(params.arg), 'main')) };
arg: parseArgument(params.arg),
isFunction: params.isFunction,
waitForSignals: true,
world: 'main',
}))};
} }
async evaluateExpressionHandle(params: channels.FrameEvaluateExpressionHandleParams, metadata: CallMetadata): Promise<channels.FrameEvaluateExpressionHandleResult> { async evaluateExpressionHandle(params: channels.FrameEvaluateExpressionHandleParams, metadata: CallMetadata): Promise<channels.FrameEvaluateExpressionHandleResult> {
return { handle: ElementHandleDispatcher.fromJSHandle(this._scope, await this._frame.eval(params.expression, { return { handle: ElementHandleDispatcher.fromJSHandle(this._scope, await this._frame.evaluateExpressionHandleAndWaitForSignals(params.expression, params.isFunction, parseArgument(params.arg), 'main')) };
arg: parseArgument(params.arg),
isFunction: params.isFunction,
returnHandle: true,
waitForSignals: true,
world: 'main',
}))};
} }
async waitForSelector(params: channels.FrameWaitForSelectorParams, metadata: CallMetadata): Promise<channels.FrameWaitForSelectorResult> { async waitForSelector(params: channels.FrameWaitForSelectorParams, metadata: CallMetadata): Promise<channels.FrameWaitForSelectorResult> {

View file

@ -337,9 +337,9 @@ export abstract class BrowserContext extends SdkObject {
const originStorage: types.OriginStorage = { origin, localStorage: [] }; const originStorage: types.OriginStorage = { origin, localStorage: [] };
const frame = page.mainFrame(); const frame = page.mainFrame();
await frame.goto(internalMetadata, origin); await frame.goto(internalMetadata, origin);
const storage = await frame.eval(`({ const storage = await frame.evaluateExpression(`({
localStorage: Object.keys(localStorage).map(name => ({ name, value: localStorage.getItem(name) })), localStorage: Object.keys(localStorage).map(name => ({ name, value: localStorage.getItem(name) })),
})`, {world: 'utility'}); })`, false, undefined, 'utility');
originStorage.localStorage = storage.localStorage; originStorage.localStorage = storage.localStorage;
if (storage.localStorage.length) if (storage.localStorage.length)
result.origins.push(originStorage); result.origins.push(originStorage);
@ -361,10 +361,11 @@ export abstract class BrowserContext extends SdkObject {
for (const originState of state.origins) { for (const originState of state.origins) {
const frame = page.mainFrame(); const frame = page.mainFrame();
await frame.goto(metadata, originState.origin); await frame.goto(metadata, originState.origin);
await frame.eval((originState: types.OriginStorage) => { await frame.evaluateExpression(`
for (const { name, value } of (originState.localStorage || [])) originState => {
localStorage.setItem(name, value); for (const { name, value } of (originState.localStorage || []))
}, {arg: originState, world: 'utility'}); localStorage.setItem(name, value);
}`, true, originState, 'utility');
} }
await page.close(internalMetadata); await page.close(internalMetadata);
} }

View file

@ -171,7 +171,7 @@ export class CRPage implements PageDelegate {
async exposeBinding(binding: PageBinding) { async exposeBinding(binding: PageBinding) {
await this._forAllFrameSessions(frame => frame._initBinding(binding)); await this._forAllFrameSessions(frame => frame._initBinding(binding));
await Promise.all(this._page.frames().map(frame => frame.eval(binding.source, {world: binding.world}).catch(e => {}))); await Promise.all(this._page.frames().map(frame => frame.evaluateExpression(binding.source, false, {}, binding.world).catch(e => {})));
} }
async updateExtraHTTPHeaders(): Promise<void> { async updateExtraHTTPHeaders(): Promise<void> {
@ -473,9 +473,9 @@ class FrameSession {
worldName: UTILITY_WORLD_NAME, worldName: UTILITY_WORLD_NAME,
}); });
for (const binding of this._crPage._browserContext._pageBindings.values()) for (const binding of this._crPage._browserContext._pageBindings.values())
frame.eval(binding.source, {world: binding.world}).catch(e => {}); frame.evaluateExpression(binding.source, false, undefined, binding.world).catch(e => {});
for (const source of this._crPage._browserContext._evaluateOnNewDocumentSources) for (const source of this._crPage._browserContext._evaluateOnNewDocumentSources)
frame.eval(source).catch(e => {}); frame.evaluateExpression(source, false, undefined, 'main').catch(e => {});
} }
const isInitialEmptyPage = this._isMainFrame() && this._page.mainFrame().url() === ':'; const isInitialEmptyPage = this._isMainFrame() && this._page.mainFrame().url() === ':';
if (isInitialEmptyPage) { if (isInitialEmptyPage) {

View file

@ -50,6 +50,36 @@ export class FrameExecutionContext extends js.ExecutionContext {
return null; return null;
} }
async evaluate<Arg, R>(pageFunction: js.Func1<Arg, R>, arg?: Arg): Promise<R> {
return js.evaluate(this, true /* returnByValue */, pageFunction, arg);
}
async evaluateHandle<Arg, R>(pageFunction: js.Func1<Arg, R>, arg?: Arg): Promise<js.SmartHandle<R>> {
return js.evaluate(this, false /* returnByValue */, pageFunction, arg);
}
async evaluateExpression(expression: string, isFunction: boolean | undefined, arg?: any): Promise<any> {
return js.evaluateExpression(this, true /* returnByValue */, expression, isFunction, arg);
}
async evaluateAndWaitForSignals<Arg, R>(pageFunction: js.Func1<Arg, R>, arg?: Arg): Promise<R> {
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
return this.evaluate(pageFunction, arg);
});
}
async evaluateExpressionAndWaitForSignals(expression: string, isFunction: boolean | undefined, arg?: any): Promise<any> {
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
return this.evaluateExpression(expression, isFunction, arg);
});
}
async evaluateExpressionHandleAndWaitForSignals(expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
return js.evaluateExpression(this, false /* returnByValue */, expression, isFunction, arg);
});
}
createHandle(remoteObject: js.RemoteObject): js.JSHandle { createHandle(remoteObject: js.RemoteObject): js.JSHandle {
if (this.frame._page._delegate.isElementHandle(remoteObject)) if (this.frame._page._delegate.isElementHandle(remoteObject))
return new ElementHandle(this, remoteObject.objectId!); return new ElementHandle(this, remoteObject.objectId!);
@ -105,7 +135,7 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
private async _evaluateInMainAndWaitForSignals<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> { private async _evaluateInMainAndWaitForSignals<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {
const main = await this._context.frame._mainContext(); const main = await this._context.frame._mainContext();
return main.eval(pageFunction, {arg: [await main.injectedScript(), this, arg], waitForSignals: true}); return main.evaluateAndWaitForSignals(pageFunction, [await main.injectedScript(), this, arg]);
} }
async evaluateInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> { async evaluateInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {

View file

@ -68,7 +68,7 @@ export class ElectronApplication extends SdkObject {
this._nodeSession.on('Runtime.executionContextCreated', async (event: any) => { this._nodeSession.on('Runtime.executionContextCreated', async (event: any) => {
if (event.context.auxData && event.context.auxData.isDefault) { if (event.context.auxData && event.context.auxData.isDefault) {
this._nodeExecutionContext = new js.ExecutionContext(this, new CRExecutionContext(this._nodeSession, event.context)); this._nodeExecutionContext = new js.ExecutionContext(this, new CRExecutionContext(this._nodeSession, event.context));
f(this._nodeExecutionContext.eval(`process.mainModule.require('electron')`, { returnHandle: true })); f(await js.evaluate(this._nodeExecutionContext, false /* returnByValue */, `process.mainModule.require('electron')`));
} }
}); });
}); });

View file

@ -508,7 +508,7 @@ export class Frame extends SdkObject {
this._nonStallingEvaluations.add(callback); this._nonStallingEvaluations.add(callback);
try { try {
return await Promise.race([ return await Promise.race([
context.eval(expression, { isFunction }), context.evaluateExpression(expression, isFunction),
frameInvalidated frameInvalidated
]); ]);
} finally { } finally {
@ -658,10 +658,25 @@ export class Frame extends SdkObject {
return this._context('utility'); return this._context('utility');
} }
async eval(pageFunction: string|Function, options: js.EvalOptions & {world?: types.World} = {}) { async evaluateExpressionHandleAndWaitForSignals(expression: string, isFunction: boolean | undefined, arg: any, world: types.World = 'main'): Promise<any> {
const {world = 'main'} = options;
const context = await this._context(world); const context = await this._context(world);
const value = await context.eval(pageFunction, options); const handle = await context.evaluateExpressionHandleAndWaitForSignals(expression, isFunction, arg);
if (world === 'main')
await this._page._doSlowMo();
return handle;
}
async evaluateExpression(expression: string, isFunction: boolean | undefined, arg: any, world: types.World = 'main'): Promise<any> {
const context = await this._context(world);
const value = await context.evaluateExpression(expression, isFunction, arg);
if (world === 'main')
await this._page._doSlowMo();
return value;
}
async evaluateExpressionAndWaitForSignals(expression: string, isFunction: boolean | undefined, arg: any, world: types.World = 'main'): Promise<any> {
const context = await this._context(world);
const value = await context.evaluateExpressionAndWaitForSignals(expression, isFunction, arg);
if (world === 'main') if (world === 'main')
await this._page._doSlowMo(); await this._page._doSlowMo();
return value; return value;

View file

@ -52,14 +52,6 @@ export interface ExecutionContextDelegate {
releaseHandle(objectId: ObjectId): Promise<void>; releaseHandle(objectId: ObjectId): Promise<void>;
} }
export type EvalOptions = {
arg?: any;
args?: any[];
returnHandle?: boolean;
isFunction?: boolean;
waitForSignals?: boolean;
};
export class ExecutionContext extends SdkObject { export class ExecutionContext extends SdkObject {
readonly _delegate: ExecutionContextDelegate; readonly _delegate: ExecutionContextDelegate;
private _utilityScriptPromise: Promise<JSHandle> | undefined; private _utilityScriptPromise: Promise<JSHandle> | undefined;
@ -97,27 +89,6 @@ export class ExecutionContext extends SdkObject {
return await this._delegate.rawEvaluateJSON(expression); return await this._delegate.rawEvaluateJSON(expression);
} }
async eval(pageFunction: Function|string, {
arg,
args = [arg],
returnHandle = false,
isFunction = typeof pageFunction === 'function',
waitForSignals = false
}: EvalOptions): Promise<any> {
const action = () => evaluateExpression(this, !returnHandle, String(pageFunction), isFunction, ...args);
if (waitForSignals)
return this.waitForSignalsCreatedBy(action);
return action();
}
async evaluate<Arg, R>(pageFunction: Func1<Arg, R>, arg?: Arg): Promise<R> {
return this.eval(pageFunction, {arg});
}
async evaluateHandle<Arg, R>(pageFunction: Func1<Arg, R>, arg?: Arg): Promise<SmartHandle<R>> {
return this.eval(pageFunction, {arg, returnHandle: true});
}
async doSlowMo() { async doSlowMo() {
// overrided in FrameExecutionContext // overrided in FrameExecutionContext
} }
@ -148,15 +119,15 @@ export class JSHandle<T = any> extends SdkObject {
} }
async evaluate<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg?: Arg): Promise<R> { async evaluate<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg?: Arg): Promise<R> {
return this._context.eval(pageFunction, { args: [this, arg]}); return evaluate(this._context, true /* returnByValue */, pageFunction, this, arg);
} }
async evaluateHandle<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg?: Arg): Promise<SmartHandle<R>> { async evaluateHandle<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg?: Arg): Promise<SmartHandle<R>> {
return this._context.eval(pageFunction, { returnHandle: true, args: [this, arg]}); return evaluate(this._context, false /* returnByValue */, pageFunction, this, arg);
} }
async evaluateExpressionAndWaitForSignals(expression: string, isFunction: boolean | undefined, returnByValue: boolean, arg: any) { async evaluateExpressionAndWaitForSignals(expression: string, isFunction: boolean | undefined, returnByValue: boolean, arg: any) {
const value = await this._context.eval(expression, {isFunction, returnHandle: !returnByValue, args: [this, arg], waitForSignals: true}); const value = await evaluateExpressionAndWaitForSignals(this._context, returnByValue, expression, isFunction, this, arg);
await this._context.doSlowMo(); await this._context.doSlowMo();
return value; return value;
} }
@ -218,7 +189,11 @@ export class JSHandle<T = any> extends SdkObject {
} }
} }
async function evaluateExpression(context: ExecutionContext, returnByValue: boolean, expression: string, isFunction: boolean | undefined, ...args: any[]): Promise<any> { export async function evaluate(context: ExecutionContext, returnByValue: boolean, pageFunction: Function | string, ...args: any[]): Promise<any> {
return evaluateExpression(context, returnByValue, String(pageFunction), typeof pageFunction === 'function', ...args);
}
export async function evaluateExpression(context: ExecutionContext, returnByValue: boolean, expression: string, isFunction: boolean | undefined, ...args: any[]): Promise<any> {
const utilityScript = await context.utilityScript(); const utilityScript = await context.utilityScript();
expression = normalizeEvaluationExpression(expression, isFunction); expression = normalizeEvaluationExpression(expression, isFunction);
const handles: (Promise<JSHandle>)[] = []; const handles: (Promise<JSHandle>)[] = [];
@ -261,6 +236,10 @@ async function evaluateExpression(context: ExecutionContext, returnByValue: bool
} }
} }
export async function evaluateExpressionAndWaitForSignals(context: ExecutionContext, returnByValue: boolean, expression: string, isFunction?: boolean, ...args: any[]): Promise<any> {
return await context.waitForSignalsCreatedBy(() => evaluateExpression(context, returnByValue, expression, isFunction, ...args));
}
export function parseUnserializableValue(unserializableValue: string): any { export function parseUnserializableValue(unserializableValue: string): any {
if (unserializableValue === 'NaN') if (unserializableValue === 'NaN')
return NaN; return NaN;

View file

@ -537,11 +537,11 @@ export class Worker extends SdkObject {
} }
async evaluateExpression(expression: string, isFunction: boolean | undefined, arg: any): Promise<any> { async evaluateExpression(expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
return (await this._executionContextPromise).eval(expression, {isFunction, arg}); return js.evaluateExpression(await this._executionContextPromise, true /* returnByValue */, expression, isFunction, arg);
} }
async evaluateExpressionHandle(expression: string, isFunction: boolean | undefined, arg: any): Promise<any> { async evaluateExpressionHandle(expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
return (await this._executionContextPromise).eval(expression, {isFunction, arg, returnHandle: true}); return js.evaluateExpression(await this._executionContextPromise, false /* returnByValue */, expression, isFunction, arg);
} }
} }

View file

@ -82,12 +82,12 @@ export class HarTracer {
private _onDOMContentLoaded(page: Page) { private _onDOMContentLoaded(page: Page) {
const pageEntry = this._ensurePageEntry(page); const pageEntry = this._ensurePageEntry(page);
const promise = page.mainFrame().eval(() => { const promise = page.mainFrame().evaluateExpression(String(() => {
return { return {
title: document.title, title: document.title,
domContentLoaded: performance.timing.domContentLoadedEventStart, domContentLoaded: performance.timing.domContentLoadedEventStart,
}; };
}, {world: 'utility'}).then(result => { }), true, undefined, 'utility').then(result => {
pageEntry.title = result.title; pageEntry.title = result.title;
pageEntry.pageTimings.onContentLoad = result.domContentLoaded; pageEntry.pageTimings.onContentLoad = result.domContentLoaded;
}).catch(() => {}); }).catch(() => {});
@ -96,12 +96,12 @@ export class HarTracer {
private _onLoad(page: Page) { private _onLoad(page: Page) {
const pageEntry = this._ensurePageEntry(page); const pageEntry = this._ensurePageEntry(page);
const promise = page.mainFrame().eval(() => { const promise = page.mainFrame().evaluateExpression(String(() => {
return { return {
title: document.title, title: document.title,
loaded: performance.timing.loadEventStart, loaded: performance.timing.loadEventStart,
}; };
}, {world: 'utility'}).then(result => { }), true, undefined, 'utility').then(result => {
pageEntry.title = result.title; pageEntry.title = result.title;
pageEntry.pageTimings.onLoad = result.loaded; pageEntry.pageTimings.onLoad = result.loaded;
}).catch(() => {}); }).catch(() => {});

View file

@ -122,27 +122,27 @@ export class RecorderApp extends EventEmitter {
} }
async setMode(mode: 'none' | 'recording' | 'inspecting'): Promise<void> { async setMode(mode: 'none' | 'recording' | 'inspecting'): Promise<void> {
await this._page.mainFrame().eval((mode: Mode) => { await this._page.mainFrame().evaluateExpression(((mode: Mode) => {
window.playwrightSetMode(mode); window.playwrightSetMode(mode);
}, {arg: mode}).catch(() => {}); }).toString(), true, mode, 'main').catch(() => {});
} }
async setFile(file: string): Promise<void> { async setFile(file: string): Promise<void> {
await this._page.mainFrame().eval((file: string) => { await this._page.mainFrame().evaluateExpression(((file: string) => {
window.playwrightSetFile(file); window.playwrightSetFile(file);
}, {arg: file}).catch(() => {}); }).toString(), true, file, 'main').catch(() => {});
} }
async setPaused(paused: boolean): Promise<void> { async setPaused(paused: boolean): Promise<void> {
await this._page.mainFrame().eval((paused: boolean) => { await this._page.mainFrame().evaluateExpression(((paused: boolean) => {
window.playwrightSetPaused(paused); window.playwrightSetPaused(paused);
}, {arg: paused}).catch(() => {}); }).toString(), true, paused, 'main').catch(() => {});
} }
async setSources(sources: Source[]): Promise<void> { async setSources(sources: Source[]): Promise<void> {
await this._page.mainFrame().eval((sources: Source[]) => { await this._page.mainFrame().evaluateExpression(((sources: Source[]) => {
window.playwrightSetSources(sources); window.playwrightSetSources(sources);
}, {arg: sources}).catch(() => {}); }).toString(), true, sources, 'main').catch(() => {});
// Testing harness for runCLI mode. // Testing harness for runCLI mode.
{ {
@ -155,15 +155,15 @@ export class RecorderApp extends EventEmitter {
} }
async setSelector(selector: string, focus?: boolean): Promise<void> { async setSelector(selector: string, focus?: boolean): Promise<void> {
await this._page.mainFrame().eval((selector: string, focus?: boolean) => { await this._page.mainFrame().evaluateExpression(((arg: any) => {
window.playwrightSetSelector(selector, focus); window.playwrightSetSelector(arg.selector, arg.focus);
}, {args: [selector, focus]}).catch(() => {}); }).toString(), true, { selector, focus }, 'main').catch(() => {});
} }
async updateCallLogs(callLogs: CallLog[]): Promise<void> { async updateCallLogs(callLogs: CallLog[]): Promise<void> {
await this._page.mainFrame().eval((callLogs: CallLog[]) => { await this._page.mainFrame().evaluateExpression(((callLogs: CallLog[]) => {
window.playwrightUpdateLogs(callLogs); window.playwrightUpdateLogs(callLogs);
}, {arg: callLogs}).catch(() => {}); }).toString(), true, callLogs, 'main').catch(() => {});
} }
async bringToFront() { async bringToFront() {

View file

@ -249,7 +249,7 @@ export class RecorderSupplement implements InstrumentationListener {
private _refreshOverlay() { private _refreshOverlay() {
for (const page of this._context.pages()) for (const page of this._context.pages())
page.mainFrame().eval('window._playwrightRefreshOverlay()').catch(() => {}); page.mainFrame().evaluateExpression('window._playwrightRefreshOverlay()', false, undefined, 'main').catch(() => {});
} }
private async _onPage(page: Page) { private async _onPage(page: Page) {

View file

@ -191,7 +191,7 @@ export class WKPage implements PageDelegate {
const bootstrapScript = this._calculateBootstrapScript(world); const bootstrapScript = this._calculateBootstrapScript(world);
if (bootstrapScript.length) if (bootstrapScript.length)
promises.push(session.send('Page.setBootstrapScript', { source: bootstrapScript, worldName: webkitWorldName(world) })); promises.push(session.send('Page.setBootstrapScript', { source: bootstrapScript, worldName: webkitWorldName(world) }));
this._page.frames().map(frame => frame.eval(bootstrapScript, {world}).catch(e => {})); this._page.frames().map(frame => frame.evaluateExpression(bootstrapScript, false, undefined, world).catch(e => {}));
} }
if (contextOptions.bypassCSP) if (contextOptions.bypassCSP)
promises.push(session.send('Page.setBypassCSP', { enabled: true })); promises.push(session.send('Page.setBypassCSP', { enabled: true }));
@ -723,7 +723,7 @@ export class WKPage implements PageDelegate {
private async _evaluateBindingScript(binding: PageBinding): Promise<void> { private async _evaluateBindingScript(binding: PageBinding): Promise<void> {
const script = this._bindingToScript(binding); const script = this._bindingToScript(binding);
await Promise.all(this._page.frames().map(frame => frame.eval(script, {world: binding.world}).catch(e => {}))); await Promise.all(this._page.frames().map(frame => frame.evaluateExpression(script, false, {}, binding.world).catch(e => {})));
} }
async evaluateOnNewDocument(script: string): Promise<void> { async evaluateOnNewDocument(script: string): Promise<void> {

View file

@ -15,7 +15,6 @@
* limitations under the License. * limitations under the License.
*/ */
import { JSHandle } from '..';
import { contextTest as it, expect } from './config/browserTest'; import { contextTest as it, expect } from './config/browserTest';
it('expose binding should work', async ({context}) => { it('expose binding should work', async ({context}) => {
@ -74,7 +73,7 @@ it('should be callable from-inside addInitScript', async ({context, server}) =>
}); });
it('exposeBindingHandle should work', async ({context}) => { it('exposeBindingHandle should work', async ({context}) => {
let target: JSHandle; let target;
await context.exposeBinding('logme', (source, t) => { await context.exposeBinding('logme', (source, t) => {
target = t; target = t;
return 17; return 17;

View file

@ -250,22 +250,22 @@ it('should work with internal bindings', async ({page, toImpl, server, mode, bro
foo = arg; foo = arg;
}, 'utility'); }, 'utility');
expect(await page.evaluate('!!window.foo')).toBe(false); expect(await page.evaluate('!!window.foo')).toBe(false);
expect(await implPage.mainFrame().eval('!!window.foo', {world: 'utility'})).toBe(true); expect(await implPage.mainFrame().evaluateExpression('!!window.foo', false, {}, 'utility')).toBe(true);
expect(foo).toBe(undefined); expect(foo).toBe(undefined);
await implPage.mainFrame().eval('window.foo(123)', {world: 'utility'}); await implPage.mainFrame().evaluateExpression('window.foo(123)', false, {}, 'utility');
expect(foo).toBe(123); expect(foo).toBe(123);
// should work after reload // should work after reload
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
expect(await page.evaluate('!!window.foo')).toBe(false); expect(await page.evaluate('!!window.foo')).toBe(false);
await implPage.mainFrame().eval('window.foo(456)', {world: 'utility'}); await implPage.mainFrame().evaluateExpression('window.foo(456)', false, {}, 'utility');
expect(foo).toBe(456); expect(foo).toBe(456);
// should work inside frames // should work inside frames
const frame = await attachFrame(page, 'myframe', server.CROSS_PROCESS_PREFIX + '/empty.html'); const frame = await attachFrame(page, 'myframe', server.CROSS_PROCESS_PREFIX + '/empty.html');
expect(await frame.evaluate('!!window.foo')).toBe(false); expect(await frame.evaluate('!!window.foo')).toBe(false);
const implFrame: import('../../src/server/frames').Frame = toImpl(frame); const implFrame: import('../../src/server/frames').Frame = toImpl(frame);
await implFrame.eval('window.foo(789)', {world: 'utility'}); await implFrame.evaluateExpression('window.foo(789)', false, {}, 'utility');
expect(foo).toBe(789); expect(foo).toBe(789);
}); });