Dakadaka
Dakadaka

Reputation: 1513

remove alert confirms javascript with watir webdriver

watir webdriver and javascript dialogs I have example and cant get rid of that alert in test. this is javascript

<script>
alert("hello.\n\nPress OK to go.");
</script>

then I need to click on it and page will load

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"

error is

/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>'

with

b.execute_script "window.onbeforeunload = null"

with this works fine if javascript is different like window.onbeforeunload

but this alert gives me error and everything that comes after don't work anymore, because I cant click on that alert, so that page loads.

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

but it doesn't work because page doesnt load and alert is before and I cant find the way to click on it.

Upvotes: 1

Views: 1693

Answers (1)

Justin Ko
Justin Ko

Reputation: 46846

It seems a bit hacky, but the following seems to work.

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

Basically, you:

  1. Go to the page
  2. Rescue the exception
  3. Use watir's alert API to click okay in the alert
  4. Continue on your way

Upvotes: 4

Related Questions