Skip to content

Commit

Permalink
Don't strip unless the result if non-nil; make the error message for …
Browse files Browse the repository at this point in the history
…this case specific.
  • Loading branch information
cmeiklejohn committed Jul 12, 2011
1 parent 9fe7301 commit a90e31d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/capybara/driver/webkit.rb
Expand Up @@ -3,7 +3,10 @@
require "capybara/driver/webkit/browser"

class Capybara::Driver::Webkit
class WebkitError < StandardError
class WebkitInvalidResponseError < StandardError
end

class WebkitNoResponseError < StandardError
end

attr_reader :browser
Expand Down
11 changes: 8 additions & 3 deletions lib/capybara/driver/webkit/browser.rb
Expand Up @@ -110,11 +110,16 @@ def attempt_connect
end

def check
result = @socket.gets.strip
result = @socket.gets
result.strip! if result

unless result == 'ok'
raise WebkitError, read_response
if result.nil?
raise WebkitNoResponseError, "No response received from the server."
elsif result != 'ok'
raise WebkitInvalidResponseError, read_response
end

result
end

def read_response
Expand Down
47 changes: 40 additions & 7 deletions spec/driver_spec.rb
Expand Up @@ -20,7 +20,7 @@
p_id = "greeting"
msg = "hello"
iframe = "<iframe id=\"f\" src=\"/?iframe=true\"></iframe>"
end
end
body = <<-HTML
<html>
<head>
Expand Down Expand Up @@ -56,12 +56,12 @@

it "raises error for missing frame by index" do
expect { subject.within_frame(1) { } }.
to raise_error(Capybara::Driver::Webkit::WebkitError)
to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError)
end

it "raise_error for missing frame by id" do
expect { subject.within_frame("foo") { } }.
to raise_error(Capybara::Driver::Webkit::WebkitError)
to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError)
end

it "returns an attribute's value" do
Expand Down Expand Up @@ -142,7 +142,7 @@

it "raises an error for an invalid xpath query" do
expect { subject.find("totally invalid salad") }.
to raise_error(Capybara::Driver::Webkit::WebkitError, /xpath/i)
to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError, /xpath/i)
end

it "returns an attribute's value" do
Expand Down Expand Up @@ -237,7 +237,7 @@

it "raises an error for failing Javascript" do
expect { subject.execute_script(%<invalid salad>) }.
to raise_error(Capybara::Driver::Webkit::WebkitError)
to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError)
end

it "doesn't raise an error for Javascript that doesn't return anything" do
Expand Down Expand Up @@ -626,7 +626,7 @@
wait_for_error_to_complete
subject.find("//body")
}.
to raise_error(Capybara::Driver::Webkit::WebkitError, %r{/error})
to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError, %r{/error})
end

def wait_for_error_to_complete
Expand Down Expand Up @@ -657,7 +657,7 @@ def wait_for_error_to_complete

it "raises a webkit error and then continues" do
subject.find("//input").first.click
expect { subject.find("//p") }.to raise_error(Capybara::Driver::Webkit::WebkitError)
expect { subject.find("//p") }.to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError)
subject.visit("/")
subject.find("//p").first.text.should == "hello"
end
Expand Down Expand Up @@ -740,4 +740,37 @@ def wait_for_error_to_complete
subject.find('id("accept")').first.text.should_not == 'text/html'
end
end

context "no response app" do
before(:all) do
@app = lambda do |env|
body = <<-HTML
<html><body>
<form action="/error"><input type="submit"/></form>
</body></html>
HTML
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
end

it "raises a webkit error for the requested url" do
expect {
subject.find("//input").first.click
make_the_server_go_away
subject.find("//body")
}.
to raise_error(Capybara::Driver::Webkit::WebkitNoResponseError, %r{response})
make_the_server_come_back
end

def make_the_server_come_back
subject.browser.instance_variable_get(:@socket).unstub!(:gets)
end

def make_the_server_go_away
subject.browser.instance_variable_get(:@socket).stub!(:gets).and_return(nil)
end
end
end

0 comments on commit a90e31d

Please sign in to comment.