如何告诉capybara-webkit不要忽略确认框(或只是测试他们的内容)

Emi*_*äck 3 ruby-on-rails cucumber capybara ruby-on-rails-3 capybara-webkit

所以我有一个场景:

Given I signed in to my business account
And there is one customer named "Samantha Seeley"
When I go to the customers management page
Then "Delete" should show a confirmation dialog saying "Are you sure?"
Run Code Online (Sandbox Code Playgroud)

链接很简单:

<%= link_to 'Delete' customer_path(customer), method: :delete, confirm: 'Are you sure?' do %>
Run Code Online (Sandbox Code Playgroud)

因此,当我点击Chrome中的链接时,此代码确实会显示确认对话框,但使用如下页面渲染:

Then /^"(.*?)" should show a confirmation dialog saying "(.*?)"$/ do |arg1, arg2|
  click_on arg1
  page.driver.render "tmp/screenshot.jpg"
end
Run Code Online (Sandbox Code Playgroud)

我相信我可以确认javascript似乎没有被评估(没有显示确认对话框).这个假设可能是错的,我不确定.

谢谢你的帮助.

geo*_*ock 5

在GitHub上提出了类似的问题:https://github.com/thoughtbot/capybara-webkit/issues/84

结论是使用像Jesse CookeKen Collins所建议的这样的东西:

def handle_js_confirm(accept=true)
  page.evaluate_script "window.original_confirm_function = window.confirm"
  page.evaluate_script "window.confirm = function(msg) { return #{!!accept}; }"
  yield
ensure
  page.evaluate_script "window.confirm = window.original_confirm_function"
end
Run Code Online (Sandbox Code Playgroud)

这无法帮助您测试确认对话框的内容(尽管可以轻松扩展它),但它可以让您测试用户单击"确定"或"取消"时会发生什么.

在这种特定情况下,您期望一个确认对话框,因为您正在使用生成一个的Rails助手.值得考虑的是,这是否需要在您自己的应用中进行测试覆盖; Rails已经有测试来覆盖它.