14

I have a page with something like this:

<input type="button" onclick="confirm('confirm popup');" value="click" />

When I go to the page and click the link:

require "watir-webdriver"
browser = Watir::Browser.new
browser.goto "page.html"
browser.button.click

confirm popup appears. I can dismiss it with this:

require "watir-webdriver/extensions/alerts"
browser.confirm(true) {browser.button(:value => 'Confirm').click}

but the I get this error message:

[remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/command_processor.js:10302:in `unknown': Modal dialog present (Selenium::WebDriver::Error::UnhandledAlertError)
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/driver-component.js -> file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/dispatcher.js:85:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/driver-component.js -> file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/dispatcher.js:516:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/driver-component.js -> file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/dispatcher.js:351:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/driver-component.js -> file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/webdriverserver.js:47:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/httpd.js:1935:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/httpd.js:2261:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/httpd.js:1168:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/httpd.js:1616:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/httpd.js:1464:in `unknown'
    from [remote server] file:///var/folders/yx/bz_81fc143b4r9pyhks300m00000gn/T/webdriver-profile20120218-1562-7b7q8d/extensions/fxdriver@googlecode.com/components/httpd.js:1333:in `unknown'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/default.rb:64:in `request'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:590:in `raw_execute'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:568:in `execute'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:314:in `executeScript'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/common/driver.rb:209:in `execute_script'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/watir-webdriver-0.5.3/lib/watir-webdriver/browser.rb:129:in `execute_script'
    from /Users/zeljko/.rvm/gems/ruby-1.9.3-p125/gems/watir-webdriver-0.5.3/lib/watir-webdriver/extensions/alerts.rb:39:in `confirm'
    from popup.rb:7:in `<main>'

What am I doing wrong?

Environment:

  • Mac OS X 10.7.3
  • Firefox 10.0.2
  • Ruby 1.9.3p125
  • selenium-webdriver 2.19.0
  • watir-webdriver 0.5.3
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125

1 Answers1

4

I'll refer you to this answer https://stackoverflow.com/a/8172888/409820 where I explained about using methods like browser.confirm. The examples of which are a bit confusing since they come straight from the tests, and if you don't understand the context that there were multiple buttons on the test page, and the one that causes a confirm dialog has a value of 'confirm' then the 'inner' command wrapped by the method doesn't make a lot of sense.

The commands in the loop (or block ) following the confirm method is the command that causes the popup to occur.

Hence a better example of how to use those methods would have been something like

alert_message_text = browser.confirm(true) do
  #watir code that causes confirm popup goes here
end 

or

alert_message_text = browser.confirm(true) {#code that causes confirm popup}

in other words the way this works using .confirm(response) {block} is a lot like telling watir "ok the command I am giving you in the block will cause a confirm dialog to appear, and when it does I want you to take the action specified by response"

So where you are going wrong is the command inside the block, you need to move the 'browser.button.click' or something similar inside the block. Your code would then end up looking something like this

require "watir-webdriver"
require "watir-webdriver/extensions/alerts"

browser = Watir::Browser.new
browser.goto "page.html"
browser.confirm(true) {browser.button(:value => 'click').click}

I changed your click action to be a bit more specific, identifying the button based on the value attribute in the HTML you provided. If you were sure it was always going to be the first button on the page you could change the last line to

browser.confirm(true) {browser.button.click}
Community
  • 1
  • 1
Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
  • 1
    Thanks Chuck. There is a beer waiting for you in Austin. :) The funny thing is that I do not see the popup appearing when I use `browser.confirm(true) {browser.button.click}`. I guess it overrides `window.confirm` JavaScript function, similar to `browser.execute_script("window.confirm = function() {return true}")` – Željko Filipin Feb 19 '12 at 04:40
  • Might be, however your code was executing the click that should have made it appear, so not sure what is up there. I'll take you up on the beer. Will buy you one in return if you find the time to get the info above and in the other linked answer into the watir wiki or someplace that is easier for our users to find. – Chuck van der Linden Feb 19 '12 at 05:16
  • The plan is to put it all in the book, but I just did not have the time so far. Should be in the book before Bazaar. – Željko Filipin Feb 19 '12 at 21:21