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")
但它不起作用,因为页面没有加载和警报之前,我无法找到点击它的方式。
答案 0 :(得分:4)
看起来有点hacky,但以下似乎有效。
require 'watir-webdriver'
require 'webdriver-user-agent'
driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => phone.to_sym, :orientation => :landscape)
b = Watir::Browser.new driver
begin
b.goto "file:///test.html"
rescue Selenium::WebDriver::Error::UnhandledAlertError
b.alert.ok
end
# Continue to use the page
基本上,你: