Capybara::Poltergeist Status error, failed to reach server
Asked Answered
F

3

7

I am trying to make some tests with Rspec/Capybara/Poltergeist on my Rails 4 application, Phantomjs is installed (version 2.2.1), but I always get this error :

Failure/Error: visit(perfect_landing_page_path)

Capybara::Poltergeist::StatusFailError:

Request to 'http://127.0.0.1:49623/path' failed to reach server, check DNS and/or server status

The test i'm working on :

require 'rails_helper'

RSpec.feature 'Subscription', :type => :feature do

  let!(:plan) { create(:plan) }
  let!(:landing_page) { create(:landing_page) }

  before(:each) { landing_page.default_plan = plan }

  describe 'landing_page#perfect_show' do
    scenario 'form display', js: true do
      plan_2 = create(:plan)
      plan_3 = create(:plan)
      landing_page.plans << plan_2
      landing_page.plans << plan_3
      visit(perfect_landing_page_path)
      expect(page).to have_css(".start-now", count: 3)
      first(".start-now").click
      expect(page).to have_css("#new_user")
    end

  end

end

My Gemfile looks like this :

  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails'
  gem 'guard-rspec'

group :test do
  gem 'database_cleaner'
  gem 'capybara'
  gem 'capybara-screenshot'
  gem 'poltergeist'
  gem 'selenium-webdriver'
  gem 'shoulda-matchers', require: false
  gem 'show_me_the_cookies'
end

My spec/support/capybara.rb file :

require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'

require "capybara/poltergeist" # Add this line to require poltergeist
require 'selenium-webdriver'

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, {:js_errors => false, :default_max_wait_time => 30, :timeout => 30, phantomjs_options: [
                             '--load-images=no',
                             '--ignore-ssl-errors=true',
                             '--ssl-protocol=any']})
end

Capybara.register_driver :poltergeist_debug do |app|
  Capybara::Poltergeist::Driver.new(app, :inspector => true)
end

Capybara.configure do |config|
  config.javascript_driver = :poltergeist
  config.ignore_hidden_elements = true
  config.default_max_wait_time = 30
end

My spec_helper.rb file :

require 'capybara/rspec'

RSpec.configure do |config|

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.filter_run :focus
  config.run_all_when_everything_filtered = true

Did someone encounter this problem before ? Does anyone have a solution for this ? I have been banging my head for days looking on the web... Thank you very much.

PS : it works fine on my friend's mac (Yosemite or El Capitan), just not on mine.

Fidole answered 2/3, 2016 at 9:29 Comment(2)
Can you post the full error message? What you've posted is truncated and missing the most important part (the detail of the error)Hoofer
I updated the answer to add a whitelist alternativeHoofer
P
3

I was having this problem in CI (CodeShip).

Adding a call to bundle exec rake assets:precompile at the end of the setup commands seemed to fix it:

CodeShip CI Setup Commands:

rvm use 2.3.4 --install
bundle install
export RAILS_ENV=test
bundle exec rake db:schema:load
bundle exec rake assets:precompile

Also had a whitelist per @agbodike but in the rails_helper.rb

config.before(:each, js: true) do
  page.driver.browser.url_whitelist = ["127.0.0.1"]
end
Pope answered 24/5, 2017 at 1:2 Comment(0)
H
2

I had the same issue and it was due to a 3rd party script timing out. You can prevent loading of 3rd party scripts with a blacklist. For example:

config.before(:each, js: true) do
  page.driver.browser.url_blacklist = ["http://use.typekit.net"]
end

would prevent any URL starting with http://use.typekit.net from being called for each test that uses the js driver. More information can be found at:

https://robots.thoughtbot.com/speed-up-javascript-capybara-specs-by-blacklisting-urls

Alternatively you can use a whitelist:

config.before(:each, js: true) do
  page.driver.browser.url_whitelist = ["127.0.0.1"]
end

which will block all requests not to 127.0.0.1

I placed the configuration in spec/feature_helper.rb to ensure it was only set for feature specs.

Hoofer answered 15/3, 2016 at 6:20 Comment(3)
Thanks for the reply. The bug actually appears randomly now... but it's also true that the tests are taking a very long time to run, and I tried your method, but it didn't change anything for me.Fidole
Where would you put that statement?Corrinacorrine
I have it in spec/feature_helper.rbHoofer
E
0

You can avoid these by precompile the assets before tests. And the code is:

RSpec.configure do |config|
  config.before :all do
    ENV['PRECOMPILE_ASSETS'] ||= begin
      case self.class.metadata[:type]
      when :feature, :view
        STDOUT.write "Precompiling assets..."

        require 'rake'
        Rails.application.load_tasks
        Rake::Task['assets:precompile'].invoke

        STDOUT.puts " done."
        Time.now.to_s
      end
    end
  end
end

more info

Exhume answered 23/9, 2016 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.