RyanQuey
RyanQuey

Reputation: 725

capybaraWebKit is unable to visit webpages when running RSpec tests when :js => true

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:

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.

Notes on my configuration:

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

Upvotes: 1

Views: 264

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49870

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).

Upvotes: 2

Related Questions