diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 6efa7c6f..c78fa61e 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -108,6 +108,8 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base # Use instance variable directly so we avoid starting the browser just to reset the session return unless @browser + window_handles.reject { |handle| handle == current_window_handle }.each { |win| close_window(win) } + navigated = false start_time = Capybara::Helpers.monotonic_time begin diff --git a/lib/capybara/spec/session/reset_session_spec.rb b/lib/capybara/spec/session/reset_session_spec.rb index 780cf062..913dbf3b 100644 --- a/lib/capybara/spec/session/reset_session_spec.rb +++ b/lib/capybara/spec/session/reset_session_spec.rb @@ -63,6 +63,24 @@ Capybara::SpecHelper.spec '#reset_session!' do expect(@session.current_path).to eq("/") end + it "closes extra windows", requires: [:windows] do + @session.visit('/with_html') + @session.open_new_window + @session.open_new_window + expect(@session.windows.size).to eq 3 + @session.reset_session! + expect(@session.windows.size).to eq 1 + end + + it "closes extra windows when not on the first window", requires: [:windows] do + @session.visit('/with_html') + @session.switch_to_window(@session.open_new_window) + @session.open_new_window + expect(@session.windows.size).to eq 3 + @session.reset_session! + expect(@session.windows.size).to eq 1 + end + context "When reuse_server == false" do before do @reuse_server = Capybara.reuse_server