Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecation warning from Capybara

I am upgrading from rails 3.2.19 to rails 4.1.5, using rspec-rails 2.14.0.rc1 and capybara 2.4.1. All tests pass, and I only have one deprecation warning left:

[DEPRECATION] Capybara::Webkit::Driver#accept_js_confirms! is deprecated. Please use Capybara::Session#accept_confirm instead.

The line of code that is causing this is

page.driver.accept_js_confirms!

How do I change this line in order to eliminate the deprecation warning?

like image 939
Obromios Avatar asked Oct 09 '14 09:10

Obromios


3 Answers

Given that the exception says:

Please use Capybara::Session#accept_confirm instead.

You probably want:

page.accept_confirm

Note that accept_confirm is being run against the Capybara::Session instead of the driver.

This method expects a block that triggers the confirm alert to appear. For example:

page.accept_confirm do
  click_link('that_opens_confirm')
end
like image 84
Justin Ko Avatar answered Oct 10 '22 07:10

Justin Ko


Justin Ko's answer is correct as to the usage of #accept_confirm - it's

page.accept_confirm do
  #code that will trigger the modal
end

or you can do

page.accept_confirm 'Are you sure?' do
  #code that will trigger the modal
end

which will verify that "Are you sure?" is the prompt displayed in the confirm box.

In your failing test do you happen to be dealing with another modal first? capybara-webkit had a bug with multiple modals that was fixed a few days ago - https://github.com/thoughtbot/capybara-webkit/commit/86e422f94422d39e537329d64d7bfe8f6360bd8b . It's not in a relased version yet though.

like image 31
Thomas Walpole Avatar answered Oct 10 '22 07:10

Thomas Walpole


I had 50/50 success with Justin Ko's answer. The one that worked had code like this:

link_to "Reset", reset_pre_shot_description_mental_game_path(@mental_game), data: {confirm: 'Are you sure?'}, class: "small_button round", id: "reset_pre-shot"

and this test:

page.accept_confirm do
  click_link "Reset"
end

The test that fails (but has code that works in the browser) has code

link_to 'Delete', micropost, data: {confirm: 'Are you sure?'}, method: :delete

and test

page.accept_confirm do
  click_link "Delete"
end

The failure message was

Failure/Error: page.accept_confirm do
Capybara::ModalNotFound:
  Timed out waiting for modal dialog

I tried moving the method: :delete into the :data hash, but this did not help.

It turns out that the deprecation warning actually found two bugs in the code, as I was using the rails 3 syntax for confirm i.e. not using the :data hash, so my code was broken but the page.driver.accept_js_confirms! test was not picking it up. So this has been worthwhile tracking down.

like image 26
Obromios Avatar answered Oct 10 '22 09:10

Obromios