From 8820275188e004740cccc8039793bd264f154a6a Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Fri, 5 Jun 2015 16:34:04 -0400 Subject: [PATCH] Attempt to handle crashes * Restart server process after a command crashes the process * Inform the user where they can get help --- lib/capybara/webkit/browser.rb | 14 +++++++++++++- lib/capybara/webkit/connection.rb | 6 ++++++ lib/capybara/webkit/errors.rb | 3 +++ spec/driver_spec.rb | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/capybara/webkit/browser.rb b/lib/capybara/webkit/browser.rb index edeca48..dc14533 100644 --- a/lib/capybara/webkit/browser.rb +++ b/lib/capybara/webkit/browser.rb @@ -1,4 +1,5 @@ -require 'json' +require "json" +require "capybara/webkit/errors" module Capybara::Webkit class Browser @@ -209,6 +210,17 @@ module Capybara::Webkit end check read_response + rescue SystemCallError => exception + @connection.restart + raise(Capybara::Webkit::CrashError, <<-MESSAGE.strip) +The webkit_server process crashed! + + #{exception.message} + +This is a bug in capybara-webkit. For help with this crash, please visit: + +https://github.com/thoughtbot/capybara-webkit/wiki/Reporting-Crashes + MESSAGE end def evaluate_script(script) diff --git a/lib/capybara/webkit/connection.rb b/lib/capybara/webkit/connection.rb index 4df5279..0c9f22a 100644 --- a/lib/capybara/webkit/connection.rb +++ b/lib/capybara/webkit/connection.rb @@ -61,6 +61,12 @@ module Capybara::Webkit response end + def restart + @socket = nil + start_server + connect + end + private def start_server diff --git a/lib/capybara/webkit/errors.rb b/lib/capybara/webkit/errors.rb index 097aa5c..06681fa 100644 --- a/lib/capybara/webkit/errors.rb +++ b/lib/capybara/webkit/errors.rb @@ -23,6 +23,9 @@ module Capybara::Webkit class ModalNotFound < StandardError end + class CrashError < StandardError + end + class JsonError def initialize(response) error = JSON.parse response diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index a048b41..547ca70 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -3092,6 +3092,26 @@ CACHE MANIFEST end end + context "when the driver process crashes" do + let(:driver) do + driver_for_app browser do + get "/" do + "Relaunched" + end + end + end + + let(:browser) { Capybara::Webkit::Browser.new(connection) } + let(:connection) { Capybara::Webkit::Connection.new } + + it "reports and relaunches on reset" do + Process.kill "KILL", connection.pid + expect { driver.reset! }.to raise_error(Capybara::Webkit::CrashError) + visit "/" + expect(driver.html).to include("Relaunched") + end + end + def driver_url(driver, path) URI.parse(driver.current_url).merge(path).to_s end