mirror of
https://github.com/thoughtbot/capybara-webkit
synced 2023-03-27 23:22:28 -04:00
Expose the socket debugger
This commit is contained in:
parent
ebd3d81759
commit
01665e604f
4 changed files with 81 additions and 42 deletions
|
@ -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
|
||||
|
|
43
lib/capybara/driver/webkit/socket_debugger.rb
Normal file
43
lib/capybara/driver/webkit/socket_debugger.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
<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
|
||||
|
Loading…
Add table
Reference in a new issue