Skip to content

Instantly share code, notes, and snippets.

@mislav
Created September 23, 2011 21:09
Show Gist options
  • Save mislav/1238448 to your computer and use it in GitHub Desktop.
Save mislav/1238448 to your computer and use it in GitHub Desktop.
Browser history with Capybara: implementing "go_back"
# Usage (works only with default Rack::Test driver):
page.go_back
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