diff --git a/docs/api.md b/docs/api.md index 9b5c9fc5bec..460bab04f2e 100644 --- a/docs/api.md +++ b/docs/api.md @@ -76,6 +76,7 @@ + [tracing.stop()](#tracingstop) * [class: Dialog](#class-dialog) + [dialog.accept([promptText])](#dialogacceptprompttext) + + [dialog.defaultValue()](#dialogdefaultvalue) + [dialog.dismiss()](#dialogdismiss) + [dialog.message()](#dialogmessage) + [dialog.type](#dialogtype) @@ -901,6 +902,9 @@ browser.newPage().then(async page => { - `promptText` <[string]> A text to enter in prompt. Does not cause any effects if the dialog's `type` is not prompt. - returns: <[Promise]> Promise which resolves when the dialog has being accepted. +#### dialog.defaultValue() +- returns: <[string]> If dialog is prompt, returns default prompt value. Otherwise, returns empty string. + #### dialog.dismiss() - returns: <[Promise]> Promise which resolves when the dialog has being dismissed. diff --git a/lib/Dialog.js b/lib/Dialog.js index 0ad4b58c6cf..33cdb1fd95c 100644 --- a/lib/Dialog.js +++ b/lib/Dialog.js @@ -21,12 +21,14 @@ class Dialog { * @param {!Session} client * @param {!Dialog.Type} type * @param {string} message + * @param {(string|undefined)} defaultValue */ - constructor(client, type, message) { + constructor(client, type, message, defaultValue) { this._client = client; this.type = type; this._message = message; this._handled = false; + this._defaultValue = defaultValue || ''; } /** @@ -36,6 +38,13 @@ class Dialog { return this._message; } + /** + * @return {string} + */ + defaultValue() { + return this._defaultValue; + } + /** * @param {string=} promptText * @return {!Promise} diff --git a/lib/Page.js b/lib/Page.js index 75b58ebdd13..dad2e4ed536 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -237,7 +237,7 @@ class Page extends EventEmitter { else if (event.type === 'beforeunload') dialogType = Dialog.Type.BeforeUnload; console.assert(dialogType, 'Unknown javascript dialog type: ' + event.type); - let dialog = new Dialog(this._client, dialogType, event.message); + let dialog = new Dialog(this._client, dialogType, event.message, event.defaultPrompt); this.emit(Page.Events.Dialog, dialog); } diff --git a/phantom_shim/WebPage.js b/phantom_shim/WebPage.js index 596cfa54b60..4a87c28fcfa 100644 --- a/phantom_shim/WebPage.js +++ b/phantom_shim/WebPage.js @@ -58,6 +58,7 @@ class WebPage { this._onResourceRequestedCallback = undefined; this._onConfirmCallback = undefined; + this._onPromptCallback = undefined; this._onAlertCallback = undefined; this._onError = noop; @@ -351,6 +352,22 @@ class WebPage { this._onConfirmCallback = handler; } + /** + * @return {(function()|undefined)} + */ + get onPrompt() { + return this._onPromptCallback; + } + + /** + * @param {function()} handler + */ + set onPrompt(handler) { + if (typeof handler !== 'function') + handler = undefined; + this._onPromptCallback = handler; + } + /** * @return {(function()|undefined)} */ @@ -377,6 +394,9 @@ class WebPage { } else if (dialog.type === 'confirm' && this._onConfirmCallback) { let result = this._onConfirmCallback.call(null, dialog.message()); await(result ? dialog.accept() : dialog.dismiss()); + } else if (dialog.type === 'prompt' && this._onPromptCallback) { + let result = this._onPromptCallback.call(null, dialog.message(), dialog.defaultValue()); + await(result ? dialog.accept(result) : dialog.dismiss()); } } diff --git a/test/test.js b/test/test.js index 2e933e8b2dc..635081ddeb4 100644 --- a/test/test.js +++ b/test/test.js @@ -818,6 +818,7 @@ describe('Page', function() { it('should fire', SX(async function() { page.on('dialog', dialog => { expect(dialog.type).toBe('alert'); + expect(dialog.defaultValue()).toBe(''); expect(dialog.message()).toBe('yo'); dialog.accept(); }); @@ -826,10 +827,11 @@ describe('Page', function() { it('should allow accepting prompts', SX(async function() { page.on('dialog', dialog => { expect(dialog.type).toBe('prompt'); + expect(dialog.defaultValue()).toBe('yes.'); expect(dialog.message()).toBe('question?'); dialog.accept('answer!'); }); - let result = await page.evaluate(() => prompt('question?')); + let result = await page.evaluate(() => prompt('question?', 'yes.')); expect(result).toBe('answer!'); })); it('should dismiss the prompt', SX(async function() { diff --git a/third_party/phantomjs/test/module/webpage/prompt.js b/third_party/phantomjs/test/module/webpage/prompt.js index 2826acf3cd2..d1f099e4ada 100644 --- a/third_party/phantomjs/test/module/webpage/prompt.js +++ b/third_party/phantomjs/test/module/webpage/prompt.js @@ -1,4 +1,3 @@ -//! unsupported test(function () { var page = require('webpage').create();