Plumb default prompt value to the dialog (#243)

This patch:
- plumbs default prompt value to the dialog
- enables one more phantom test
This commit is contained in:
Andrey Lushnikov 2017-08-11 12:17:43 -07:00 committed by GitHub
parent 53baed6af6
commit dbac6788ae
6 changed files with 38 additions and 4 deletions

View File

@ -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.

View File

@ -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}

View File

@ -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);
}

View File

@ -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());
}
}

View File

@ -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() {

View File

@ -1,4 +1,3 @@
//! unsupported
test(function () {
var page = require('webpage').create();