rubycdp / cuprite

Headless Chrome/Chromium driver for Capybara
https://cuprite.rubycdp.com
MIT License
1.24k stars 93 forks source link

With cuprite in local test, I always get Ferrum::StatusError and RuntimeError #142

Closed Sylor-huang closed 3 years ago

Sylor-huang commented 3 years ago

Hi, I use cuprite to test my codes in local , but I always get errors like this:

 1) VendorsController Get #index format html I should see Vendors
     Got 0 failures and 2 other errors:

     1.1) Failure/Error: visit vendors_path

          Ferrum::StatusError:
            Request to http://localhost:55114/vendors reached server, but there are still pending connections: http://localhost:55114/vendors

          [Screenshot]: /Users/mac/MyCodes/mob_admin/tmp/screenshots/failures_r_spec_example_groups_vendors_controller_get_index_format_html_i_should_see_vendors_963.png

          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/ferrum-0.9/lib/ferrum/page.rb:82:in `rescue in goto'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/ferrum-0.9/lib/ferrum/page.rb:68:in `goto'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/cuprite-0.11/lib/capybara/cuprite/browser.rb:50:in `visit'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/cuprite-0.11/lib/capybara/cuprite/driver.rb:43:in `visit'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/capybara-3.31.0/lib/capybara/session.rb:278:in `visit'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/capybara-3.31.0/lib/capybara/dsl.rb:58:in `block (2 levels) in <module:DSL>'
          # ./spec/system/controllers/vendors_controller_spec.rb:18:in `block (4 levels) in <top (required)>'
          # ./spec/system/support/better_rails_system_tests.rb:32:in `block (2 levels) in <top (required)>'
          # ------------------
          # --- Caused by: ---
          # Ferrum::TimeoutError:
          #   Timed out waiting for response. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the :timeout option to a higher value might help.
          #   /Users/mac/.rvm/gems/ruby-2.6.5/gems/ferrum-0.9/lib/ferrum/browser/client.rb:46:in `command'

     1.2) Failure/Error: ex.run

          RuntimeError:
            Requests did not finish in 60 seconds: ["/vendors"]

          [Screenshot]: /Users/mac/MyCodes/mob_admin/tmp/screenshots/failures_r_spec_example_groups_vendors_controller_get_index_format_html_i_should_see_vendors_963.png

          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/capybara-3.31.0/lib/capybara/server.rb:66:in `wait_for_pending_requests'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/capybara-3.31.0/lib/capybara/session.rb:133:in `reset!'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/capybara-3.31.0/lib/capybara.rb:322:in `block in reset_sessions!'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/capybara-3.31.0/lib/capybara.rb:322:in `reverse_each'
          # /Users/mac/.rvm/gems/ruby-2.6.5/gems/capybara-3.31.0/lib/capybara.rb:322:in `reset_sessions!'
          # ./spec/system/support/better_rails_system_tests.rb:32:in `block (2 levels) in <top (required)>'

Finished in 2 minutes 8.3 seconds (files took 5.57 seconds to load)
1 example, 1 failure

My cuprite settings is like follows:

1. capybara_setup.rb

capybara_setup settings ```ruby # frozen_string_literal: true # Capybara settings (not covered by Rails system tests) # Make server listening on all hosts Capybara.server_host = "0.0.0.0" # Use a hostname accessible from the outside world # NOTE: Rails overrides Capybara.app_host in Rails <6.1, so we have # to store it differently CAPYBARA_APP_HOST = "http://#{ENV.fetch("APP_HOST", "localhost")}" # Capybara.app_host = "http://#{`hostname`.strip&.downcase || "0.0.0.0"}" # Which domain to use when setting cookies directly in tests. CAPYBARA_COOKIE_DOMAIN = URI.parse(CAPYBARA_APP_HOST).host.then do |host| # If host is a top-level domain next host unless host.include?(".") ".#{host}" end # Don't wait too long in `have_xyz` matchers Capybara.default_max_wait_time = 2 # Normalizes whitespaces when using `has_text?` and similar matchers Capybara.default_normalize_ws = true # Where to store artifacts (e.g. screenshots, downloaded files, etc.) Capybara.save_path = ENV.fetch("CAPYBARA_ARTIFACTS", "./tmp/capybara") Capybara.singleton_class.prepend(Module.new do attr_accessor :last_used_session def using_session(name, &block) self.last_used_session = name super ensure self.last_used_session = nil end end) ```

2. cuprite_setup.rb

cuprite_setup settings ```ruby # frozen_string_literal: true # Cuprite is a modern Capybara driver which uses Chrome CDP API # instead of Selenium & co. # See https://github.com/rubycdp/cuprite REMOTE_CHROME_URL = ENV["CHROME_URL"] REMOTE_CHROME_HOST, REMOTE_CHROME_PORT = if REMOTE_CHROME_URL URI.parse(REMOTE_CHROME_URL).yield_self do |uri| [uri.host, uri.port] end end # Check whether the remote chrome is running and configure the Capybara # driver for it. remote_chrome = begin if REMOTE_CHROME_URL.nil? false else Socket.tcp(REMOTE_CHROME_HOST, REMOTE_CHROME_PORT, connect_timeout: 1).close true end rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError false end remote_options = remote_chrome ? {url: REMOTE_CHROME_URL} : {} require "capybara/cuprite" Capybara.register_driver(:cuprite) do |app| Capybara::Cuprite::Driver.new( app, **{ window_size: [1200, 800], browser_options: remote_chrome ? {"no-sandbox" => nil} : {}, inspector: true }.merge(remote_options) ) end Capybara.default_driver = Capybara.javascript_driver = :cuprite RSpec.configure do |config| config.include CupriteHelpers, type: :system end ```

3. better_rails_system_tests.rb

better_rails_system_tests settings ```ruby # frozen_string_literal: true # Rails is not flexible enough; at least for now( module BetterRailsSystemTests # Use our `Capybara.save_path` to store screenshots with other capybara artifacts # (Rails screenshots path is not configurable https://github.com/rails/rails/blob/49baf092439fc74fc3377b12e3334c3dd9d0752f/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb#L79) def absolute_image_path Rails.root.join("#{Capybara.save_path}/screenshots/#{image_name}.png") end # Use relative path in screenshot message to make it clickable in VS Code when running in Docker def image_path absolute_image_path.relative_path_from(Rails.root).to_s end # Make failure screenshots compatible with multi-session setup def take_screenshot return super unless Capybara.last_used_session Capybara.using_session(Capybara.last_used_session) { super } end # Convert dom_id to selector def dom_id(*args) "##{super}" end end RSpec.configure do |config| # Make urls in mailers contain the correct server host config.around(:each, type: :system) do |ex| was_host, Rails.application.default_url_options[:host] = Rails.application.default_url_options[:host], Capybara.server_host ex.run Rails.application.default_url_options[:host] = was_host end # Make sure this hook runs before others config.prepend_before(:each, type: :system) do # Rails sets host to `127.0.0.1` for every test by default. # That would break in Docker # NOTE: Fixed in 6.1 (https://github.com/rails/rails/commit/d415eb4f6d6bb24b78b968ae28c22bb7e1721285#diff-9de6fe0bff4847b77cba72441ee855c2) host! CAPYBARA_APP_HOST # Use JS driver always driven_by Capybara.javascript_driver end end ```

4. I had tried

(1) I had change CAPYBARA_APP_HOST incapybara_setup.rb to

CAPYBARA_APP_HOST = "http://#{ENV.fetch("APP_HOST", "0.0.0.0")}"

but I got the page that like 0.0.0.0:60569/vendors with blank page:

image

Could you help me , what's wrong with the codes?

Thanks very much~

route commented 3 years ago

Something is taking too long to respond from vendors, if you use assets try to precompile them first and then run tests. Also try to put debug and see yourself in the browser what is taking so long https://github.com/rubycdp/cuprite#debugging

Sylor-huang commented 3 years ago

Something is taking too long to respond from vendors, if you use assets try to precompile them first and then run tests. Also try to put debug and see yourself in the browser what is taking so long https://github.com/rubycdp/cuprite#debugging

@route Thanks very much, I had got the time out reasons.