1
0
Fork 0
mirror of https://github.com/teampoltergeist/poltergeist.git synced 2022-11-09 12:05:00 -05:00

Don't wait for frame indefinitely

This needs a better fix in Capybara, see
https://github.com/jnicklas/capybara/issues/984#issuecomment-14347198
This commit is contained in:
Jon Leighton 2013-03-03 14:04:39 +00:00
parent 5ba99efc53
commit 7e216ee0f5
8 changed files with 67 additions and 16 deletions

View file

@ -1,8 +1,14 @@
require "capybara/poltergeist/errors"
require 'json' require 'json'
require 'time' require 'time'
module Capybara::Poltergeist module Capybara::Poltergeist
class Browser class Browser
ERROR_MAPPINGS = {
"Poltergeist.JavascriptError" => JavascriptError,
"Poltergeist.FrameNotFound" => FrameNotFound
}
attr_reader :server, :client, :logger attr_reader :server, :client, :logger
def initialize(server, client, logger = nil) def initialize(server, client, logger = nil)
@ -193,14 +199,11 @@ module Capybara::Poltergeist
log json.inspect log json.inspect
if json['error'] if json['error']
if json['error']['name'] == 'Poltergeist.JavascriptError' klass = ERROR_MAPPINGS[json['error']['name']] || BrowserError
raise JavascriptError.new(json['error']) raise klass.new(json['error'])
else else
raise BrowserError.new(json['error'])
end
end
json['response'] json['response']
end
rescue DeadClient rescue DeadClient
restart restart
raise raise

View file

@ -145,16 +145,17 @@ class Poltergeist.Browser
@page.execute("function() { #{script} }") @page.execute("function() { #{script} }")
this.sendResponse(true) this.sendResponse(true)
push_frame: (name) -> push_frame: (name, timeout = new Date().getTime() + 2000) ->
if @page.pushFrame(name) if @page.pushFrame(name)
if @page.currentUrl() == 'about:blank' if @page.currentUrl() == 'about:blank'
this.setState 'awaiting_frame_load' this.setState 'awaiting_frame_load'
else else
this.sendResponse(true) this.sendResponse(true)
else else
# There's currently no PhantomJS callback available for frame creation, if new Date().getTime() < timeout
# so we have to poll setTimeout((=> this.push_frame(name, timeout)), 50)
setTimeout((=> this.push_frame(name)), 50) else
@owner.sendError(new Poltergeist.FrameNotFound(name))
pop_frame: -> pop_frame: ->
this.sendResponse(@page.popFrame()) this.sendResponse(@page.popFrame())

View file

@ -182,8 +182,11 @@ Poltergeist.Browser = (function() {
return this.sendResponse(true); return this.sendResponse(true);
}; };
Browser.prototype.push_frame = function(name) { Browser.prototype.push_frame = function(name, timeout) {
var _this = this; var _this = this;
if (timeout == null) {
timeout = new Date().getTime() + 2000;
}
if (this.page.pushFrame(name)) { if (this.page.pushFrame(name)) {
if (this.page.currentUrl() === 'about:blank') { if (this.page.currentUrl() === 'about:blank') {
return this.setState('awaiting_frame_load'); return this.setState('awaiting_frame_load');
@ -191,9 +194,13 @@ Poltergeist.Browser = (function() {
return this.sendResponse(true); return this.sendResponse(true);
} }
} else { } else {
if (new Date().getTime() < timeout) {
return setTimeout((function() { return setTimeout((function() {
return _this.push_frame(name); return _this.push_frame(name, timeout);
}), 50); }), 50);
} else {
return this.owner.sendError(new Poltergeist.FrameNotFound(name));
}
} }
}; };

View file

@ -86,6 +86,24 @@ Poltergeist.ObsoleteNode = (function(_super) {
})(Poltergeist.Error); })(Poltergeist.Error);
Poltergeist.FrameNotFound = (function(_super) {
__extends(FrameNotFound, _super);
function FrameNotFound(frameName) {
this.frameName = frameName;
}
FrameNotFound.prototype.name = "Poltergeist.FrameNotFound";
FrameNotFound.prototype.args = function() {
return [this.frameName];
};
return FrameNotFound;
})(Poltergeist.Error);
Poltergeist.ClickFailed = (function(_super) { Poltergeist.ClickFailed = (function(_super) {
__extends(ClickFailed, _super); __extends(ClickFailed, _super);

View file

@ -50,6 +50,11 @@ class Poltergeist.ObsoleteNode extends Poltergeist.Error
args: -> [] args: -> []
toString: -> this.name toString: -> this.name
class Poltergeist.FrameNotFound extends Poltergeist.Error
constructor: (@frameName) ->
name: "Poltergeist.FrameNotFound"
args: -> [@frameName]
class Poltergeist.ClickFailed extends Poltergeist.Error class Poltergeist.ClickFailed extends Poltergeist.Error
constructor: (@selector, @position) -> constructor: (@selector, @position) ->
name: "Poltergeist.ClickFailed" name: "Poltergeist.ClickFailed"

View file

@ -132,7 +132,6 @@ module Capybara::Poltergeist
end end
def within_frame(name, &block) def within_frame(name, &block)
raise NotImplementedError
browser.within_frame(name, &block) browser.within_frame(name, &block)
end end

View file

@ -54,6 +54,16 @@ module Capybara
end end
end end
class FrameNotFound < ClientError
def name
response['args'].first
end
def message
"The frame '#{name}' was not found."
end
end
class NodeError < ClientError class NodeError < ClientError
attr_reader :node attr_reader :node

View file

@ -410,6 +410,14 @@ describe Capybara::Session do
log.text.should == "one" log.text.should == "one"
end end
end end
it "doesn't wait forever for the frame to load" do
@session.visit '/'
expect {
@session.within_frame('omg') { }
}.to raise_error(Capybara::Poltergeist::FrameNotFound)
end
end end
# see https://github.com/jonleighton/poltergeist/issues/115 # see https://github.com/jonleighton/poltergeist/issues/115