diff --git a/lib/capybara/driver/webkit.rb b/lib/capybara/driver/webkit.rb index 1405d65..2bdd223 100644 --- a/lib/capybara/driver/webkit.rb +++ b/lib/capybara/driver/webkit.rb @@ -1,6 +1,7 @@ require "capybara" require "capybara/driver/webkit/node" require "capybara/driver/webkit/browser" +require "capybara/driver/webkit/socket_debugger" class Capybara::Driver::Webkit class WebkitInvalidResponseError < StandardError diff --git a/lib/capybara/driver/webkit/socket_debugger.rb b/lib/capybara/driver/webkit/socket_debugger.rb new file mode 100644 index 0000000..e814b9d --- /dev/null +++ b/lib/capybara/driver/webkit/socket_debugger.rb @@ -0,0 +1,43 @@ +# Wraps the TCP socket and prints data sent and received. Used for debugging +# the wire protocol. You can use this by passing a :socket_class to Browser. +class Capybara::Driver::Webkit + class SocketDebugger + def self.open(host, port) + real_socket = TCPSocket.open(host, port) + new(real_socket) + end + + def initialize(socket) + @socket = socket + end + + def read(length) + received @socket.read(length) + end + + def puts(line) + sent line + @socket.puts(line) + end + + def print(content) + sent content + @socket.print(content) + end + + def gets + received @socket.gets + end + + private + + def sent(content) + Kernel.puts " >> " + content.to_s + end + + def received(content) + Kernel.puts " << " + content.to_s + content + end + end +end diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index e594c15..b815891 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -789,4 +789,41 @@ describe Capybara::Driver::Webkit do subject.browser.instance_variable_get(:@socket).stub!(:print) end end + + context "with socket debugger" do + let(:socket_debugger_class){ Capybara::Driver::Webkit::SocketDebugger } + let(:browser_with_debugger){ + Capybara::Driver::Webkit::Browser.new(:socket_class => socket_debugger_class) + } + let(:driver_with_debugger){ Capybara::Driver::Webkit.new(@app, :browser => browser_with_debugger) } + + before(:all) do + @app = lambda do |env| + body = <<-HTML + +
+
Expected
+
+
Unexpected
+ + HTML + [200, + { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, + [body]] + end + end + + it "prints out sent content" do + socket_debugger_class.any_instance.stub(:received){|content| content } + sent_content = ['Find', 1, 17, "//*[@id='parent']"] + socket_debugger_class.any_instance.should_receive(:sent).exactly(sent_content.size).times + driver_with_debugger.find("//*[@id='parent']") + end + + it "prints out received content" do + socket_debugger_class.any_instance.stub(:sent) + socket_debugger_class.any_instance.should_receive(:received).at_least(:once).and_return("ok") + driver_with_debugger.find("//*[@id='parent']") + end + end end diff --git a/spec/support/socket_debugger.rb b/spec/support/socket_debugger.rb deleted file mode 100644 index 77e8e8b..0000000 --- a/spec/support/socket_debugger.rb +++ /dev/null @@ -1,42 +0,0 @@ -# Wraps the TCP socket and prints data sent and received. Used for debugging -# the wire protocol. You can use this by passing a :socket_class to Browser. -class SocketDebugger - def self.open(host, port) - real_socket = TCPSocket.open(host, port) - new(real_socket) - end - - def initialize(socket) - @socket = socket - end - - def read(length) - received @socket.read(length) - end - - def puts(line) - sent line - @socket.puts(line) - end - - def print(content) - sent content - @socket.print(content) - end - - def gets - received @socket.gets - end - - private - - def sent(content) - Kernel.puts " >> " + content.to_s - end - - def received(content) - Kernel.puts " << " + content.to_s - content - end -end -