watir webdriver 和 javascript 对话框我有示例并且无法在测试中摆脱该警报。这是 javascript
<script>
alert("hello.\n\nPress OK to go.");
</script>
然后我需要点击它并加载页面
require 'watir-webdriver'
require 'webdriver-user-agent'
b = Watir::Browser.new Webdriver::UserAgent.driver(:browser => :chrome, :agent => phone.to_sym, :orientation => :landscape)
b.goto "file:///test.html"
错误是
/var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/response.rb:51:in `assert_ok': unexpected alert open (Selenium::WebDriver::Error::UnhandledAlertError)
(Session info: chrome=30.0.1599.114)
(Driver info: chromedriver=2.4.226074,platform=Linux 3.5.0-17-generic x86)
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/bridge.rb:634:in `raw_execute'
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/bridge.rb:612:in `execute'
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/remote/bridge.rb:166:in `getCurrentUrl'
from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/common/driver.rb:120:in `current_url'
from /var/lib/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir-webdriver/browser.rb:111:in `url'
from /var/lib/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir-webdriver/browser.rb:80:in `goto'
from test.rb:13:in `block in <main>'
from test.rb:7:in `each'
from test.rb:7:in `<main>'
和
b.execute_script "window.onbeforeunload = null"
如果 javascript 与 window.onbeforeunload 不同,这可以正常工作
但是这个警报给了我错误,之后的一切都不再起作用了,因为我不能点击那个警报,所以页面加载了。
# don't return anything for alert
browser.execute_script("window.alert = function() {}")
# return some string for prompt to simulate user entering it
browser.execute_script("window.prompt = function() {return 'my name'}")
# return null for prompt to simulate clicking Cancel
browser.execute_script("window.prompt = function() {return null}")
# return true for confirm to simulate clicking OK
browser.execute_script("window.confirm = function() {return true}")
# return false for confirm to simulate clicking Cancel
browser.execute_script("window.confirm = function() {return false}")
# don't return anything for leave page popup
browser.execute_script("window.onbeforeunload = null")
但它不起作用,因为页面没有加载并且警报在之前,我找不到点击它的方法。