-->

capybaraWebKit is unable to visit webpages when ru

2019-05-31 15:26发布

问题:

On previous RSpec integration tests where :js => false (i.e., JavaScript was turned off), my tests were running just fine. However, for RSpec tests where :js => true (and therefore needing to use capybara WebKit), I consistently get the following error:

Unable to load URL: http://my.server:5000/auth/identity because of error loading http://my.server:5000/auth/identity: Unknown error

Noted this is happening on a line of code that was working just fine when :js => false (it is part of a method that signs in a user, which is required for both sets of integration tests).

What I have tried:

  • Using ufw (since I am running on Ubuntu), I made sure that port 5000 is open. However, I don't think that this was the problem anyway, since the tests were able to run fine using the same port in other tests when :js => false
  • Visiting other pages (e.g., http://my.server:5000/), but the same error message appears.
  • I tried using the debugging code that capybara WebKit documentation provides:

    Capybara::Webkit.configure do |config| config.debug = true

I put it in my spec_helper.rb. But this ends up giving me another error message, namely: NoMethodError: undefined method configure' for Capybara::Webkit:Module.

  • I tried stopping and restarting spring (using spring stop and spring restart) following these instructions. Still nothing is working. (I tried running the tests after both commands, and still got the same error message both times)

Notes on my configuration:

  • When I received the project, the spec_helper.rb file had Capybara.server_port = 5000, and I haven't changed that. This variable is then used in all of the visit methods (e.g., `visit "http://my.server:#{Capybara.server_port}/").

  • There is no line that has Capybara.app_host = ______

  • This web app includes three subdomains which I will need to be able to access: cms.my.server, www.my.server, and admin.my.server. (Keep in mind that my.server is a stand-in for the actual server name, which I cannot use for legal reasons). All 3 of these are set to resolve to 127.0.0.1 in my hosts file.

Also note that my teammate has got the exact same test to work.

回答1:

Assuming you are using the standard Capybara rspec configuration, when js: false is specified (or no js metadata is specified) then Capybara will be using the rack_test driver. The rack_test driver completely ignores all hostnames and ports specified and submits "requests" directly to the rails apps routing handlers. Because of this if you tell it connect to http://my.server:5000 it just goes straight to the routing handlers with a destination of / - so the fact that it works when js: false is specified does not in any way imply that port 5000 is actually doing anything.

The question to answer is why is Capybara is trying to connect to port 5000. When testing an app with normal Capybara usage, Capybara will start up the app in a separate thread and bind it to 127.0.0.1 on a random port. It's highly unlikely the random port would be 5000 and definitely wouldn't be multiple runs in a row. From here on we're in guessing territory (without seeing your Capybara config) but you probably have specified Capybara.app_host = 'http://my.server:5000' which would only work if you combined it Capybara.server_port = 5000, thereby forcing Capybara to bind its server on port 5000. The preferred option would be to do

Capybara.app_host = 'http://my.server'
Capybara.always_include_port = true

which will let capybara pick a random port, but then add it into every visit call made (Note: this all assumes that 'my.server' does resolve to 127.0.0.1 on the machine you're testing on).