JavaScript dialogs are fairly common in web applications.
Watir-WebDriver has an inbuilt library for handling the dialogs, and capturing values from the dialogs. First, require the extension:
require "watir-webdriver/extensions/alerts"
JavaScript Alerts
browser.alert do browser.button(:value => 'Alert').click end #=> 'the alert message'
JavaScript Confirms
browser.confirm(true) do browser.button(:value => 'Confirm').click end #=> 'the confirm message'
JavaScript Prompt
browser.prompt('hello') do
browser.button(:value => 'Prompt').click
end #=> { :message => 'foo', :default_value => 'bar' }
Alternative Method
If you’re having trouble using the above method, you can override the JavaScript functions to return the value you want, so when they’re meant to show, they don’t!
# 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}")