technicalpickles

Open Source ProjectsCode that Might be Useful to You

Talks I've GivenOn Technologies and Ideas

ThoughtsWhere I Sometimes Write Things

Resume If You Believe In Those

Follow Me On

GitHubIf coding is your thing

TwitterIf you tweet

Capybara (and Cucumber) and Domains


While hacking on a new application, it took a good few weeks to find out how to, using cucumber and Capybara, interact with domains that weren’t localhost. Consider this a note to my future self :)

Let’s imagine your Rails application has a parimary domain and allows users to have subdomains (as if, granted by the power of subdomain-fu). By default, everything is hitting localhost, for example:

visit "/about" # => http://localhost/about

To get around this, you can tell Capybara to use a different domain:

Capybara.default_host = "awesome.com"
visit "/about" # => http://awesome.com/about

I found it useful to make a step to navigate to a particular domain:

When /^I visit (.*)$/ do |site_domain|
  site_domain = "localhost" if site_domain == "the main domain"

  Capybara.default_host = site_domain
  visit "/"
end

Armed with this, we can write our features:

Scenario: Visit the main domain
  When I visit the main domain
  Then I should see information about the main domain

Scenario: Visit another domain
  When I visit awesome.com
  Then I should see awesomeness
comments powered by Disqus