Watir Webdriver Safari : A modal dialog was opened. The SafariDriver does not support interacting with modal dialogs

858 views Asked by At

While using the watir safari webdriver, the webdriver terminates as soon as it encounters an alert pop-up. Are there any workarounds that will either allow Safari webdriver to function properly with alerts, or to prevent the alerts from appearing at all?

    $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")

error:

/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:in 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 execute' 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: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' from /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:in click' from /Users/usr/Documents/workspace/TTQA Safari/tttestlibrary.rb:47:increate_org' from /Users/usr/Documents/workspace/TTQA Safari/ttorgusercompare.rb:23:in org_user_compare' from /Users/usr/Documents/workspace/TTQA Safari/ttorgusercompare.rb:34:in'

Thanks!

2

There are 2 answers

0
Carldmitch On BEST ANSWER

Here's an example implementation:

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" }

You won't see the alert display at all

0
Justin Ko On

As described on watirwebdriver.com/javascript-dialogs, you could override the Javascript functions so that the alerts do not appear:

# 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")