使用watir safari webdriver时,webdriver会在遇到警告弹出窗口时立即终止。是否有任何变通办法可以让Safari webdriver在警报时正常运行,或者防止警报出现?
$driver.button(:xpath, "//button[@class='btn btn-success btn-success-red']").wait_until_present
$driver.button(:xpath, "//button[@class='btn btn-success btn-success-red']").click
$driver.alert.ok()
$log.info("create organization successful")
错误:
/Users/usr/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/safari/bridge.rb :73:在
raw_execute': A modal dialog was opened. The SafariDriver does not support interacting with modal dialogs. To avoid hanging your test, the alert has been dismissed. For more information, see http://code.google.com/p/selenium/issues/detail?id=3862 (Selenium::WebDriver::Error::UnhandledAlertError) from /Users/usr/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/bridge.rb:612:in
执行'来自 /Users/usr/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/bridge.rb:369:在clickElement' from /Users/usr/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/common/element.rb:54:in
点击“来自” /Users/usr/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.6.10/lib/watir-webdriver/elements/element.rb:132:在 来自click' from /Users/usr/Documents/workspace/TTQA Safari/tttestlibrary.rb:47:in
create_org' / Users / usr / Documents / workspace / TTQA Safari / ttorgusercompare.rb:23:inorg_user_compare' from /Users/usr/Documents/workspace/TTQA Safari/ttorgusercompare.rb:34:in
“
谢谢!
答案 0 :(得分:2)
如watirwebdriver.com/javascript-dialogs所述,您可以覆盖Javascript函数,以便不显示警报:
# don't return anything for alert
$driver.execute_script("window.alert = function() {}")
# return some string for prompt to simulate user entering it
$driver.execute_script("window.prompt = function() {return 'my name'}")
# return null for prompt to simulate clicking Cancel
$driver.execute_script("window.prompt = function() {return null}")
# return true for confirm to simulate clicking OK
$driver.execute_script("window.confirm = function() {return true}")
# return false for confirm to simulate clicking Cancel
$driver.execute_script("window.confirm = function() {return false}")
# don't return anything for leave page popup
$driver.execute_script("window.onbeforeunload = null")
答案 1 :(得分:0)
以下是一个示例实现:
require 'watir-webdriver'
browser = Watir::Browser.new :safari
browser.goto 'http://the-internet.herokuapp.com/javascript_alerts'
browser.execute_script("window.alert = function() {}")
browser.button(:text => 'Click for JS Alert').click
puts Watir::Wait.until { browser.text.include? "You successfuly clicked an alert" }
您无法在所有
中看到警报显示