Subdomains with capybara-webkit? #87

Closed
lancejpollard opened this Issue Jun 24, 2011 · 21 comments

Comments

Projects
None yet
@lancejpollard

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

This comment has been minimized.

Show comment
Hide comment
@millisami

millisami Jun 28, 2011

@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??

@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??

@nicholaides

This comment has been minimized.

Show comment
Hide comment
@nicholaides

nicholaides Jul 26, 2011

Contributor

+1 on this issue

Contributor

nicholaides commented Jul 26, 2011

+1 on this issue

@nicholaides

This comment has been minimized.

Show comment
Hide comment
@nicholaides

nicholaides Jul 26, 2011

Contributor

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.

Contributor

nicholaides commented Jul 26, 2011

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

This comment has been minimized.

Show comment
Hide comment
@espen

espen Aug 3, 2011

@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/

espen commented Aug 3, 2011

@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/

@nicholaides

This comment has been minimized.

Show comment
Hide comment
@nicholaides

nicholaides Aug 3, 2011

Contributor

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

Contributor

nicholaides commented Aug 3, 2011

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

@espen

This comment has been minimized.

Show comment
Hide comment
@espen

espen Aug 5, 2011

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"

espen commented Aug 5, 2011

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"

@nicholaides

This comment has been minimized.

Show comment
Hide comment
@nicholaides

nicholaides Aug 5, 2011

Contributor

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

Contributor

nicholaides commented Aug 5, 2011

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

@bradrobertson

This comment has been minimized.

Show comment
Hide comment
@bradrobertson

bradrobertson Nov 9, 2011

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.

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.

@shioyama

This comment has been minimized.

Show comment
Hide comment
@shioyama

shioyama Jan 21, 2012

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.

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.

@glebm

This comment has been minimized.

Show comment
Hide comment
@glebm

glebm Mar 13, 2012

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

Any updates?

glebm commented Mar 13, 2012

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

Any updates?

@shioyama

This comment has been minimized.

Show comment
Hide comment
@shioyama

shioyama Mar 13, 2012

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

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

@aka47

This comment has been minimized.

Show comment
Hide comment
@aka47

aka47 Apr 17, 2012

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

aka47 commented Apr 17, 2012

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

@aka47

This comment has been minimized.

Show comment
Hide comment
@aka47

aka47 Apr 18, 2012

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

aka47 commented Apr 18, 2012

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

@jferris

This comment has been minimized.

Show comment
Hide comment
@jferris

jferris Jul 11, 2012

Member

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.

Member

jferris commented Jul 11, 2012

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.

@jferris jferris closed this Jul 11, 2012

@jbarreneche

This comment has been minimized.

Show comment
Hide comment
@jbarreneche

jbarreneche Nov 22, 2012

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!

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!

@mhoran

This comment has been minimized.

Show comment
Hide comment
@mhoran

mhoran Nov 22, 2012

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.

Collaborator

mhoran commented Nov 22, 2012

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.

@jcostello

This comment has been minimized.

Show comment
Hide comment
@jcostello

jcostello Nov 27, 2012

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

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

@mhoran

This comment has been minimized.

Show comment
Hide comment
@mhoran

mhoran Nov 27, 2012

Collaborator

Are you running Ruby 1.9?

Juan Manuel notifications@github.com wrote:

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

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


Reply to this email directly or view it on GitHub:
#87 (comment)

Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Collaborator

mhoran commented Nov 27, 2012

Are you running Ruby 1.9?

Juan Manuel notifications@github.com wrote:

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

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


Reply to this email directly or view it on GitHub:
#87 (comment)

Sent from my Android phone with K-9 Mail. Please excuse my brevity.

@jcostello

This comment has been minimized.

Show comment
Hide comment
@jcostello

jcostello Nov 27, 2012

ruby -v
ruby 1.9.3p327

pkg-config --modversion QtCore
4.8.3

ruby -v
ruby 1.9.3p327

pkg-config --modversion QtCore
4.8.3

@mhoran

This comment has been minimized.

Show comment
Hide comment
@mhoran

mhoran Nov 27, 2012

Collaborator

Just pushed a possible fix.

Juan Manuel notifications@github.com wrote:

ruby 1.9.3p327


Reply to this email directly or view it on GitHub:
#87 (comment)

Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Collaborator

mhoran commented Nov 27, 2012

Just pushed a possible fix.

Juan Manuel notifications@github.com wrote:

ruby 1.9.3p327


Reply to this email directly or view it on GitHub:
#87 (comment)

Sent from my Android phone with K-9 Mail. Please excuse my brevity.

@jcostello

This comment has been minimized.

Show comment
Hide comment
@jcostello

jcostello Nov 27, 2012

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

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