Created
September 23, 2011 21:09
-
-
Save mislav/1238448 to your computer and use it in GitHub Desktop.
Browser history with Capybara: implementing "go_back"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage (works only with default Rack::Test driver): | |
page.go_back |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module HistoryManagement | |
def go_back | |
visit browser.previous_url | |
end | |
end | |
module BrowserHistory | |
def history | |
@history = [] unless defined?(@history) | |
@history | |
end | |
def previous_url | |
history[-2] or raise "can't go back in history" | |
end | |
[:get, :post, :put, :patch, :delete].each do |method| | |
define_method(method) { |path, attributes, env| | |
result = super(path, attributes, env) | |
history << path unless result.redirect? | |
result | |
} | |
end | |
end | |
Capybara::RackTest::Driver.class_eval do | |
include HistoryManagement | |
end | |
Capybara::RackTest::Browser.class_eval do | |
include BrowserHistory | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment