playwright/packages/playwright-core/src/server/dispatchers/electronDispatcher.ts

73 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-07-14 06:46:59 +02:00
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the 'License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-04-06 23:57:14 +02:00
import type { DispatcherScope } from './dispatcher';
import { Dispatcher } from './dispatcher';
import type { Electron } from '../electron/electron';
import { ElectronApplication } from '../electron/electron';
import type * as channels from '../../protocol/channels';
2020-07-14 06:46:59 +02:00
import { BrowserContextDispatcher } from './browserContextDispatcher';
2022-04-06 23:57:14 +02:00
import type { PageDispatcher } from './pageDispatcher';
import { parseArgument, serializeResult } from './jsHandleDispatcher';
import { ElementHandleDispatcher } from './elementHandlerDispatcher';
2020-07-14 06:46:59 +02:00
2021-11-18 00:26:01 +01:00
export class ElectronDispatcher extends Dispatcher<Electron, channels.ElectronChannel> implements channels.ElectronChannel {
_type_Electron = true;
2020-07-14 06:46:59 +02:00
constructor(scope: DispatcherScope, electron: Electron) {
super(scope, electron, 'Electron', {}, true);
2020-07-14 06:46:59 +02:00
}
async launch(params: channels.ElectronLaunchParams): Promise<channels.ElectronLaunchResult> {
2021-02-01 20:43:26 +01:00
const electronApplication = await this._object.launch(params);
return { electronApplication: new ElectronApplicationDispatcher(this._scope, electronApplication) };
2020-07-14 06:46:59 +02:00
}
}
2021-11-18 00:26:01 +01:00
export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, channels.ElectronApplicationChannel> implements channels.ElectronApplicationChannel {
_type_EventTarget = true;
_type_ElectronApplication = true;
2020-07-14 06:46:59 +02:00
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
super(scope, electronApplication, 'ElectronApplication', {
context: new BrowserContextDispatcher(scope, electronApplication.context())
}, true);
this.addObjectListener(ElectronApplication.Events.Close, () => {
this._dispatchEvent('close');
this._dispose();
2020-07-14 06:46:59 +02:00
});
}
async browserWindow(params: channels.ElectronApplicationBrowserWindowParams): Promise<channels.ElectronApplicationBrowserWindowResult> {
const handle = await this._object.browserWindow((params.page as PageDispatcher).page());
return { handle: ElementHandleDispatcher.fromJSHandle(this._scope, handle) };
2020-07-14 06:46:59 +02:00
}
async evaluateExpression(params: channels.ElectronApplicationEvaluateExpressionParams): Promise<channels.ElectronApplicationEvaluateExpressionResult> {
const handle = await this._object._nodeElectronHandlePromise;
return { value: serializeResult(await handle.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, true /* returnByValue */, parseArgument(params.arg))) };
2020-07-14 06:46:59 +02:00
}
async evaluateExpressionHandle(params: channels.ElectronApplicationEvaluateExpressionHandleParams): Promise<channels.ElectronApplicationEvaluateExpressionHandleResult> {
const handle = await this._object._nodeElectronHandlePromise;
const result = await handle.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, false /* returnByValue */, parseArgument(params.arg));
return { handle: ElementHandleDispatcher.fromJSHandle(this._scope, result) };
2020-07-14 06:46:59 +02:00
}
async close(): Promise<void> {
await this._object.close();
}
}