mirror of
https://github.com/teampoltergeist/poltergeist.git
synced 2022-11-09 12:05:00 -05:00
Revert "Revert "Add alert(), confirm(), prompt()
implementation""
This reverts commit c5cd330062
.
This commit is contained in:
parent
2f13856097
commit
d619600d78
11 changed files with 174 additions and 29 deletions
|
@ -22,6 +22,9 @@
|
|||
* Write JSON to the logger, rather than Ruby [Issue #430]
|
||||
* Added ability to access all of a nodes attributes (Jon Rowe)
|
||||
* Capybara 2.3 window support (Dmitry Vorotilin)
|
||||
* Added ability to manipulate modals (`window.alert()`, `window.confirm()`
|
||||
and `window.prompt()`) that has been implemented from Capybara 2.4
|
||||
(Wataru MIYAGUNI)
|
||||
* Added ability to clear all cookies with clear_cookies method (unmanbearpig)
|
||||
* Move from `phantom.args` to `system.args` to support PhantomJS 2.0
|
||||
(Filip Spiridonov) [Issue 566]
|
||||
|
|
|
@ -349,6 +349,32 @@ module Capybara::Poltergeist
|
|||
command 'go_forward'
|
||||
end
|
||||
|
||||
def accept_confirm
|
||||
command 'set_confirm_process', true
|
||||
end
|
||||
|
||||
def dismiss_confirm
|
||||
command 'set_confirm_process', false
|
||||
end
|
||||
|
||||
#
|
||||
# press "OK" with text (response) or default value
|
||||
#
|
||||
def accept_prompt(response)
|
||||
command 'set_prompt_response', response || false
|
||||
end
|
||||
|
||||
#
|
||||
# press "Cancel"
|
||||
#
|
||||
def dismiss_prompt
|
||||
command 'set_prompt_response', nil
|
||||
end
|
||||
|
||||
def modal_messages
|
||||
command 'modal_messages'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(message)
|
||||
|
|
|
@ -397,6 +397,3 @@ document.addEventListener(
|
|||
'DOMContentLoaded',
|
||||
-> console.log('__DOMContentLoaded')
|
||||
)
|
||||
|
||||
window.confirm = (message) -> true
|
||||
window.prompt = (message, _default) -> _default or null
|
||||
|
|
|
@ -7,6 +7,10 @@ class Poltergeist.Browser
|
|||
@_debug = false
|
||||
@_counter = 0
|
||||
|
||||
@processed_modal_messages = []
|
||||
@confirm_processes = []
|
||||
@prompt_responses = []
|
||||
|
||||
this.resetPage()
|
||||
|
||||
resetPage: ->
|
||||
|
@ -23,6 +27,24 @@ class Poltergeist.Browser
|
|||
@page.handle = "#{@_counter++}"
|
||||
@pages.push(@page)
|
||||
|
||||
@page.onAlert = (msg) =>
|
||||
@setModalMessage msg
|
||||
|
||||
@page.onConfirm = (msg) =>
|
||||
process = @confirm_processes.shift()
|
||||
process = true if process == undefined
|
||||
|
||||
@setModalMessage msg
|
||||
return process
|
||||
|
||||
@page.onPrompt = (msg, defaultVal) =>
|
||||
defaultVal ||= ''
|
||||
response = @prompt_responses.shift()
|
||||
response = defaultVal if (response == undefined || response == false)
|
||||
|
||||
@setModalMessage msg
|
||||
return response
|
||||
|
||||
@page.onPageCreated = (newPage) =>
|
||||
page = new Poltergeist.WebPage(newPage)
|
||||
page.handle = "#{@_counter++}"
|
||||
|
@ -39,6 +61,9 @@ class Poltergeist.Browser
|
|||
if @_debug
|
||||
console.log "poltergeist [#{new Date().getTime()}] #{message}"
|
||||
|
||||
setModalMessage: (msg) ->
|
||||
@processed_modal_messages.push(msg)
|
||||
|
||||
sendResponse: (response) ->
|
||||
errors = @currentPage.errors
|
||||
@currentPage.clearErrors()
|
||||
|
@ -430,3 +455,15 @@ class Poltergeist.Browser
|
|||
set_url_blacklist: ->
|
||||
@currentPage.urlBlacklist = Array.prototype.slice.call(arguments)
|
||||
@sendResponse(true)
|
||||
|
||||
set_confirm_process: (process) ->
|
||||
@confirm_processes.push process
|
||||
@sendResponse(true)
|
||||
|
||||
set_prompt_response: (response) ->
|
||||
@prompt_responses.push response
|
||||
@sendResponse(true)
|
||||
|
||||
modal_messages: ->
|
||||
@sendResponse(@processed_modal_messages)
|
||||
@processed_modal_messages = []
|
||||
|
|
|
@ -540,11 +540,3 @@ window.__poltergeist = new PoltergeistAgent;
|
|||
document.addEventListener('DOMContentLoaded', function() {
|
||||
return console.log('__DOMContentLoaded');
|
||||
});
|
||||
|
||||
window.confirm = function(message) {
|
||||
return true;
|
||||
};
|
||||
|
||||
window.prompt = function(message, _default) {
|
||||
return _default || null;
|
||||
};
|
||||
|
|
|
@ -9,6 +9,9 @@ Poltergeist.Browser = (function() {
|
|||
this.js_errors = true;
|
||||
this._debug = false;
|
||||
this._counter = 0;
|
||||
this.processed_modal_messages = [];
|
||||
this.confirm_processes = [];
|
||||
this.prompt_responses = [];
|
||||
this.resetPage();
|
||||
}
|
||||
|
||||
|
@ -31,14 +34,35 @@ Poltergeist.Browser = (function() {
|
|||
});
|
||||
this.page.handle = "" + (this._counter++);
|
||||
this.pages.push(this.page);
|
||||
return this.page.onPageCreated = (function(_this) {
|
||||
return function(newPage) {
|
||||
var page;
|
||||
page = new Poltergeist.WebPage(newPage);
|
||||
page.handle = "" + (_this._counter++);
|
||||
return _this.pages.push(page);
|
||||
};
|
||||
})(this);
|
||||
|
||||
this.page.onAlert = function(msg) {
|
||||
return _this.setModalMessage(msg);
|
||||
};
|
||||
this.page.onConfirm = function(msg) {
|
||||
var process;
|
||||
process = _this.confirm_processes.shift();
|
||||
if (process === void 0) {
|
||||
process = true;
|
||||
}
|
||||
_this.setModalMessage(msg);
|
||||
return process;
|
||||
};
|
||||
this.page.onPrompt = function(msg, defaultVal) {
|
||||
var response;
|
||||
defaultVal || (defaultVal = '');
|
||||
response = _this.prompt_responses.shift();
|
||||
if (response === void 0 || response === false) {
|
||||
response = defaultVal;
|
||||
}
|
||||
_this.setModalMessage(msg);
|
||||
return response;
|
||||
};
|
||||
return this.page.onPageCreated = function(newPage) {
|
||||
var page;
|
||||
page = new Poltergeist.WebPage(newPage);
|
||||
page.handle = "" + (_this._counter++);
|
||||
return _this.pages.push(page);
|
||||
};
|
||||
};
|
||||
|
||||
Browser.prototype.getPageByHandle = function(handle) {
|
||||
|
@ -58,6 +82,10 @@ Poltergeist.Browser = (function() {
|
|||
}
|
||||
};
|
||||
|
||||
Browser.prototype.setModalMessage = function(msg) {
|
||||
return this.processed_modal_messages.push(msg);
|
||||
};
|
||||
|
||||
Browser.prototype.sendResponse = function(response) {
|
||||
var errors;
|
||||
errors = this.currentPage.errors;
|
||||
|
@ -589,6 +617,21 @@ Poltergeist.Browser = (function() {
|
|||
return this.sendResponse(true);
|
||||
};
|
||||
|
||||
Browser.prototype.set_confirm_process = function(process) {
|
||||
this.confirm_processes.push(process);
|
||||
return this.sendResponse(true);
|
||||
};
|
||||
|
||||
Browser.prototype.set_prompt_response = function(response) {
|
||||
this.prompt_responses.push(response);
|
||||
return this.sendResponse(true);
|
||||
};
|
||||
|
||||
Browser.prototype.modal_messages = function() {
|
||||
this.sendResponse(this.processed_modal_messages);
|
||||
return this.processed_modal_messages = [];
|
||||
};
|
||||
|
||||
return Browser;
|
||||
|
||||
})();
|
||||
|
|
|
@ -4,7 +4,7 @@ var __slice = [].slice,
|
|||
Poltergeist.WebPage = (function() {
|
||||
var command, delegate, _fn, _fn1, _i, _j, _len, _len1, _ref, _ref1;
|
||||
|
||||
WebPage.CALLBACKS = ['onAlert', 'onConsoleMessage', 'onLoadFinished', 'onInitialized', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onError', 'onNavigationRequested', 'onUrlChanged', 'onPageCreated', 'onClosing'];
|
||||
WebPage.CALLBACKS = ['onAlert', 'onConfirm', 'onPrompt', 'onConsoleMessage', 'onLoadFinished', 'onInitialized', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onError', 'onNavigationRequested', 'onUrlChanged', 'onPageCreated', 'onClosing'];
|
||||
|
||||
WebPage.DELEGATES = ['open', 'sendEvent', 'uploadFile', 'release', 'render', 'renderBase64', 'goBack', 'goForward'];
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
class Poltergeist.WebPage
|
||||
@CALLBACKS = ['onAlert', 'onConsoleMessage', 'onLoadFinished',
|
||||
'onInitialized', 'onLoadStarted', 'onResourceRequested',
|
||||
'onResourceReceived', 'onError', 'onNavigationRequested',
|
||||
'onUrlChanged', 'onPageCreated', 'onClosing']
|
||||
@CALLBACKS = ['onAlert', 'onConfirm', 'onPrompt', 'onConsoleMessage',
|
||||
'onLoadFinished', 'onInitialized', 'onLoadStarted',
|
||||
'onResourceRequested', 'onResourceReceived', 'onError',
|
||||
'onNavigationRequested', 'onUrlChanged', 'onPageCreated',
|
||||
'onClosing']
|
||||
|
||||
@DELEGATES = ['open', 'sendEvent', 'uploadFile', 'release', 'render',
|
||||
'renderBase64', 'goBack', 'goForward']
|
||||
|
|
|
@ -304,5 +304,51 @@ module Capybara::Poltergeist
|
|||
def go_forward
|
||||
browser.go_forward
|
||||
end
|
||||
|
||||
def accept_modal(type, options = {})
|
||||
case type
|
||||
when :confirm
|
||||
browser.accept_confirm
|
||||
when :prompt
|
||||
browser.accept_prompt options[:with]
|
||||
end
|
||||
|
||||
yield if block_given?
|
||||
find_modal(options)
|
||||
end
|
||||
|
||||
def dismiss_modal(type, options = {})
|
||||
case type
|
||||
when :confirm
|
||||
browser.dismiss_confirm
|
||||
when :prompt
|
||||
browser.dismiss_prompt
|
||||
end
|
||||
|
||||
yield if block_given?
|
||||
find_modal(options)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_modal(options)
|
||||
start_time = Time.now
|
||||
timeout_sec = options[:wait] || Capybara.default_wait_time
|
||||
expect_text = options[:text]
|
||||
not_found_msg = 'Unable to find modal dialog'
|
||||
not_found_msg += " with #{expect_text}" if expect_text
|
||||
|
||||
begin
|
||||
modals = browser.modal_messages
|
||||
raise Capybara::ModalNotFound if modals.empty?
|
||||
raise Capybara::ModalNotFound if (expect_text && !modals.include?(expect_text))
|
||||
rescue Capybara::ModalNotFound => e
|
||||
raise e, not_found_msg if (Time.now - start_time) >= timeout_sec
|
||||
sleep(0.05)
|
||||
retry
|
||||
end
|
||||
|
||||
modals.first
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
skip = [:modals]
|
||||
skip << :windows if ENV['TRAVIS']
|
||||
Capybara::SpecHelper.run_specs TestSessions::Poltergeist, 'Poltergeist', capybara_skip: skip
|
||||
Capybara::SpecHelper.run_specs TestSessions::Poltergeist, 'Poltergeist'
|
||||
|
||||
describe Capybara::Session do
|
||||
context 'with poltergeist driver' do
|
||||
|
|
|
@ -64,7 +64,9 @@ RSpec.configure do |config|
|
|||
Poltergeist::SpecHelper.set_capybara_wait_time(0)
|
||||
end
|
||||
|
||||
config.before(:each, :requires => :js) do
|
||||
Poltergeist::SpecHelper.set_capybara_wait_time(1)
|
||||
[:js, :modals].each do |cond|
|
||||
config.before(:each, :requires => cond) do
|
||||
Poltergeist::SpecHelper.set_capybara_wait_time(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue