mirror of
https://github.com/microsoft/playwright.git
synced 2024-09-20 16:58:47 +03:00
chore: reuse Dialog between browsers (#115)
This commit is contained in:
parent
8efd258e40
commit
76ab83f581
@ -24,7 +24,6 @@ import { TimeoutSettings } from '../TimeoutSettings';
|
||||
import { Browser } from './Browser';
|
||||
import { BrowserContext } from './BrowserContext';
|
||||
import { CDPSession, CDPSessionEvents } from './Connection';
|
||||
import { Dialog, DialogType } from './Dialog';
|
||||
import { EmulationManager } from './EmulationManager';
|
||||
import { Events } from './events';
|
||||
import { Accessibility } from './features/accessibility';
|
||||
@ -47,6 +46,7 @@ import * as dom from '../dom';
|
||||
import * as frames from '../frames';
|
||||
import * as js from '../javascript';
|
||||
import * as network from '../network';
|
||||
import * as dialog from '../dialog';
|
||||
import { DOMWorldDelegate } from './JSHandle';
|
||||
|
||||
const writeFileAsync = helper.promisify(fs.writeFile);
|
||||
@ -379,18 +379,13 @@ export class Page extends EventEmitter {
|
||||
}
|
||||
|
||||
_onDialog(event : Protocol.Page.javascriptDialogOpeningPayload) {
|
||||
let dialogType = null;
|
||||
if (event.type === 'alert')
|
||||
dialogType = DialogType.Alert;
|
||||
else if (event.type === 'confirm')
|
||||
dialogType = DialogType.Confirm;
|
||||
else if (event.type === 'prompt')
|
||||
dialogType = DialogType.Prompt;
|
||||
else if (event.type === 'beforeunload')
|
||||
dialogType = DialogType.BeforeUnload;
|
||||
assert(dialogType, 'Unknown javascript dialog type: ' + event.type);
|
||||
const dialog = new Dialog(this._client, dialogType, event.message, event.defaultPrompt);
|
||||
this.emit(Events.Page.Dialog, dialog);
|
||||
this.emit(Events.Page.Dialog, new dialog.Dialog(
|
||||
event.type as dialog.DialogType,
|
||||
event.message,
|
||||
async (accept: boolean, promptText?: string) => {
|
||||
await this._client.send('Page.handleJavaScriptDialog', { accept, promptText });
|
||||
},
|
||||
event.defaultPrompt));
|
||||
}
|
||||
|
||||
url(): string {
|
||||
|
@ -7,7 +7,7 @@ export { BrowserContext } from './BrowserContext';
|
||||
export { BrowserFetcher } from './BrowserFetcher';
|
||||
export { Chromium } from './features/chromium';
|
||||
export { CDPSession } from './Connection';
|
||||
export { Dialog } from './Dialog';
|
||||
export { Dialog } from '../dialog';
|
||||
export { ExecutionContext, JSHandle } from '../javascript';
|
||||
export { ElementHandle } from '../dom';
|
||||
export { Accessibility } from './features/accessibility';
|
||||
|
@ -15,21 +15,24 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { CDPSession } from './Connection';
|
||||
import { assert } from '../helper';
|
||||
import { assert } from './helper';
|
||||
|
||||
type OnHandle = (accept: boolean, promptText?: string) => Promise<void>;
|
||||
|
||||
export type DialogType = 'alert' | 'beforeunload' | 'confirm' | 'prompt';
|
||||
|
||||
export class Dialog {
|
||||
private _client: CDPSession;
|
||||
private _type: string;
|
||||
private _message: string;
|
||||
private _onHandle: OnHandle;
|
||||
private _handled = false;
|
||||
private _defaultValue: string;
|
||||
|
||||
constructor(client: CDPSession, type: string, message: string, defaultValue: (string | undefined) = '') {
|
||||
this._client = client;
|
||||
constructor(type: string, message: string, onHandle: OnHandle, defaultValue?: string) {
|
||||
this._type = type;
|
||||
this._message = message;
|
||||
this._defaultValue = defaultValue;
|
||||
this._onHandle = onHandle;
|
||||
this._defaultValue = defaultValue || '';
|
||||
}
|
||||
|
||||
type(): string {
|
||||
@ -47,24 +50,12 @@ export class Dialog {
|
||||
async accept(promptText: string | undefined) {
|
||||
assert(!this._handled, 'Cannot accept dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Page.handleJavaScriptDialog', {
|
||||
accept: true,
|
||||
promptText: promptText
|
||||
});
|
||||
await this._onHandle(true, promptText);
|
||||
}
|
||||
|
||||
async dismiss() {
|
||||
assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Page.handleJavaScriptDialog', {
|
||||
accept: false
|
||||
});
|
||||
await this._onHandle(false);
|
||||
}
|
||||
}
|
||||
|
||||
export const DialogType = {
|
||||
Alert: 'alert',
|
||||
BeforeUnload: 'beforeunload',
|
||||
Confirm: 'confirm',
|
||||
Prompt: 'prompt'
|
||||
};
|
@ -1,51 +0,0 @@
|
||||
import {assert, debugError} from '../helper';
|
||||
|
||||
export class Dialog {
|
||||
private _client: any;
|
||||
private _dialogId: any;
|
||||
private _type: string;
|
||||
private _message: string;
|
||||
private _handled: boolean;
|
||||
private _defaultValue: string;
|
||||
|
||||
constructor(client, payload) {
|
||||
this._client = client;
|
||||
this._dialogId = payload.dialogId;
|
||||
this._type = payload.type;
|
||||
this._message = payload.message;
|
||||
this._handled = false;
|
||||
this._defaultValue = payload.defaultValue || '';
|
||||
}
|
||||
|
||||
type(): string {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
message(): string {
|
||||
return this._message;
|
||||
}
|
||||
|
||||
defaultValue(): string {
|
||||
return this._defaultValue;
|
||||
}
|
||||
|
||||
async accept(promptText: string | undefined) {
|
||||
assert(!this._handled, 'Cannot accept dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Page.handleDialog', {
|
||||
dialogId: this._dialogId,
|
||||
accept: true,
|
||||
promptText: promptText
|
||||
}).catch(debugError);
|
||||
}
|
||||
|
||||
async dismiss() {
|
||||
assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Page.handleDialog', {
|
||||
dialogId: this._dialogId,
|
||||
accept: false
|
||||
}).catch(debugError);
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ import { assert, debugError, helper, RegisteredListener } from '../helper';
|
||||
import { TimeoutSettings } from '../TimeoutSettings';
|
||||
import { BrowserContext, Target } from './Browser';
|
||||
import { JugglerSession, JugglerSessionEvents } from './Connection';
|
||||
import { Dialog } from './Dialog';
|
||||
import { Events } from './events';
|
||||
import { Accessibility } from './features/accessibility';
|
||||
import { Interception } from './features/interception';
|
||||
@ -38,6 +37,7 @@ import * as js from '../javascript';
|
||||
import * as network from '../network';
|
||||
import * as frames from '../frames';
|
||||
import { toHandle, toPayload, deserializeValue } from './ExecutionContext';
|
||||
import * as dialog from '../dialog';
|
||||
|
||||
const writeFileAsync = helper.promisify(fs.writeFile);
|
||||
|
||||
@ -304,7 +304,13 @@ export class Page extends EventEmitter {
|
||||
}
|
||||
|
||||
_onDialogOpened(params) {
|
||||
this.emit(Events.Page.Dialog, new Dialog(this._session, params));
|
||||
this.emit(Events.Page.Dialog, new dialog.Dialog(
|
||||
params.type as dialog.DialogType,
|
||||
params.message,
|
||||
async (accept: boolean, promptText?: string) => {
|
||||
await this._session.send('Page.handleDialog', { dialogId: params.dialogId, accept, promptText }).catch(debugError);
|
||||
},
|
||||
params.defaultValue));
|
||||
}
|
||||
|
||||
mainFrame(): frames.Frame {
|
||||
|
@ -5,7 +5,7 @@ export { TimeoutError } from '../Errors';
|
||||
export { Keyboard, Mouse } from '../input';
|
||||
export { Browser, BrowserContext, Target } from './Browser';
|
||||
export { BrowserFetcher } from './BrowserFetcher';
|
||||
export { Dialog } from './Dialog';
|
||||
export { Dialog } from '../dialog';
|
||||
export { ExecutionContext, JSHandle } from '../javascript';
|
||||
export { Accessibility } from './features/accessibility';
|
||||
export { Interception } from './features/interception';
|
||||
|
@ -1,70 +0,0 @@
|
||||
/**
|
||||
* Copyright 2017 Google Inc. All rights reserved.
|
||||
* Modifications 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.
|
||||
*/
|
||||
|
||||
import { TargetSession } from './Connection';
|
||||
import { assert } from '../helper';
|
||||
|
||||
export class Dialog {
|
||||
private _client: TargetSession;
|
||||
private _type: string;
|
||||
private _message: string;
|
||||
private _handled = false;
|
||||
private _defaultValue: string;
|
||||
|
||||
constructor(client: TargetSession, type: string, message: string, defaultValue: (string | undefined) = '') {
|
||||
this._client = client;
|
||||
this._type = type;
|
||||
this._message = message;
|
||||
this._defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
type(): string {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
message(): string {
|
||||
return this._message;
|
||||
}
|
||||
|
||||
defaultValue(): string {
|
||||
return this._defaultValue;
|
||||
}
|
||||
|
||||
async accept(promptText: string | undefined) {
|
||||
assert(!this._handled, 'Cannot accept dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Dialog.handleJavaScriptDialog', {
|
||||
accept: true,
|
||||
promptText: promptText
|
||||
});
|
||||
}
|
||||
|
||||
async dismiss() {
|
||||
assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Dialog.handleJavaScriptDialog', {
|
||||
accept: false
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const DialogType = {
|
||||
Alert: 'alert',
|
||||
BeforeUnload: 'beforeunload',
|
||||
Confirm: 'confirm',
|
||||
Prompt: 'prompt'
|
||||
};
|
@ -38,7 +38,7 @@ import * as dom from '../dom';
|
||||
import * as frames from '../frames';
|
||||
import * as js from '../javascript';
|
||||
import * as network from '../network';
|
||||
import { Dialog, DialogType } from './Dialog';
|
||||
import * as dialog from '../dialog';
|
||||
|
||||
const writeFileAsync = helper.promisify(fs.writeFile);
|
||||
|
||||
@ -119,18 +119,13 @@ export class Page extends EventEmitter {
|
||||
}
|
||||
|
||||
_onDialog(event: Protocol.Dialog.javascriptDialogOpeningPayload) {
|
||||
let dialogType = null;
|
||||
if (event.type === 'alert')
|
||||
dialogType = DialogType.Alert;
|
||||
else if (event.type === 'confirm')
|
||||
dialogType = DialogType.Confirm;
|
||||
else if (event.type === 'prompt')
|
||||
dialogType = DialogType.Prompt;
|
||||
else if (event.type === 'beforeunload')
|
||||
dialogType = DialogType.BeforeUnload;
|
||||
assert(dialogType, 'Unknown javascript dialog type: ' + event.type);
|
||||
const dialog = new Dialog(this._session, dialogType, event.message, event.defaultPrompt);
|
||||
this.emit(Events.Page.Dialog, dialog);
|
||||
this.emit(Events.Page.Dialog, new dialog.Dialog(
|
||||
event.type as dialog.DialogType,
|
||||
event.message,
|
||||
async (accept: boolean, promptText?: string) => {
|
||||
await this._session.send('Dialog.handleJavaScriptDialog', { accept, promptText });
|
||||
},
|
||||
event.defaultPrompt));
|
||||
}
|
||||
|
||||
_setTarget(newTarget: Target) {
|
||||
|
@ -12,4 +12,4 @@ export { Request, Response } from '../network';
|
||||
export { ConsoleMessage, Page } from './Page';
|
||||
export { Playwright } from './Playwright';
|
||||
export { Target } from './Target';
|
||||
|
||||
export { Dialog } from '../dialog';
|
||||
|
Loading…
Reference in New Issue
Block a user