Deprecation warning from Capybara

喜夏-厌秋 提交于 2019-11-30 09:14:17

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

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.

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.

I replaced page.driver.accept_js_confirms! with:

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

And the test passed.

This was from the documentation here: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#evaluate_script-instance_method

And help from the internets. Now, that doesn't still tell us how to use accept_confirm so I'm still looking for that answer.

The actual code looks like this:

# Execute the block, accepting a confirm.
#
# @macro modal_params
#
def accept_confirm(text_or_options=nil, options={}, &blk)
  if text_or_options.is_a? Hash
    options=text_or_options
  else
    options[:text]=text_or_options
  end

  driver.accept_modal(:confirm, options, &blk)
 end

Honestly, I think it's just page.accept_confirm with SOMETHING else, but I can't figure out what or passing in that block.

Works perfect for me:

page.execute_script('window.confirm = function() { return true }')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!