Permalink
Browse files

Expose the socket debugger

  • Loading branch information...
1 parent ebd3d81 commit 01665e604f020320191c88a064d67e0a89e83b91 @gabebw gabebw committed Jul 22, 2011
@@ -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
@@ -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
View
@@ -789,4 +789,41 @@ def make_the_server_go_away
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
+ <html><body>
+ <div id="parent">
+ <div class="find">Expected</div>
+ </div>
+ <div class="find">Unexpected</div>
+ </body></html>
+ 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
@@ -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
-

0 comments on commit 01665e6

Please sign in to comment.