Ruby-on-rails 如何使用 Capybara 确认 javascript 弹出窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6930927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:42:17  来源:igfitidea点击:

How do I confirm a javascript popup with Capybara?

ruby-on-railstestingcapybara

提问by Eric M.

I've tried several examples found online, but with no luck. I am looking to confirm the confirm message of a delete link. The last attempt was the code below, but that resulted in an Capybara::NotSupportedByDriverError error.

我已经尝试了几个在网上找到的例子,但没有运气。我想确认删除链接的确认消息。最后一次尝试是下面的代码,但这导致了 Capybara::NotSupportedByDriverError 错误。

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

回答by retroGiant

Adding an answer for those hitting this in 2016 and beyond. You can now use Capybara directly to accept a confirmation box. You do this by wrapping the code that cause the confirmation box to appear in the accept_confirmfunction.

为 2016 年及以后遇到此问题的人添加答案。您现在可以直接使用 Capybara 来接受确认框。您可以通过包装导致确认框出现在accept_confirm函数中的代码来完成此操作。

accept_confirm do
  click_link 'Destroy'
end

回答by Peter Nixey

First of all switch to using Selenium as the driver by putting an @javascript tag in front of your scenario.

首先,通过在您的场景前面放置一个 @javascript 标签,切换到使用 Selenium 作为驱动程序。

The following code in your cucumber step will then confirm the dialogue:

黄瓜步骤中的以下代码将确认对话:

page.driver.browser.switch_to.alert.accept
# or
page.driver.browser.switch_to.alert.dismiss
# or
page.driver.browser.switch_to.alert.text

As @NobbZ said, this question has been asked and answered before here: How to test a confirm dialog with Cucumber?.

正如@NobbZ 所说,这个问题在此之前已被问及回答:How to test a confirm dialog with Cucumber? .

More selenium documentation available here too: http://code.google.com/p/selenium/wiki/RubyBindings#JavaScript_dialogs

此处也提供了更多 selenium 文档:http: //code.google.com/p/selenium/wiki/RubyBindings#JavaScript_dialogs

回答by magicgod

for capybara-webkit:

对于水豚 webkit:

page.driver.browser.accept_js_confirms
page.driver.browser.reject_js_confirms

which is still working, but the documentation says also:

它仍在工作,但文档还说:

page.driver.accept_js_confirms!
page.driver.accept_js_confirms!

See https://github.com/thoughtbot/capybara-webkit, search "accept_js_confirms"

参见https://github.com/thoughtbot/capybara-webkit,搜索“accept_js_confirms”

回答by Stephan Seidt

I've had timing issues with browser dialogs in a CI environment so I'm polling for a dialog before accepting it:

我在 CI 环境中遇到浏览器对话框的计时问题,所以我在接受对话框之前轮询它:

def accept_browser_dialog
  wait = Selenium::WebDriver::Wait.new(:timeout => 30)
  wait.until {
    begin
      page.driver.browser.switch_to.alert
      true
    rescue Selenium::WebDriver::Error::NoAlertPresentError
      false
    end
  }
  page.driver.browser.switch_to.alert.accept
end

回答by Michael Yagudaev

I had to use a sleep in the webkit test since it would fail everynow and then otherwise.

我不得不在 webkit 测试中使用睡眠,因为它会时不时地失败。

Here is what I came up with after reading everyones posts:

看完大家的帖子,我的想法是这样的:

if page.driver.class == Capybara::Selenium::Driver
  page.driver.browser.switch_to.alert.accept
elsif page.driver.class == Capybara::Webkit::Driver
  sleep 1 # prevent test from failing by waiting for popup
  page.driver.browser.accept_js_confirms
else
  raise "Unsupported driver"
end

回答by NobbZ

I would guess that you have to add selenium to your gem-file and configure it and capybara that capybara uses selenium as the driver.

我猜你必须将 selenium 添加到你的 gem 文件中并配置它和 capybara 使用 selenium 作为驱动程序的水豚。

I think also that How to test a confirm dialog with Cucumber?is very similar to your question, especially the accepted answer.

我还认为如何用 Cucumber 测试确认对话框?与您的问题非常相似,尤其是已接受的答案。

回答by Vasiliy Ermolovich

try to add :js => trueto your test.

尝试添加:js => true到您的测试中。

RSpec's metadata feature can be used to switch to a different driver. Use :js => true to switch to the javascript driver, or provide a :driver option to switch to one specific driver. For example:

RSpec 的元数据功能可用于切换到不同的驱动程序。使用 :js => true 切换到 javascript 驱动程序,或提供 :driver 选项切换到一个特定的驱动程序。例如:

it 'will use the default js driver' :js => true do
  ...
end

回答by Jagan

In Capybara its very simple to accept the model window. Even we can do the same in selenium but its little tough for people who are not aware about selenium.

在 Capybara 中,接受模型窗口非常简单。即使我们可以在 selenium 中做同样的事情,但对于不了解 selenium 的人来说这有点困难。

page.accept_modal #This will accept the modal window

page.accept_modal #这将接受模态窗口

page.dismiss_modal #This will Reject/Dismiss the modal window

page.dismiss_modal #这将拒绝/关闭模态窗口