Dialog.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. const {assert} = require('./helper');
  17. class Dialog {
  18. /**
  19. * @param {!Puppeteer.CDPSession} client
  20. * @param {string} type
  21. * @param {string} message
  22. * @param {(string|undefined)} defaultValue
  23. */
  24. constructor(client, type, message, defaultValue = '') {
  25. this._client = client;
  26. this._type = type;
  27. this._message = message;
  28. this._handled = false;
  29. this._defaultValue = defaultValue;
  30. }
  31. /**
  32. * @return {string}
  33. */
  34. type() {
  35. return this._type;
  36. }
  37. /**
  38. * @return {string}
  39. */
  40. message() {
  41. return this._message;
  42. }
  43. /**
  44. * @return {string}
  45. */
  46. defaultValue() {
  47. return this._defaultValue;
  48. }
  49. /**
  50. * @param {string=} promptText
  51. */
  52. async accept(promptText) {
  53. assert(!this._handled, 'Cannot accept dialog which is already handled!');
  54. this._handled = true;
  55. await this._client.send('Page.handleJavaScriptDialog', {
  56. accept: true,
  57. promptText: promptText
  58. });
  59. }
  60. async dismiss() {
  61. assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
  62. this._handled = true;
  63. await this._client.send('Page.handleJavaScriptDialog', {
  64. accept: false
  65. });
  66. }
  67. }
  68. Dialog.Type = {
  69. Alert: 'alert',
  70. BeforeUnload: 'beforeunload',
  71. Confirm: 'confirm',
  72. Prompt: 'prompt'
  73. };
  74. module.exports = {Dialog};