Skip to content

Instantly share code, notes, and snippets.

@justin808
Last active November 26, 2016 09:29
Show Gist options
  • Select an option

  • Save justin808/a615eb5a5e7bb8c43fa1decf54876b88 to your computer and use it in GitHub Desktop.

Select an option

Save justin808/a615eb5a5e7bb8c43fa1decf54876b88 to your computer and use it in GitHub Desktop.

Revisions

  1. justin808 revised this gist Nov 26, 2016. 1 changed file with 24 additions and 7 deletions.
    31 changes: 24 additions & 7 deletions poltergeist.rb
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,36 @@
    # See in use here:
    # https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/spec/support/poltergeist.rb

    STRATEGY = :switch_to_selenium
    # This file supports 2 strategies:
    # 1. switch_to_selenium: switch drivers
    # 2. restart_poltergeist

    RESTART_PHANTOMJS = ENV["RESTART_PHANTOMJS"] &&
    %w(TRUE YES).include?(ENV["RESTART_PHANTOMJS"].upcase)
    puts "RESTART_PHANTOMJS = #{RESTART_PHANTOMJS}"

    CAPYBARA_TIMEOUT_RETRIES = 5

    # HACK: workaround for Capybara Poltergeist StatusFailErrors, simply retries, maybe with selenium,
    # HACK: workaround for Capybara Poltergeist StatusFailErrors, simply retries
    # based on https://gist.github.com/afn/c04ccfe71d648763b306
    RSpec.configure do |config|
    config.around(:each, type: :feature) do |ex|
    example = RSpec.current_example
    use_selenium = false
    original_driver = Capybara.default_driver
    CAPYBARA_TIMEOUT_RETRIES.times do
    example.instance_variable_set("@exception", nil)

    # Private method in rspec:
    # rspec-core-3.5.4/lib/rspec/core/memoized_helpers.rb:139
    __init_memoized

    if use_selenium
    puts "Switching to selenium from #{Capybara.current_driver}"
    Capybara.current_driver = js_selenium_driver
    Capybara.javascript_driver = js_selenium_driver
    end

    ex.run

    example_ex = example.exception
    @@ -44,16 +59,17 @@
    puts "=" * 80
    puts "Exception caught! #{example_ex.ai}"
    puts "when running example:\n #{example.full_description}"
    puts "at #{example.location}"
    puts " at #{example.location} with driver #{Capybara.current_driver}."

    if STRATEGY == :switch_to_selenium
    puts "Switching to selenium from #{Capybara.current_driver}"
    Capybara.current_driver = js_selenium_driver
    else
    if RESTART_PHANTOMJS
    PhantomJSRestart.call
    else
    use_selenium = true
    end
    puts "=" * 80
    end
    Capybara.current_driver = original_driver
    Capybara.javascript_driver = original_driver
    Capybara.use_default_driver
    end
    end
    @@ -76,3 +92,4 @@ def self.call
    end
    end
    end

  2. justin808 revised this gist Nov 26, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion poltergeist.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    # See in use here:
    # https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/spec/support/poltergeist.rb

    STRATEGY = :switch_to_selenium
    CAPYBARA_TIMEOUT_RETRIES = 5

    # HACK: workaround for Capybara Poltergeist StatusFailErrors, simply retries
    # HACK: workaround for Capybara Poltergeist StatusFailErrors, simply retries, maybe with selenium,
    # based on https://gist.github.com/afn/c04ccfe71d648763b306
    RSpec.configure do |config|
    config.around(:each, type: :feature) do |ex|
  3. justin808 revised this gist Nov 26, 2016. 1 changed file with 16 additions and 5 deletions.
    21 changes: 16 additions & 5 deletions poltergeist.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    STRATEGY = :switch_to_selenium
    CAPYBARA_TIMEOUT_RETRIES = 5

    # HACK: workaround for Capybara Poltergeist StatusFailErrors, simply retries
    @@ -7,7 +8,11 @@
    example = RSpec.current_example
    CAPYBARA_TIMEOUT_RETRIES.times do
    example.instance_variable_set("@exception", nil)

    # Private method in rspec:
    # rspec-core-3.5.4/lib/rspec/core/memoized_helpers.rb:139
    __init_memoized

    ex.run

    example_ex = example.exception
    @@ -37,17 +42,23 @@
    puts "Exception caught! #{example_ex.ai}"
    puts "when running example:\n #{example.full_description}"
    puts "at #{example.location}"
    puts "Restarting phantomjs and retrying..."
    puts " -> If this doesn't work, put a modest sleep before your last assertion."
    PhantomJSRestart.call

    if STRATEGY == :switch_to_selenium
    puts "Switching to selenium from #{Capybara.current_driver}"
    Capybara.current_driver = js_selenium_driver
    else
    PhantomJSRestart.call
    end
    puts "=" * 80
    end
    Capybara.use_default_driver
    end
    end

    # Rather than using switching to use selenium, we could have restarted Phantomjs
    module PhantomJSRestart
    def self.call
    puts "-> Restarting phantomjs: iterating through capybara sessions..."
    puts "Restarting phantomjs: iterating through capybara sessions..."
    session_pool = Capybara.send("session_pool")
    session_pool.each do |mode, session|
    msg = " => #{mode} -- "
    @@ -61,4 +72,4 @@ def self.call
    puts msg
    end
    end
    end
    end
  4. justin808 created this gist Nov 26, 2016.
    64 changes: 64 additions & 0 deletions poltergeist.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    CAPYBARA_TIMEOUT_RETRIES = 5

    # HACK: workaround for Capybara Poltergeist StatusFailErrors, simply retries
    # based on https://gist.github.com/afn/c04ccfe71d648763b306
    RSpec.configure do |config|
    config.around(:each, type: :feature) do |ex|
    example = RSpec.current_example
    CAPYBARA_TIMEOUT_RETRIES.times do
    example.instance_variable_set("@exception", nil)
    __init_memoized
    ex.run

    example_ex = example.exception

    break unless example_ex

    is_multiple_exception = example_ex.is_a?(RSpec::Core::MultipleExceptionError)

    break unless example_ex.is_a?(Capybara::Poltergeist::StatusFailError) ||
    example_ex.is_a?(Capybara::Poltergeist::DeadClient) ||
    is_multiple_exception

    if is_multiple_exception
    m_exceptions = example_ex.all_exceptions

    idx = m_exceptions.find_index do |exception|
    exception.is_a?(Capybara::Poltergeist::StatusFailError) ||
    exception.is_a?(Capybara::Poltergeist::DeadClient) ||
    exception.class < SystemCallError
    end

    break unless idx
    end

    puts "\n"
    puts "=" * 80
    puts "Exception caught! #{example_ex.ai}"
    puts "when running example:\n #{example.full_description}"
    puts "at #{example.location}"
    puts "Restarting phantomjs and retrying..."
    puts " -> If this doesn't work, put a modest sleep before your last assertion."
    PhantomJSRestart.call
    puts "=" * 80
    end
    end
    end

    module PhantomJSRestart
    def self.call
    puts "-> Restarting phantomjs: iterating through capybara sessions..."
    session_pool = Capybara.send("session_pool")
    session_pool.each do |mode, session|
    msg = " => #{mode} -- "
    driver = session.driver
    if driver.is_a?(Capybara::Poltergeist::Driver)
    msg += "restarting"
    driver.restart
    else
    msg += "not poltergeist: #{driver.class}"
    end
    puts msg
    end
    end
    end