2014-10-09 164 views
8

我使用rspec-rails 2.14.0.rc1和capybara 2.4.1从rails 3.2.19升级到rails 4.1.5。所有的测试都通过了,我只有一个弃用警告左:水豚的弃用警告

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

的代码,是造成这种情况的线

page.driver.accept_js_confirms! 

如何以消除弃用警告改变这一行?

+0

page.driver.browser.accept_confirm? – coorasse 2014-10-09 12:25:51

+0

试过我自己,因为我有同样的问题。它不起作用。它似乎确认accept_modal(:confirm,options,&blk),但给出参数错误。我仍然在寻找答案。 – Art 2014-10-10 17:29:17

回答

10

鉴于例外说:

请使用水豚::活动#accept_confirm代替。

你可能想:

page.accept_confirm 

注意accept_confirm正在对水豚::会议,而不是驱动程序运行。

此方法需要一个触发确认警报出现的块。例如:

page.accept_confirm do 
    click_link('that_opens_confirm') 
end 
+0

这不起作用,但你是对的,它接近正确的答案。它正在寻找一个街区。我只是不明白如何传递它。下面是它的原始代码。 #执行块,接受确认。如果是text_or_options.is_a?或者是text_or_options.is_a?散列 选项= text_or_options 别的 选项[:文本] = text_or_options 端 driver.accept_modal(:确认,选择,和BLK) 端 – Art 2014-10-10 19:13:54

+0

基于该[规格](https://github.com/jnicklas/水豚/ blob/master/lib /水豚/ spec/session/accept_confirm_spec.rb),该块应该是触发确认出现的动作。 – 2014-10-10 19:29:51

+0

这很有帮助,但仍不能完全放置它。我正在使用黄瓜,并在page.driver.console_messages.first的上一步中弹出提示。 切换到会话使用page.accept_confirm应该工作,但并没有因为我不及格的东西在我还是个菜鸟,虽然:) – Art 2014-10-10 19:36:11

0

我替换了page.driver.accept_js_confirms!用:

page.execute_script( 'window.confirm =函数(){返回true}')

并通过了测试。

这是从文档浏览:http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#evaluate_script-instance_method

而来自互联网络的帮助。现在,这还不能告诉我们如何使用accept_confirm,所以我仍然在寻找这个答案。

实际的代码如下所示:

# 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 

老实说,我认为它只是与别的page.accept_confirm,但我不能找出或通过在该块。

3

我和Justin Ko的回答有50/50成功。该工作的人有这样的代码:

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" 

这个测试:

page.accept_confirm do 
    click_link "Reset" 
end 

失败(但在浏览器中运行的代码)的代码

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

测试并测试

page.accept_confirm do 
    click_link "Delete" 
end 

失败URE消息是

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

我试过method: :delete移动到:data哈希值,但这并没有帮助。

事实证明,弃用警告居然发现代码中的两个错误,因为我用的是轨道3语法确认,即不使用:data散,所以我的代码被打破,但page.driver.accept_js_confirms!试验中未捡起来。所以这值得跟踪。

+0

正如在回答user2209090识别错误时提到的那样,在测试中有两种模式,当我将每种模式分离到不同的场景中时,“删除”测试都起作用了,所以我现在把贾斯汀高的答案作为100%关于user229090答案中的错误 – Obromios 2014-10-15 10:02:55

5

官高的答案是为#accept_confirm的使用正确的 - 这是

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

,或者你可以做

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

这将验证“你确定?”是确认框中显示的提示。

在你失败的测试中,你碰巧正在处理另一个模态吗? capybara-webkit在几天前修复了多个模块的错误 - https://github.com/thoughtbot/capybara-webkit/commit/86e422f94422d39e537329d64d7bfe8f6360bd8b。尽管如此,它还没有出现在新版本中。

+0

是的,我有两种模式,我将它们分成了两种不同的场景,并且都通过了测试,除了你引用的bug外,一切都很好。 – Obromios 2014-10-15 09:59:44

0

工作非常适合我:

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