Subdomains with Capybara and a JS Webdriver
April 7th, 2018
In situations where you are working with subdomains in your Rails application, writing feature specs that require to be ran with a JS webdrive will cause your specs to fail. In order to fix this we will need to configure Capaybara and override the default host.
In this example we will be using lvh.me, but it you are using
/etc/hosts for custom domains for development you can use those domains instead.
By default Capybara uses http://www.example.com on port 80 for feature specs, however to test subdomains we will need to tell Capybara to always include the port number as we will be pointing to localhost.
Add or modify your existing Capybara config to always include the port number.
1 2 3 4 5 # spec/support/capybara.rb Capybara.configure do |config| config.always_include_port = true end
Feature Spec Example
In a before hook, you can simply add
default_url_options[:host] = "http://lvh.me" and your feature specs will run correctly.
1 2 3 4 5 6 7 8 RSpec.feature "Example", type: :feature, js: true do before do default_url_options[:host] = "http://lvh.me" # rest of code end # rest of code end