黄瓜,确认页面开始刷新时显示的浏览器对话框

Cucumber, confirm browser dialog which shows when page starts to refresh

本文关键字:显示 浏览器 对话框 刷新 开始 确认 黄瓜      更新时间:2023-09-26

当一个场景完成后,页面刷新(在网站中,有一个javascript模式在刷新页面之前实现,它会问用户"你确定要离开页面吗?")。我需要确认那个模态。但是当我为它创建步骤时,我总是得到这个错误:

Then User clicks "Leave this page" button in the popup at online reg form
      no alert open
        (Session info: chrome=35.0.1916.114)

和代码

And(/^User clicks "([^"]*)" button in the popup at online reg form$/) do |button|
  wait = Selenium::WebDriver::Wait.new
  alert = wait.until { page.driver.browser.switch_to.alert }
  alert.accept
end

有人知道怎么处理这个吗?

您需要重写javascript中的确认对话框方法,以始终返回true

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

在触发对话框的代码行之前使用这一行,它将始终接受它,不需要更多的步骤:)