0

在使用 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:in 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 click' 来自 /Users/usr/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.6.10/lib/watir-webdriver/元素 click' from /Users/usr/Documents/workspace/TTQA Safari/tttestlibrary.rb:47:in/element.rb:132:in create_org' 来自 /Users/usr/Documents/workspace/TTQA Safari/ttorgusercompare.rb:23:in org_user_compare' from /Users/usr/Documents/workspace/TTQA Safari/ttorgusercompare.rb:34:in'

谢谢!

4

2 回答 2

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")
于 2014-07-21T17:59:28.543 回答
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" }

您根本看不到警报显示

于 2014-07-23T03:13:49.760 回答