capybara-envjs icon indicating copy to clipboard operation
capybara-envjs copied to clipboard

No support for subdomains

Open iamnader opened this issue 14 years ago • 35 comments

Details described in this thread: http://groups.google.com/group/ruby-capybara/browse_thread/thread/69e85fa4bbc40cb1

Here is a simple test that passes using selenium, but fails using envjs: http://gist.github.com/551764

Thanks

iamnader avatar Aug 26 '10 16:08 iamnader

Thanks. I'll try to look at it this weekend.

smparkes avatar Aug 26 '10 17:08 smparkes

This doesn't seem to be a free standing test. It's got a minor syntax error which is easy to fix, but I'm not sure the context is should be placed in. Did you have one?

smparkes avatar Sep 02 '10 17:09 smparkes

I just noticed the 'require "test_helper"'. You should be able to remove that. It's an integration test. So if you put in test/integration and run a rake test:integration it should run right?

iamnader avatar Sep 02 '10 18:09 iamnader

Nope. It's giving me some kind of error on uninitialized constant ActionController. I'm not really that familiar with rails integration tests (I use rspec) so if you could throw together whatever it wants ...

smparkes avatar Sep 02 '10 18:09 smparkes

OK, I created a very simple app that illustrates the problem: http://github.com/iamnader/envjs-subdomain-example

From the README: == ENVJS SUBDOMAIN EXAMPLE

This is a very simple app that illustrates this bug in capybara-envjs: http://github.com/smparkes/capybara-envjs/issues#issue/31

Should be very straightforward to run, directions below from the README. Let me know if you have any issues.

== Getting Started

  1. Run 'bundle install' to download rails 3 and capybara-envjs (and dependencies)
  2. Run 'rake test:integration' to see a very simple test fail trying to navigate to a subdomain.
  3. In test/integration/subdomain_test.rb uncomment the Selenium driver line and see the test pass using Selenium

iamnader avatar Sep 02 '10 19:09 iamnader

Thanks. I appreciate the effort. I'll look at it in the morning (I already spent as much time as I can for today tracking down an envjs bug that surfaced with the newest capy changes).

smparkes avatar Sep 02 '10 19:09 smparkes

Okay. I've tweaked things.

First, as far as I can tell, your test isn't testing envjs, it's testing rack::test. I tweaked it and pushed to http://github.com/smparkes/envjs-subdomain-example.

Next, you have to set default_host if you want to use rack test. I'm not 100% sure if that's what you wanted. Did you want this to run a server like it does with selenium, or run within envjs? I kinda assumed the later. Was I wrong?

At this point, the envjs driver won't start a server like the selenium server does. It's possible, but I haven't needed it. It's come up once, that I'm aware of.

Next, I fixed envjs so it will actually do this. Before, it wasn't allowing default_host and app_host to be changed on the fly. I guess that's okay ... Fixed in master (which requires capy master)

Finally, rack-test does not support port numbers, as far as I can tell. envjs passes them on happily, but rack test will barf on them. That would be a rack-test issue ...

smparkes avatar Sep 03 '10 16:09 smparkes

Excellent. I was actually testing envjs, it was just set as the default driver in the test_helper file. I would prefer to always use it, I only use Selenium right now for subdomain tests because of the limitation. Yes, you're correct I don't want to run a server, just envjs. So I want to take your updated version of my sample app, and point it at capy_envjs master to use your latest code and see if the test passes.

But it looks like the capybara-envjs github repo doesn't have a Gemfile. How would you recommend I use the version on master? Install it as a plugin?

Adding this to my Gemfile: gem "capybara-envjs", :require => 'capybara/envjs', :git => 'git://github.com/smparkes/capybara-envjs.git', :branch => 'master'

gives me this error: Could not find gem 'capybara-envjs (>= 0, runtime)' in git://github.com/smparkes/capybara-envjs.git (at master).

iamnader avatar Sep 03 '10 17:09 iamnader

I thought you were probably testing envjs ... seemed like you'd notice the difference.

Do you need the port in there? It didn't look like it'd be too terribly hard to hack rack::test to support ports.

You'll need to point at both capy_envjs master and capy master: I try to make capy_envjs master track capy and there are a number of unreleased changes in capy that capy_envjs is tied to.

I usually just download the repo, build the gem, and install it (which means I need to pre-bump the version in the repo, which I haven't done yet). I've heard that you should just be able to point bundler at a git clone and it should work?

smparkes avatar Sep 03 '10 17:09 smparkes

OK, I pre-bumped the version # to 0.1.7, and installed the gem. My gemfile:

source 'http://rubygems.org'

Rails essentials

gem 'rails', '3.0.0.rc' gem 'ruby-debug'

Testing

group :test do gem "xpath" gem "capybara", :git => "git://github.com/jnicklas/capybara.git", :branch => "master" gem "capybara-envjs", '0.1.7' end

Everything else is the same and I'm getting a new exception when running the test.

E Finished in 3.634254 seconds.

  1. Error: test_simple_subdomain_test(SubdomainTest): Johnson::Error: document.getElementById("search-text") is null /test/integration/subdomain_test.rb:15:in `test_simple_subdomain_test'

1 tests, 0 assertions, 0 failures, 1 errors

Is it working for you?

iamnader avatar Sep 03 '10 17:09 iamnader

No, that's what I got, too. But, then, I don't actually see that id anywhere in the document. Did I miss something?

smparkes avatar Sep 03 '10 18:09 smparkes

If you comment out the default_host line you added the test at least finishes, but the assert fails. However, the selenium version that previously passed now fails, because it requires the port.

Can you update the sample app so that the test passes? Or am I missing something? I thought you said that subdomains were now supported in the version of capy-envjs on master?

iamnader avatar Sep 03 '10 18:09 iamnader

If you comment out the default_host line you added the test at least finishes, but the assert fails.

Don't know what this means. Do you mean the Johnson error? Have you actually tried loading that page in a browser and looked a the Javascript console? You get the same error. So I think capy-envjs is doing the right thing ...

... as long as you add default_host == app_host without http:// and without a port. If you're adding the port simply because you can't bind to port 80, then you can just leave the port off when testing. If you need port support, somebody needs to add it to Rack::Test. This isn't a limitation of capy or capy-envjs, it's something rack test doesn't support, which both capy and capy-envjs use.

smparkes avatar Sep 03 '10 18:09 smparkes

You were right about the Johnson error. Weird thing is the rails generator creates that. So the good news is I got basic subdomain support working with envjs! The bad news is it looks like it only works once per test. So for example if at the beginning of my test I set the default and app hosts they are set correctly and the navigation goes to the subdomains. If half way through a test I want to go to a different subdomain and I change the app_host and default_host values they don't take and requests still go to the first subdomain.

Any thoughts on how to handle that?

Thanks for the effort on this. envjs is SO much more convenient than Selenium

iamnader avatar Sep 06 '10 16:09 iamnader

Weird thing is the rails generator creates that.

Yeah, I didn't look at it at first for that reason, as well.

Are you saying you're hitting visit("/foo") and the changing values? I guess I'm not sure what should happen. Maybe that should work. If you're trying to hit a link on a page and have it go to a different url, I don't see that ever working: the page knows where it was loaded from and hacking that seems like a bad idea: to un-browser-like.

Doing more than one visit in a test, I'm not sure about. Is that what you're doing? Do you have an example?

smparkes avatar Sep 07 '10 16:09 smparkes

Yep, I'm doing more than one visit. Just set the default and app hosts, visit a page, check the url. All good. Set the default and app to a different subdomain, visit a page, the url is still set to the original. My test is checking that actions I do on one subdomain, don't show up on another.

iamnader avatar Sep 07 '10 16:09 iamnader

If you want to push a test somewhere, I can look at it, and either make it work or tell you why it can't.

smparkes avatar Sep 07 '10 16:09 smparkes

Hi, it's hard to bundle a test using your latest because I can't reference your head from bundler.

But if you take the same test from here: http://github.com/smparkes/envjs-subdomain-example/blob/master/test/integration/subdomain_test.rb

And just do two visits like this:

# this test passes using selenium, fails using the default envjs
Capybara.current_driver = :envjs

subdomain = "something"
Capybara.default_host = "#{subdomain}.lvh.me"
Capybara.app_host = "http://#{subdomain}.lvh.me"
visit root_path
assert current_url.include?(subdomain)

subdomain2 = "another"
Capybara.default_host = "#{subdomain2}.lvh.me"
Capybara.app_host = "http://#{subdomain2}.lvh.me"
visit root_path
assert current_url.include?(subdomain2)

iamnader avatar Sep 07 '10 17:09 iamnader

I'll give it a shot ...

smparkes avatar Sep 10 '10 20:09 smparkes

Hmm ... works for me? I've updated my version of the example. Am I testing it wrong?

Jonas says a new version of the capy should be out soon ... (though it sounds like I'm not the only one backed up ...)

smparkes avatar Sep 14 '10 16:09 smparkes

Sorry, I just got around to testing this. Unfortunately I can't get your test to pass. The version in rubygems doesn't work. If I build a new gem from your master using the capybara rubygem I get an XHR can't connect error. If I use your master and the capy master I get this error: Capybara::DriverNotFoundError: no driver called :envjs was found, available drivers: :culerity, :celerity, :rack_test, :selenium

And this is all basically moot as I can't deploy your master as it isn't bundle friendly (vendoring doesn't work). So I guess I'm back to Selenium til there are new official envjs/capy gem versions

Thanks

iamnader avatar Sep 25 '10 00:09 iamnader

I'll try to get the gemspec stuff fixed ...

smparkes avatar Sep 26 '10 19:09 smparkes

Jonas did the hoe->bundler conversion so you should be able to use the git repo as a gem.

smparkes avatar Oct 24 '10 17:10 smparkes

Has this been addressed yet? I'm having issues with getting this to work.

geetarista avatar Feb 23 '11 21:02 geetarista

Last I checked, it worked. Do you have a failing example?

smparkes avatar Feb 24 '11 15:02 smparkes

I finally got it working. I had to tweak my settings before I finally got it right.

geetarista avatar Feb 25 '11 06:02 geetarista

I'm having an issue with subdomains too. When I switch the subdomain in one test, then switch it again in another test, then again in another test, in every test after the first, rails still thinks its on the first subdomain. I've created a sample app too with failing tests using rspec and steak.

This test fails: https://github.com/snelson/capybara-envjs-steak-subdomain-example/blob/master/spec/acceptance/subdomains_spec.rb

I also can't get iamnader's example to pass. Any advice would be much appreciated.

Thanks

snelson avatar Mar 24 '11 17:03 snelson

I was able to find a workaround for this by using multiple sessions, and opening a new session when the subdomain is switched. I wouldn't consider this a fix though as I think there should be able to do this within one session, like if to test domain-wide authentication. This may not even be a capybara-envjs issue, it might be a capybara issue.

The workaround can be found here:

https://github.com/snelson/capybara-envjs-steak-subdomain-example/commit/3dc26a0a0baea9969bc2aa199cbf0512a6e13c0e

I came to this workaround when reading about a similar problem and solution at:

http://www.automation-excellence.com/blog/racking-my-brains

What's strange about his issue is that it seemed to be reverse, where selenium was working and rack test wasn't.

I'd love to help get a fix into either capybara-envjs or capybara, so any insight would be much appreciated, as the code is all still pretty unfamiliar to me.

snelson avatar Mar 25 '11 02:03 snelson

I've always had a hard time keeping track of this. If you look at the capy google group, you'll find a few threads about it. I don't use it, so I've tried to make it work now and again, but either I've never gotten it to work quite right or it gets broken again. I'm not entirely sure Jonas thinks it should be supported. I seem to remember him saying that mucking with .default_host/.app_host was evil.

smparkes avatar Mar 25 '11 02:03 smparkes

Jonas says in a number of places to use visit('http://subdomain.example.com'), that is, a FQDN, to switch the domain name, one of those places is here:

http://groups.google.com/group/ruby-capybara/browse_thread/thread/f6a109ec6d254bc8/5f9a726bc4de0b7d?lnk=gst&q=subdomain#5f9a726bc4de0b7d

That doesn't work either though. I've modified the test on another branch to show it using the default driver.

https://github.com/snelson/capybara-envjs-steak-subdomain-example/blob/fails_using_visit/spec/acceptance/subdomains_spec.rb

I'll setup an issue with Capybara and post the url here. Thanks for getting back, and thanks for all the work you've put into the envjs driver, we owe you big time.

snelson avatar Mar 25 '11 03:03 snelson

I've started a pull request with capybara so that we can use visit() to switch the subdomain and have it persist across subsequent requests.

https://github.com/jnicklas/capybara/pull/307

We'll see what happens with that. I'll follow up here.

snelson avatar Mar 25 '11 07:03 snelson

Thanks!

smparkes avatar Mar 25 '11 20:03 smparkes

Looks like its already in the works, hopefully we'll see it soon.

https://github.com/jnicklas/capybara/pull/307

snelson avatar Mar 25 '11 20:03 snelson

Cool. Let me know how it falls out. Depending on what the changes are, they might require driver changes.

smparkes avatar Mar 25 '11 21:03 smparkes

Absolutely, will do.

snelson avatar Mar 25 '11 22:03 snelson