diff --git a/README.md b/README.md index 5a84952..1476b1b 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,10 @@ makes debugging easier). Running `rake autocompile` will watch the * Fix bug where we could end up interacting with an obsolete element. [Issue #30] +* Raise an suitable error if PhantomJS returns a non-zero exit status. + Previously a version error would be raised, indicating that the + PhantomJS version was too old when in fact it did not start at all. [Issue #23] + ### 0.4.0 ### * Element click position is now calculated using the native diff --git a/lib/capybara/poltergeist/client.rb b/lib/capybara/poltergeist/client.rb index f32c063..3dc188d 100644 --- a/lib/capybara/poltergeist/client.rb +++ b/lib/capybara/poltergeist/client.rb @@ -56,9 +56,13 @@ module Capybara::Poltergeist return if @phantomjs_version_checked version = `#{path} --version`.chomp - if version < PHANTOMJS_VERSION + + if $? != 0 + raise PhantomJSFailed.new($?) + elsif version < PHANTOMJS_VERSION raise PhantomJSTooOld.new(version) end + @phantomjs_version_checked = true end end diff --git a/lib/capybara/poltergeist/errors.rb b/lib/capybara/poltergeist/errors.rb index b622651..a693278 100644 --- a/lib/capybara/poltergeist/errors.rb +++ b/lib/capybara/poltergeist/errors.rb @@ -82,5 +82,19 @@ module Capybara "PhantomJS version #{version} is too old. You must use at least version #{Client::PHANTOMJS_VERSION}" end end + + class PhantomJSFailed < Error + attr_reader :status + + def initialize(status) + @status = status + end + + def message + "PhantomJS returned non-zero exit status #{status.exitstatus}. Ensure there is an X display available and " \ + "that DISPLAY is set. (See the Poltergeist README for details.) Make sure 'phantomjs --version' " \ + "runs successfully on your system." + end + end end end diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index ea1ee07..7b1f775 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -17,5 +17,16 @@ module Capybara::Poltergeist e.message.should include('1.3.0') end end + + it 'raises an error if phantomjs returns a non-zero exit code' do + subject = Client.new(6000, nil, 'exit 42 && ') + expect { subject.start }.to raise_error(Error) + + begin + subject.start + rescue PhantomJSFailed => e + e.message.should include('42') + end + end end end