Capybara with subdomains - default_host

后端 未结 5 737
梦毁少年i
梦毁少年i 2020-12-01 03:33

I have an app that uses subdomains to switch databases (multi-tenancy). I\'m trying to use Capybara for integration testing, and it really relies a lot on subdomains.

相关标签:
5条回答
  • 2020-12-01 03:53

    This guy has the right answer here:

    http://zurb.com/forrst/posts/Testing_Subdomains_in_Capybara-g4M

    You want to do

    Capybara.current_session.driver.reset!
    Capybara.default_host = 'http://app.mydomain.com'
    
    0 讨论(0)
  • 2020-12-01 03:54

    This is not exactly the same situation as you but this might help some people:

    For my current project, I'm using pow with many subdomains. The test suite also has to run on a different port.

    The solution depends on which version of capybara you're running.

    For the current latest release I put this in custom_env.rb:

    Capybara.server_host = 'myapp.dev'
    Capybara.server_port = 9887
    Capybara.run_server = true
    
    # I don't remember what this was for. Another team member wrote this part...
    module ActionDispatch
      module Integration #:nodoc:
        class Session
          def host
            [Capybara.server_host, Capybara.server_port].join(':')
          end
        end
      end
    end
    

    With capybara 1.1.2, I had had to make the above change but server_host becomes app_host AND modify lib/capybara/server.rb in the gem like this:

    def url(path)
      ..
      if path =~ /^http/
        path
      else
        # Was this (Capybara.app_host || "http://#{host}:#{port}") + path.to_s
        (Capybara.app_host || "http://#{host}") + ":#{port}" + path.to_s
      end
    end
    
    0 讨论(0)
  • 2020-12-01 03:59

    When you need to change the URL to include the subdomain, you can specify the app_host in your step definitions. Use a domain like lvh.me since it points to 127.0.0.1:

    Capybara.app_host = "http://#{subdomain}.lvh.me"
    

    Capybara assumes that when you're specifying an app_host that you're testing a remote server running on port 80, but in our case, we're testing a local app which is running on a random port specified by Capybara. To fix this, in your env.rb file, add this line:

    Capybara.always_include_port = true
    

    Now when you visit a page of your app...

    visit '/page'
    

    ...the url will specify the subdomain as well as the port that the app is running on.

    FYI: This worked for me using Capybara 2.0.2.

    0 讨论(0)
  • 2020-12-01 04:15

    I'm not sure of the intended use of default_host, but app_host does what you need. I've found I first need to call the rails session method host! in order to set the host string that will be passed to controllers in the request object.

    Then you need to set Capybara.app_host to tell Capybara to call your app via the web server instead of just making the calls in process. If you don't do that then Capybara wigs out when it encounters redirects and drops the host information in the second request.

    I'm not sure why this doesn't take care of the Rails request end of things automatically, but I've found that unless I set the host in both places explicitly, then I get inconsistent results.

    def set_host (host)
      host! host
      Capybara.app_host = "http://" + host
    end
    
    before(:each) do
      set_host "lvh.me:3000"
    end
    

    Then you can just use relative paths to access pages.

    Update:

    Capybara 2.x and rspec-rails 2.12.0 introduced "Feature" specs for running Capybara acceptance tests. The new FeatureExampleGroup module in rspec-rails is different from RequestExampleGroup and no longer has access to the rack-test host! method. Now you want to use default_url_options instead:

    def set_host (host)
      # host! host
      default_url_options[:host] = host
      Capybara.app_host = "http://" + host
    end
    
    0 讨论(0)
  • 2020-12-01 04:15

    as of:

    • capybara (2.4.1)
    • capybara-webkit (1.3.0)

      Capybara.server_host = "example.com"
      Capybara.server_port = 3050
      Capybara.run_server = true
      Capybara.javascript_driver = :webkit #requires capybara-webkit
      
    0 讨论(0)
提交回复
热议问题