diff --git a/lib/capybara/rack_test/browser.rb b/lib/capybara/rack_test/browser.rb index 4e4e23b1..ebd226ca 100644 --- a/lib/capybara/rack_test/browser.rb +++ b/lib/capybara/rack_test/browser.rb @@ -8,6 +8,7 @@ class Capybara::RackTest::Browser def initialize(driver) @driver = driver + @current_fragment = nil end def app @@ -42,6 +43,7 @@ class Capybara::RackTest::Browser end def process_and_follow_redirects(method, path, attributes = {}, env = {}) + @current_fragment = build_uri(path).fragment process(method, path, attributes, env) return unless driver.follow_redirects? @@ -65,7 +67,7 @@ class Capybara::RackTest::Browser method = method.downcase new_uri = build_uri(path) @current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port) - + @current_fragment = new_uri.fragment || @current_fragment reset_cache! send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {})) end @@ -83,7 +85,9 @@ class Capybara::RackTest::Browser end def current_url - last_request.url + uri = build_uri(last_request.url) + uri.fragment = @current_fragment if @current_fragment + uri.to_s rescue Rack::Test::Error '' end diff --git a/lib/capybara/spec/session/current_url_spec.rb b/lib/capybara/spec/session/current_url_spec.rb index c797a650..565e8dc4 100644 --- a/lib/capybara/spec/session/current_url_spec.rb +++ b/lib/capybara/spec/session/current_url_spec.rb @@ -22,7 +22,7 @@ Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do expect(@session.current_url.chomp('?')).to eq("#{scheme}://#{s.host}:#{s.port}#{path}") expect(@session.current_host).to eq("#{scheme}://#{s.host}") # no port - expect(@session.current_path).to eq(path) + expect(@session.current_path).to eq(path.split('#')[0]) # Server should agree with us expect(@session).to have_content("Current host is #{scheme}://#{s.host}:#{s.port}") if path == '/host' end @@ -84,6 +84,16 @@ Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do should_be_on 0, '/landed' end + it 'maintains fragment' do + @session.visit("#{bases[0]}/redirect#fragment") + should_be_on 0, '/landed#fragment' + end + + it 'redirects to a fragment' do + @session.visit("#{bases[0]}/redirect_with_fragment") + should_be_on 0, '/landed#with_fragment' + end + it 'is affected by pushState', requires: [:js] do @session.visit('/with_js') @session.execute_script("window.history.pushState({}, '', '/pushed')") diff --git a/lib/capybara/spec/test_app.rb b/lib/capybara/spec/test_app.rb index fae3836a..266e2a6f 100644 --- a/lib/capybara/spec/test_app.rb +++ b/lib/capybara/spec/test_app.rb @@ -33,6 +33,10 @@ class TestApp < Sinatra::Base redirect '/redirect_again' end + get '/redirect_with_fragment' do + redirect '/landed#with_fragment' + end + get '/redirect_again' do redirect '/landed' end