Capybara: use current_path_info instead of current_path / current_url

Capybara has current_url, which returns an url that is unmatcheable since it includes a randomized port, and current_path with contains neither query nor fragment(aka hash/anchor) and is therefore rather useless.

Behold the perfect current_path_info, which returns the full path and query + fragment.

Code

def current_path_info
  current_url.sub(%r{.*?://},'')[%r{[/\?\#].*}] || '/'
end

3 thoughts on “Capybara: use current_path_info instead of current_path / current_url

  1. Thanks for this… I went with:

    “`
    def current_url_fragment
    URI.parse(current_url).fragment
    end
    “`

    which I think is a little bit more elegant, perhaps?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s