Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Forward webkit_server's stdout to Ruby's stdout.

This allows console.log() messages to be visible, and prevents
large numbers of console.log() calls from filling up webkit_server's pipe
which would block the process.
  • Loading branch information...
commit dc49e5f87587a8f735beebca26ec5800c92fb88c 1 parent 82ae0c2
@FooBarWidget FooBarWidget authored
View
19 lib/capybara/driver/webkit/browser.rb
@@ -1,4 +1,5 @@
require 'socket'
+require 'thread'
require 'capybara/util/timeout'
require 'json'
@@ -8,6 +9,9 @@ class Browser
def initialize(options = {})
@socket_class = options[:socket_class] || TCPSocket
+ @stdout = options.has_key?(:stdout) ?
+ options[:stdout] :
+ $stdout
start_server
connect
end
@@ -79,6 +83,10 @@ def render(path, width, height)
def start_server
read_pipe, write_pipe = fork_server
@server_port = discover_server_port(read_pipe)
+ @stdout_thread = Thread.new do
+ Thread.current.abort_on_exception = true
+ forward_stdout(read_pipe)
+ end
end
def fork_server
@@ -100,6 +108,17 @@ def discover_server_port(read_pipe)
return unless IO.select([read_pipe], nil, nil, 10)
((read_pipe.first || '').match(/listening on port: (\d+)/) || [])[1].to_i
end
+
+ def forward_stdout(pipe)
+ while !pipe.eof?
+ line = pipe.readline
+ if @stdout
+ @stdout.write(line)
+ @stdout.flush
+ end
+ end
+ rescue EOFError
+ end
def connect
Capybara.timeout(5) do
View
9 spec/browser_spec.rb
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'stringio'
require 'capybara/driver/webkit/browser'
describe Capybara::Driver::Webkit::Browser do
@@ -22,5 +23,13 @@
new_browser.server_port.should_not == browser.server_port
end
end
+
+ it 'forwards stdout to the given IO object' do
+ io = StringIO.new
+ new_browser = Capybara::Driver::Webkit::Browser.new(:stdout => io)
+ new_browser.execute_script('console.log("hello world")')
+ sleep(0.5)
+ io.string.should == "hello world\n"
+ end
end
View
2  spec/spec_helper.rb
@@ -18,7 +18,7 @@
require File.join(spec_dir,"spec_helper")
require 'capybara/driver/webkit/browser'
-$webkit_browser = Capybara::Driver::Webkit::Browser.new(:socket_class => TCPSocket)
+$webkit_browser = Capybara::Driver::Webkit::Browser.new(:socket_class => TCPSocket, :stdout => nil)
Capybara.register_driver :reusable_webkit do |app|
Capybara::Driver::Webkit.new(app, :browser => $webkit_browser)
Please sign in to comment.
Something went wrong with that request. Please try again.