How can I simulate the browser back button in Capybara?

James Healy picture James Healy · Oct 8, 2010 · Viewed 11.8k times · Source

We have a issue on our e-commerce site where users occasionally hit "checkout" twice and have their card charged twice.

It's a common enough bug and easy to fix, but I'd like to test the solution in our capybara setup. Once I've called click_button('checkout'), is it possible for me to pretend I'm a user hitting the browsers back button and then call click_button('checkout') a second time?

Answer

Jake Mallory picture Jake Mallory · Feb 22, 2011

You may want to try:

When(/^I go back$/) do
  page.evaluate_script('window.history.back()')
end

This will require running the senario in a javascript capable driver (selenium/celerity/akephalos)