UNPKG

1.94 kBJavaScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17const {assert} = require('./helper');
18
19class Dialog {
20 /**
21 * @param {!Puppeteer.CDPSession} client
22 * @param {string} type
23 * @param {string} message
24 * @param {(string|undefined)} defaultValue
25 */
26 constructor(client, type, message, defaultValue = '') {
27 this._client = client;
28 this._type = type;
29 this._message = message;
30 this._handled = false;
31 this._defaultValue = defaultValue;
32 }
33
34 /**
35 * @return {string}
36 */
37 type() {
38 return this._type;
39 }
40
41 /**
42 * @return {string}
43 */
44 message() {
45 return this._message;
46 }
47
48 /**
49 * @return {string}
50 */
51 defaultValue() {
52 return this._defaultValue;
53 }
54
55 /**
56 * @param {string=} promptText
57 */
58 async accept(promptText) {
59 assert(!this._handled, 'Cannot accept dialog which is already handled!');
60 this._handled = true;
61 await this._client.send('Page.handleJavaScriptDialog', {
62 accept: true,
63 promptText: promptText
64 });
65 }
66
67 async dismiss() {
68 assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
69 this._handled = true;
70 await this._client.send('Page.handleJavaScriptDialog', {
71 accept: false
72 });
73 }
74}
75
76Dialog.Type = {
77 Alert: 'alert',
78 BeforeUnload: 'beforeunload',
79 Confirm: 'confirm',
80 Prompt: 'prompt'
81};
82
83module.exports = {Dialog};