thoughtbot / capybara-webkit

A Capybara driver for headless WebKit to test JavaScript web apps
https://thoughtbot.com/open-source
MIT License
1.97k stars 427 forks source link

Subdomains with capybara-webkit? #87

Closed lancejpollard closed 12 years ago

lancejpollard commented 13 years ago

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 commented 13 years ago

@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 commented 13 years ago

+1 on this issue

nicholaides commented 13 years ago

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 commented 13 years ago

@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 commented 13 years ago

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

espen commented 13 years ago

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 commented 13 years ago

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

bradrobertson commented 13 years ago

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 commented 12 years ago

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 commented 12 years ago

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

Any updates?

shioyama commented 12 years ago

@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 commented 12 years ago

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

aka47 commented 12 years ago

i submitted a patch to capybara which fixes it. https://github.com/jnicklas/capybara/pull/694

jferris commented 12 years ago

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.

jbarreneche commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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: https://github.com/thoughtbot/capybara-webkit/issues/87#issuecomment-10761961

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

jcostello commented 11 years ago

ruby -v ruby 1.9.3p327

pkg-config --modversion QtCore 4.8.3

mhoran commented 11 years ago

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: https://github.com/thoughtbot/capybara-webkit/issues/87#issuecomment-10766865

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

jcostello commented 11 years ago

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