Skip to content
This repository

Subdomains with capybara-webkit? #87

Closed
lancejpollard opened this Issue June 24, 2011 · 21 comments
Lance Pollard

With this in my /etc/hosts file:

127.0.0.1 localhost local.host www.local.host blog.local.host wiki.local.host mobile.local.host

... I can use the app locally by going to urls like http://wiki.local.host:3000/overview.

Is there any way I can do this with capybara + capybara-webkit with rspec 2 in rails (without cucumber)? I am using capybara 1.0.

I have tried the suggestions here but none of them work:

If I am using the default rack-test driver, everything works fine:

# spec/spec_helper.rb
Capybara.app_host = "http://wiki.local.host:3000"

# spec/wiki_spec.rb
require 'spec_helper.rb'

feature "Wiki" do
  context "GET" do
    scenario "home page" do
      visit "/"
      page.should have_title "Overview"
    end
  end
end

But if I add :js => true, it doesn't work:

# spec/spec_helper.rb
Capybara.app_host  = "http://wiki.local.host:3000"
Capybara.javascript_driver = :webkit

# spec/wiki_spec.rb
require 'spec_helper.rb'

feature "Wiki" do
  context "GET" do
    scenario "home page", :js => true do
      visit "/"

      # Capybara::Driver::Webkit::WebkitError:
      #  Unable to load URL: http://wiki.local.host:3000/

      page.should have_title "Overview"
    end
  end
end

Any ideas?

millisami

@viatropos I too downloaded the qt-webkit, installed it. Then I installed the gem and when I was going to setup, there wasn't anything on how to use with the new capybara dsl.
Or similar with the :js => true option.
Any lights on this??

Mike Nicholaides

+1 on this issue

Mike Nicholaides

Ok, so was able to get subdomains working with a little bit of monkey patching. I'll share it here in the hopes this or another solution will eventually find its way into the official projects.

The problem is that you can't set Capybara.app_host correctly because the port that Capybara assigns to the rack server can differ and you have to give it the right port. (Or am I wrong?)

The solution is to make Capybara re-write your app_host to include the port that it chose for the rack server.

First, you'll want to set up any subdomains on your machine. In your hosts file:

127.0.0.1 test.local.host 

Then, add this monky-patching code:

require 'capybara'
module Capybara

  # Adding a flag you can set to make Capybara always give `app_host` the port
  #  it chose for the rack server.
  def self.use_own_port?
    @use_own_port
  end

  def self.use_own_port=(use_own_port)
    @use_own_port = use_own_port
  end


  class Server
    # re-write this method to obey the `Capybara.use_own_port?` flag
    def url(path)
      if path =~ /^http/
        path
      else
        url = Capybara.app_host ? Capybara.app_host.dup : "http://#{host}:#{port}"
        url << ":#{port}" if !Capybara.app_host || Capybara.use_own_port?
        url + path.to_s
      end
    end
  end
end

Then, set your app_host as you normally would, plus that flag:

Capybara.app_host = 'http://test.local.host'
Capybara.use_own_port = true

That's should do it.

The next question is where should this code go? Does it belong to capybara or should capybara-webkit and other drivers implement it? I assume other drivers have this issue.

Espen Antonsen

@nicholaides Tried your monkey patching but could not get it to work. Capybara::Driver::Webkit::WebkitInvalidResponseError: Unable to load URL: http://torggatasquash.makeplans.testnet:58959/

Mike Nicholaides

I'm not sure, @espen. The first thing I would do is make sure the host file addition is working.

Espen Antonsen

After google and trying 10 different solutions all night, trying to uninstall Pow to see if that had any effect, restart to ensure hosts file was really really in effect, I removed all monkeypatching and tried to keep it simple. And it worked :) All I did was force Capybara to use a specific port and hardcode that port in the app_host url. And put in the required entries in the hosts file.

test_helper.rb:
Capybara.javascript_driver = :webkit
Capybara.server_port = 7171

integration test setup:
Capybara.app_host = "http://subdomain.appname.testnet:7171"

Mike Nicholaides

That seems like a good solution. I'll try that.

Brad Robertson

Just a heads up I've been getting the exact same error even with your fix, but I realized that my page actually had an exception on it. Unfortunately the integration tests using capybara-webkit doesn't seem to handle exceptions very well and throws the Capybara::Driver::Webkit::WebkitInvalidResponseError: Unable to load URL: http://subdomain.example.com:7171 error rather than give you the real error on the page.

Chris Salzberg

echoing @bradrobertson, I had the same error and also found the problem was a page with an exception. It took me hours of picking away at the page view to track down the problem. it would be great if capybara-webkit could give more info than just the cryptic "unable to load URL" error.

The clue for me to figuring this out was realizing that only one page wasn't loading, while the others were.

Gleb Mazovetskiy
glebm commented March 13, 2012

@shioyama Didn't help me, it still tries to go to example.whatever

Any updates?

Chris Salzberg

@glebm wow, I had totally forgotten about this. Basically once I fixed the exception the problem disappeared for me. Haven't looked back since -- sorry can't be of more help on this one.

Tim Aßmann
aka47 commented April 17, 2012

it works for me with the patch above from @nicholaides .. this should be pulled into master :+1:

Tim Aßmann
aka47 commented April 18, 2012

i submitted a patch to capybara which fixes it.
jnicklas/capybara#694

Joe Ferris
Owner

I believe this is fixed in capybara now. Feel free to comment if it's still an issue with the latest capybara and capybara-webkit.

Joe Ferris jferris closed this July 10, 2012
Juan Manuel Barreneche

Hi, the issue was fixed in Capybara master branch (current for Capybara 2.0), but Capybara-webkit doesn't support that version. Any plan to support Capybara 2.0? Which is the current best workaround to this issue?

Thanks!

Matthew Horan
Collaborator

We're just about finished with Capybara 2.0 support. If you're daring, you can test out my 2.0 compatibility branch. Note that Ruby 1.9 and Qt 4.8 are required.

Juan Manuel

Hi @mhoran, im testing your mh-capybara-2-0 branch and i get this error when i visit some path, any idea?

uninitialized constant Capybara::Webkit::Connection::Open3

Matthew Horan
Collaborator
Juan Manuel

ruby -v
ruby 1.9.3p327

pkg-config --modversion QtCore
4.8.3

Matthew Horan
Collaborator
Juan Manuel

Thanks, it fix the problem. Im having another problem but is not a problem related with the version. In some test the webkit server freeze. The test has nothing different that the test that are green. I have no idea why its happening.

Anyway, thanks a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.