如何使用 capybara/selenium 映射浏览器对话框

How to map browser dialog using capybara/selenium

使用 capybara/selenium 处理浏览器对话框(带有 ok/cancel 按钮)的最佳方式是什么?

我发现最简单的方法是 运行 以下方法:

dialog = page.driver.browser.switch_to.alert
dialog.accept

有更好的方法吗?

无论谁否决了这个 post 请给出一个正当的理由...无缘无故地否决投票是没有帮助的

在可能的情况下,您应该尽量避免直接使用底层驱动程序。通过使用 Capybara 的 API,如果您想更换驱动程序并且存在驱动程序 API 差异,那么(理论上)您将处于更好的位置。

Capyabara's project page开始,处理模态对话框的方式是:

In drivers which support it, you can accept, dismiss and respond to alerts, confirms and prompts.

You can accept or dismiss alert messages by wrapping the code that produces an alert in a block:

accept_alert do
  click_link('Show Alert')
end

You can accept or dismiss a confirmation by wrapping it in a block, as well:

dismiss_confirm do
  click_link('Show Confirm')
end

You can accept or dismiss prompts as well, and also provide text to fill in for the response:

accept_prompt(with: 'Linus Torvalds') do
  click_link('Show Prompt About Linux')
end

All modal methods return the message that was presented. So, you can access the prompt message by assigning the return to a variable:

message = accept_prompt(with: 'Linus Torvalds') do
  click_link('Show Prompt About Linux')
end
expect(message).to eq('Who is the chief architect of Linux?')