Rich Seviora
Rich Seviora

Reputation: 1809

Testing for URLs with Capybara

I'm curious if there is a better way to test for the presence of a complete URL (including protocol) is present in a page.

Using Capybara 2.10.2 and Rails 5.0.0.1, I have the following setup:

rails_helper.rb

# Other config and content excluded for brevity.

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
end

test_url_spec.rb

describe 'Widget', type: :feature, js: true do
  it 'shows correct URL' do
    # Sets the default_url_options to match the session's server config.
    # default_url_options is used by the url
    self.default_url_options = {host: page.server.host, port: page.server.port} 
    # Assume that page contains the full URL.
    expect(page).to have_content(root_url) 
  end
end

I believe I can move the default_url_options settor to a before(:each) block in the appropriate context. Given the URL options are not included in Capybara by default. I'm at a loss to imagine where else I could use them.

Any thoughts?

Upvotes: 3

Views: 960

Answers (1)

Andreas
Andreas

Reputation: 998

I don't know if this will work for you, but this is what is working for me:

RSpec.configure do |config|
  config.before(:type => :feature) do
    default_url_options[:host] = "http://localhost:31337"
  end
end

Capybara.server_port = 31337
Capybara.app_host = "http://localhost:31337"

Upvotes: 1

Related Questions