diff --git a/lib/capybara/driver/base.rb b/lib/capybara/driver/base.rb index 4da36fff..b985ff87 100644 --- a/lib/capybara/driver/base.rb +++ b/lib/capybara/driver/base.rb @@ -10,6 +10,10 @@ class Capybara::Driver::Base raise NotImplementedError end + def refresh + raise NotImplementedError + end + def find_xpath(query) raise NotImplementedError end diff --git a/lib/capybara/rack_test/browser.rb b/lib/capybara/rack_test/browser.rb index 51c37763..65fb0a24 100644 --- a/lib/capybara/rack_test/browser.rb +++ b/lib/capybara/rack_test/browser.rb @@ -22,6 +22,11 @@ class Capybara::RackTest::Browser process_and_follow_redirects(:get, path, attributes) end + def refresh + reset_cache! + request(last_request.fullpath, last_request.env) + end + def submit(method, path, attributes) path = request_path if not path or path.empty? process_and_follow_redirects(method, path, attributes, {'HTTP_REFERER' => current_url}) diff --git a/lib/capybara/rack_test/driver.rb b/lib/capybara/rack_test/driver.rb index c984f7b7..99743874 100644 --- a/lib/capybara/rack_test/driver.rb +++ b/lib/capybara/rack_test/driver.rb @@ -44,6 +44,10 @@ class Capybara::RackTest::Driver < Capybara::Driver::Base browser.visit(path, attributes) end + def refresh + browser.refresh + end + def submit(method, path, attributes) browser.submit(method, path, attributes) end diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 435580cb..6698d329 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -67,6 +67,13 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base browser.navigate.to(path) end + def refresh + accept_modal(wait: 0.1) do + browser.navigate.refresh + end + rescue Capybara::ModalNotFound + end + def go_back browser.navigate.back end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index ffb53f81..50fb1fca 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -58,7 +58,7 @@ module Capybara ] SESSION_METHODS = [ :body, :html, :source, :current_url, :current_host, :current_path, - :execute_script, :evaluate_script, :visit, :go_back, :go_forward, + :execute_script, :evaluate_script, :visit, :refresh, :go_back, :go_forward, :within, :within_element, :within_fieldset, :within_table, :within_frame, :switch_to_frame, :current_window, :windows, :open_new_window, :switch_to_window, :within_window, :window_opened_by, :save_page, :save_and_open_page, :save_screenshot, @@ -269,6 +269,15 @@ module Capybara driver.visit(visit_uri.to_s) end + ## + # + # Refresh the page + # + def refresh + raise_server_error! + driver.refresh + end + ## # # Move back a single entry in the browser's history. diff --git a/lib/capybara/spec/session/refresh_spec.rb b/lib/capybara/spec/session/refresh_spec.rb new file mode 100644 index 00000000..75f1fd73 --- /dev/null +++ b/lib/capybara/spec/session/refresh_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true +Capybara::SpecHelper.spec '#refresh' do + it "reload the page" do + @session.visit('/form') + expect(@session).to have_select('form_locale', selected: 'English') + @session.select('Swedish', from: 'form_locale') + expect(@session).to have_select('form_locale', selected: 'Swedish') + @session.refresh + expect(@session).to have_select('form_locale', selected: 'English') + end + + it "raises any errors caught inside the server", requires: [:server] do + quietly { @session.visit("/error") } + expect do + @session.refresh + end.to raise_error(TestApp::TestAppError) + end + + it "it reposts" do + @session.visit('/form') + @session.select('Sweden', from: 'form_region') + @session.click_button('awesome') + expect { + @session.refresh + sleep 2 + }.to change{ extract_results(@session)['post_count'] }.by(1) + end +end diff --git a/lib/capybara/spec/test_app.rb b/lib/capybara/spec/test_app.rb index 4119b320..4e197b71 100644 --- a/lib/capybara/spec/test_app.rb +++ b/lib/capybara/spec/test_app.rb @@ -17,6 +17,7 @@ class TestApp < Sinatra::Base set :raise_errors, true set :show_exceptions, false + @@form_post_count = 0 # Also check lib/capybara/spec/views/*.erb for pages not listed here get '/' do @@ -147,7 +148,8 @@ class TestApp < Sinatra::Base end post '/form' do - '
' + params[:form].to_yaml + '
' + @@form_post_count += 1 + '
' + params[:form].merge({"post_count" => @@form_post_count}).to_yaml + '
' end post '/upload_empty' do