尽管使用了dismiss_confirm,“Selenium :: WebDriver :: Error :: UnhandledAlertError:意外警报打开”

时间:2016-10-20 01:32:30

标签: ruby-on-rails selenium capybara selenium-chromedriver

此测试失败:

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

发生Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open错误。

一旦到达click_link 'Cancel'行,就会立即失败并显示此错误:

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'

我可以在Chrome窗口中看到它仍在等待我按OK或取消。

为什么会失败?如何让它解除确认而不是错误?

当我使用byebug单步执行代码时,我发现dismiss_modal甚至 都没有 到它试图找到模态和解散的部分。当它到达click_link时,它会立即错误地 ,我认为它不会发生(否则dismiss_modal会有rescue块):

[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

selenium服务器在selenium/standalone-chrome-debug映像的docker容器中运行。

驱动程序配置为:

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

它正在使用当前版本的宝石(据我所知):

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

这是与(app/views/confirm.html.haml)交互的Rails模板:

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

更新:感谢@TomWalpole和@SaurabhGaur建议使用较旧版本的chromedriver。

看起来我几周内没有更新我的selenium/standalone-chrome-debug图片(虽然我很惊讶5周已经很久了会导致问题)。

更新前......

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

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

更新后......

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

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

它有效!

1 个答案:

答案 0 :(得分:1)

在Chrome中使用selenium-webdriver时,重要的是在Chrome更新时自动更新chromedriver。在你的日志中,它显示了2.21的chromedriver版本,但最新的是2.24 - 更新了,你的问题应该消失。