Reputation: 15168
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?
Upvotes: 35
Views: 13727
Reputation: 421
At least with capybara
2.10
and selenium-webdriver
2.53
this works:
When(/^I go back$/) do
page.go_back
end
It's basically a shortcut for jbarr's answer. For details more see the capybara documentation on go_back
.
BTW: The counter part is page.go_forward
.
Upvotes: 8
Reputation: 464
You can use page.driver.go_back
, if you are using webkit as your capybara javascript driver via the capybara-webkit
gem. Also requires :js => true
for the scenario
.
Upvotes: 11
Reputation: 2066
Thanks! This question and answer helped me a lot!
Just to add to @Jake Mallory's answer, selenium is now part of capybara and you can fairly easily run javascript in the test by adding :js => true
(and possibly a couple more tweaks) as described in these two tutorials:
http://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/ http://railscasts.com/episodes/257-request-specs-and-capybara?view=asciicast
Upvotes: 0
Reputation: 401
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)
Upvotes: 40
Reputation: 5335
I've used this method in Webrat. I'm sure something similar for Capybara would work.
When(/^I go back$/) do
visit request.env['HTTP_REFERER']
end
Side note: the "redirect_to :back" method didn't work for me for whatever reason.
Upvotes: 1