我遇到的问题是,有时JS对话行为在被测试的应用程序中是不可预测的。我希望我可以说我可以做任何事情,但我不能,因为我只是经历了黄瓜这种奇怪的行为。此外,测试实际的对话框并不是我的考虑因素。换句话说,我们只想在它们发生时点击并解雇它们。
begin
find('div', :text => 'Cancel').double_click # Click cancel
accept_browser_dialog # We've got two dialogue boxes to click through
accept_browser_dialog # That makes two.. but what if a third was created?
# Wouldn't it be great if I could just dismiss them as they appeared?
rescue Selenium::WebDriver::Error::UnhandledAlertError # <- SOMETHING LIKE THIS
accept_browser_dialog # Handled easy as pie.. in theory
end # etc etc etc
请参阅上面标记的行。每次单击“取消”时,都会有两个对话点击,但有时double_click
调用会调出第三个对话框。而不是通过计数对话等来玩游戏,有没有什么方法可以在发生未处理的警报错误时单独解除它们?
答案 0 :(得分:1)
如果您指的是系统模式框(警告,确认,提示)当使用selenium驱动程序时,您可以尝试接受打开的警报并捕获引发的错误(如果不存在)。
find('div', :text => 'Cancel').double_click # Click cancel
accept_browser_dialog # We've got two dialogue boxes to click through
accept_browser_dialog # That makes two.. but what if a third was created?
begin
accept_alert(wait: 0.1)
rescue Capybara::ModalNotFound
end
答案 1 :(得分:0)
如果您知道自己在做什么,并且真的必须绕过所有JavaScript警报和对话而不管后果如何,您可以随时follow this advice:
driver.execute_script("window.alert = function() {}")
driver.execute_script("window.prompt = function() {return null}")
driver.execute_script("window.confirm = function() {return true}")
这比尝试处理不可预测的变量行为更加清晰。