Tyler Rick
Tyler Rick

Reputation: 9491

"Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open" despite using dismiss_confirm

This test fails:

describe 'a confirm modal', :js, driver: :chrome do
  it do
    visit '/test/confirm'
    dismiss_confirm do
      click_link 'Cancel'
    end
  end
end

with a Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open error.

It fails immediately with this error as soon as it gets to the click_link 'Cancel' line:

An error occurred in an after hook
  Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open: {Alert text : Are you sure?}
  (Session info: chrome=53.0.2785.101)
  (Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 4.4.0-42-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1 milliseconds: null
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: '64c23d07d03c', ip: '172.18.0.6', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-42-generic', java.version: '1.8.0_03-Ubuntu'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a), userDataDir=/tmp/.com.google.Chrome.nJ0sj0}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=53.0.2785.101, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 12b4869246e45555e3456370e74ad536 (org.openqa.selenium.UnhandledAlertException)
  occurred at [remote server] sun.reflect.GeneratedConstructorAccessor31():-1:in `newInstance'

I can see in the Chrome window that it is still waiting for me to press OK or Cancel.

Why is this failing? How do I get it to dismiss the confirm instead of erroring out?

When I step through the code with byebug, I can see that dismiss_modal doesn't even get to the part where it tries to find a modal and dismiss. It errors out immediately when it hits the click_link, which I take it is not supposed to happen (otherwise dismiss_modal would have a rescue block for that):

[229, 238] in /usr/local/bundle/gems/capybara-2.10.1/lib/capybara/selenium/driver.rb
   229:     modal.accept
   230:     message
   231:   end
   232: 
   233:   def dismiss_modal(type, options={}, &blk)
=> 234:     yield if block_given?
   235:     modal = find_modal(options)
   236:     message = modal.text
   237:     modal.dismiss
   238:     message
(byebug) s

[3, 12] in /app/spec/features/testing_basics_spec.rb
    3: describe 'a confirm modal', :js, driver: :chrome do
    4:   it do
    5:     visit '/test/confirm'
    6:     byebug
    7:     dismiss_confirm do
=>  8:       click_link 'Cancel'
    9:     end

The selenium server is running in a docker container from the selenium/standalone-chrome-debug image.

The driver is configured with:

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser:                :remote,
    url:                    "http://#{ENV['CHROME_WEBDRIVER_HOST']}:4444/wd/hub",
    desired_capabilities:   :chrome
  )
end

It's using current versions of the gems (as far as I know):

  * capybara (2.10.1)
  * selenium-webdriver (2.53.4)

This is the Rails template it is interacting with (app/views/confirm.html.haml):

= link_to 'Cancel', '#', :confirm => 'Are you sure?'

Update: Thanks to @TomWalpole and @SaurabhGaur for suggesting this could be due using an older version of chromedriver.

It looks like I hadn't updated my selenium/standalone-chrome-debug image in a few weeks (though I'm surprised that 5 weeks is so old that it would cause a problem).

Before updating...

selenium/standalone-chrome-debug latest 84d9c94a042a 5 weeks ago         844.8 MB

root@65a9cd48f684:/# chromedriver --version                                                                                                                                                                
ChromeDriver 2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a)

After updating...

selenium/standalone-chrome-debug latest df941c594b0a 2 days ago          865.3 MB

root@6800a351c182:/# chromedriver --version                  
ChromeDriver 2.24.417424 (c5c5ea873213ee72e3d0929b47482681555340c3)

And it works!

Upvotes: 1

Views: 3330

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

When using selenium-webdriver with Chrome it's important to keep chromedriver updated when Chrome updates itself. In your log it shows chromedriver version of 2.21 but the latest is 2.24 - update that and your problems should go away.

Upvotes: 1

Related Questions