3

I'm using Devise and writing a test for the scenario of a user deleting their own account but I'm stuck on how I would call up the confirm box and click OK.

Here is the link and my test:

<p><%= link_to "Delete my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %></p>

spec/requests/users_spec.rb

scenario 'user deletes account' do
   make_user_and_login
   click_link('Account Settings')
   page.should have_selector('title', :text => 'Account Settings')
   click_link('Delete my account')
   # Are You Sure?
   # click OK in confirm box
   # page.should etc.....
end

How would this be done?

LearningRoR
  • 26,582
  • 22
  • 85
  • 150

3 Answers3

6

Make sure capybara is using a driver which supports javascript. Then try this:

page.driver.browser.switch_to.alert.accept

Alternately, to cancel:

page.driver.browser.switch_to.alert.dismiss
suweller
  • 514
  • 3
  • 13
robynhenderson
  • 1,348
  • 10
  • 8
  • Unfortunately I get: `Failure/Error: page.driver.browser.switch_to.alert.accept NoMethodError: undefined method 'switch_to' for #` – LearningRoR Sep 09 '12 at 21:28
  • 2
    You're using `Capybara::RackTest`, this is a headless browser without javascript support. – suweller Sep 11 '12 at 15:55
3

Try

page.evaluate_script('window.confirm = function() { return true; }')

this should work, then check for something like

page.should have_content "Account deleted"
RunsnbunsN
  • 88
  • 1
  • 11
  • I get this error when trying this: `Failure/Error: page.evaluate_script('window.confirm = function() { return true; }') Capybara::NotSupportedByDriverError: Capybara::NotSupportedByDriverError` – LearningRoR Sep 09 '12 at 19:56
  • 1
    I got this too some time ago, not shure what caused it. For me it was solved by updating my gems – RunsnbunsN Sep 10 '12 at 11:04
  • 3
    It is because you are using `Capybara::RackTest`. This driver does not support execution of any javascript – suweller Sep 11 '12 at 15:58
1

As @suweller said, we all were using the Capybara::RackTest default driver. However, without changing any settings and adding :js => true in my rspec tests I got them to pass, when before I got the same error as you were getting.

This allows me to then use page.driver.browser.switch_to.alert.accept at least.

pjammer
  • 9,489
  • 5
  • 46
  • 56