ruby 当Poltergeist随机失败时重新启动并重试。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 当Poltergeist随机失败时重新启动并重试。相关的知识,希望对你有一定的参考价值。

# Borrowed from https://github.com/y310/rspec-retry/blob/master/lib/rspec/retry.rb
# Drop this script into spec/support
# To test snippet just set timeout to 1

RSpec.configure do |config|

  Capybara.javascript_driver = :poltergeist

  Capybara.register_driver(:poltergeist) do |app|
    Capybara::Poltergeist::Driver.new app,
      js_errors: true, # optional, i prefer to catch js stuff
      timeout: 180,    # ensure you tweak timeout first !!
      phantomjs_logger: Puma::NullIO.new, # for puma
      logger: nil,
      phantomjs_options:
      [
        '--load-images=no',
        '--ignore-ssl-errors=yes' # only setting this can do the trick sometimes
      ]
  end

  config.around(:each, type: :feature) do |example|
    current_example = RSpec.current_example
    2.times do |index|
      current_example.instance_variable_set('@exception', nil)
      instance_variable_set('@__memoized', nil) # clear let variables
      example.run
      break unless current_example.exception.is_a?(Capybara::Poltergeist::TimeoutError)
      restart_message_for current_example if index == 1
      restart_phantomjs
    end
  end

  def restart_message_for(example)
    msg  = "\n#{example.exception} - restart and retry..\n"
    msg += "    # #{example.location}\n"
    $stdout.puts msg.yellow
  end

  def restart_phantomjs
    Capybara.send('session_pool').each do |_, session|
      next unless session.driver.is_a?(Capybara::Poltergeist::Driver)
      session.driver.restart
    end
  end
end

以上是关于ruby 当Poltergeist随机失败时重新启动并重试。的主要内容,如果未能解决你的问题,请参考以下文章

ruby 基于Poltergeist(PhantomJS)的Web Crawler Helper类。使用Capybara作为构建webcrawler的框架非常方便

ruby 使用Capybara w / Poltergeist(PhantomJS)从位于给定URL的HTML页面的主体中抓取文本内容。

Capistrano在升级ruby版本和puma时重启错误版本的puma

创建 ruby​​ db 时延迟符号绑定失败

如何使用 poltergeist webdriver 让 JS 点击?

无法安装指南针