Rails Poltergeist/PhantomJS 未渲染 www.example.com

Rails Poltergeist/PhantomJS not rendering www.example.com

背景

下面是test/integration/projects_test.rb。我们需要登录用户才能访问 projects_url.

setup do
  ...
  login_as @user
  ...
end

# PARTIAL SUCCESS (RENDERS LOGIN PAGE (instead of projects), IGNORES LOGGED IN @USER)
test "can visit projects" do
  visit "http://127.0.0.1:3000/projects"
  page.save_screenshot
end

# FAILURE (BLANK PAGE)
test "can visit projects_url" do
  visit projects_url
  page.save_screenshot
end

当我激活调试消息时,它显示了正在发生的事情:

  1. login_as @user 正在登录 www.example.com。
  2. visit "http://127.0.0.1:3000/projects" 呈现页面,但它 不会 进入 www.example.com 因此我们的 @user 未被识别为已登录。
  3. visit projects_url 将变为 www.example.com/projects,其中 Poltergeist/PhantomJS 呈现为空白。

问题

那么,我如何


资源

test_helper.rb 的内容:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'capybara/rails'

class ActiveSupport::TestCase
  fixtures :all

  require 'minitest/reporters'
  Minitest::Reporters.use!

  Capybara.default_driver = :rack_test

  include Warden::Test::Helpers   # Help with logging in and out.
end


class ActionController::TestCase
  include Devise::TestHelpers
end


class ActionDispatch::IntegrationTest
  include Capybara::DSL

  require 'capybara/poltergeist'
  Capybara.register_driver(:poltergeist) do |app|
    Capybara::Poltergeist::Driver.new(app, {
      js_errors: false,
      timeout: 10,
      phantomjs_options: ['--load-images=no', '--ignore-ssl-errors=yes', '--ssl-protocol=TLSv1'],
      url_whitelist: ["127.0.0.1", "localhost"]
    })
  end
  Capybara.javascript_driver = :poltergeist
end

宝石文件

group :development, :test do
  gem 'minitest-rails-capybara'
  gem 'poltergeist'
  gem 'minitest-reporters'
  gem 'launchy'
  gem 'simplecov', require: false
end

版本

ruby 2.3.1
rails 4.2.7
capybara 2.14.0
poltergeist 1.15.0
launchy 2.4.2
simplecov 0.14.1

一个请求

将 Poltergeist 设置为 url_whitelist: ['127.0.0.1', 'localhost', 'example.com']

运行 visit root_path.

测试

结果:

http://www.example.com/{"id":"1d9f77e2-c637-4097-bfa0-072f4371fcf6","name":"set_js_errors","args":[false]}
{"command_id":"1d9f77e2-c637-4097-bfa0-072f4371fcf6","response":true}
{"id":"983f046e-450a-4dcc-95f7-7f08caf1f059","name":"set_debug","args":[true]}
{"command_id":"983f046e-450a-4dcc-95f7-7f08caf1f059","response":true}
{"id":"e23a646a-fe9f-4e00-8d8b-dacce27d8c0e","name":"set_url_whitelist","args":["127.0.0.1","localhost","example.com"]}
{"command_id":"e23a646a-fe9f-4e00-8d8b-dacce27d8c0e","response":true}
{"id":"67d19849-60dc-4cbb-95d3-fef0e388fe2a","name":"visit","args":["http://127.0.0.1:52788/"]}
{"command_id":"67d19849-60dc-4cbb-95d3-fef0e388fe2a","error":{"name":"Poltergeist.StatusFailError","args":["http://127.0.0.1:52788/","Timed out with no open resource requests"]}}
ERROR["test_basic_poltergeist_test", ProjectsTest, 11.084383999999773]
 test_basic_poltergeist_test#ProjectsTest (11.08s)
Capybara::Poltergeist::StatusFailError:         Capybara::Poltergeist::StatusFailError: Request to 'http://127.0.0.1:52788/' failed to reach server, check DNS and/or server status - Timed out with no open resource requests
            poltergeist (1.15.0) lib/capybara/poltergeist/browser.rb:377:in `command'
            poltergeist (1.15.0) lib/capybara/poltergeist/browser.rb:36:in `visit'
            poltergeist (1.15.0) lib/capybara/poltergeist/driver.rb:97:in `visit'
            capybara (2.14.0) lib/capybara/session.rb:269:in `visit'
            capybara (2.14.0) lib/capybara/dsl.rb:50:in `block (2 levels) in <module:DSL>'
            test/integration/projects_test.rb:41:in `block in <class:ProjectsTest>'
            minitest (5.9.0) lib/minitest/test.rb:107:in `block (3 levels) in run'
            minitest (5.9.0) lib/minitest/test.rb:204:in `capture_exceptions'
            minitest (5.9.0) lib/minitest/test.rb:104:in `block (2 levels) in run'
            minitest (5.9.0) lib/minitest/test.rb:255:in `time_it'
            minitest (5.9.0) lib/minitest/test.rb:103:in `block in run'
            minitest (5.9.0) lib/minitest.rb:348:in `on_signal'
            minitest (5.9.0) lib/minitest/test.rb:275:in `with_info_handler'
            minitest (5.9.0) lib/minitest/test.rb:102:in `run'
            minitest-reporters (1.1.14) lib/minitest/reporters.rb:48:in `run_with_hooks'
            minitest (5.9.0) lib/minitest.rb:799:in `run_one_method'
            minitest (5.9.0) lib/minitest.rb:322:in `run_one_method'
            minitest (5.9.0) lib/minitest.rb:310:in `block (2 levels) in run'
            minitest (5.9.0) lib/minitest.rb:309:in `each'
            minitest (5.9.0) lib/minitest.rb:309:in `block in run'
            minitest (5.9.0) lib/minitest.rb:348:in `on_signal'
            minitest (5.9.0) lib/minitest.rb:335:in `with_info_handler'
            minitest (5.9.0) lib/minitest.rb:308:in `run'
            minitest (5.9.0) lib/minitest.rb:158:in `block in __run'
            minitest (5.9.0) lib/minitest.rb:158:in `map'
            minitest (5.9.0) lib/minitest.rb:158:in `__run'
            minitest (5.9.0) lib/minitest.rb:135:in `run'
            minitest (5.9.0) lib/minitest.rb:62:in `block in autorun'

Test.log(最后几行):

  Rendered shared/_searchBox.html.erb (0.2ms)
  Rendered projects/index.html.erb within layouts/application (7.4ms)
  Rendered layouts/_google_analytics.js.erb (0.4ms)
Completed 200 OK in 50ms (Views: 20.3ms | ActiveRecord: 2.3ms)
Started GET "/assets/application.css" for 127.0.0.1 at 2017-05-03 19:43:06 -0400
Started GET "/assets/application.js" for 127.0.0.1 at 2017-05-03 19:43:06 -0400

如果您希望允许对 url 的页面请求,您需要在您的 Poltergeist 白名单中允许“http://www.example.com”。

此外,http://127.0.0.1:3000 很可能是您的开发实例,因此您不希望测试命中它,因为它不会使用您设置的测试数据。