Skip to content

Commit

Permalink
Expose the socket debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Jul 22, 2011
1 parent ebd3d81 commit 01665e6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 42 deletions.
1 change: 1 addition & 0 deletions lib/capybara/driver/webkit.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 43 additions & 0 deletions lib/capybara/driver/webkit/socket_debugger.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions spec/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 0 additions & 42 deletions spec/support/socket_debugger.rb

This file was deleted.

0 comments on commit 01665e6

Please sign in to comment.