capybara chrome无头确认对话框

时间:2017-07-08 22:10:07

标签: ruby-on-rails google-chrome selenium-chromedriver headless

我正在尝试在rspec Ruby on Rails中使用无头模式的chrome和capybara / selenium,并在尝试单击确认对话框时出现错误

Capybara.register_driver(:headless_chrome) do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { args: %w[headless disable-gpu test-type window-size=1920x1080] }
  )

  driver = Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: capabilities
  )

  driver
end

Capybara.server = :puma
Capybara.javascript_driver = :headless_chrome



scenario 'delete movie' do
  login_as user
  visit edit_public_movie_path(movie)

  expect(page).to have_selector('span[ng-click="ctrl.deleteMovie()"]')
  find('span[ng-click="ctrl.deleteMovie()"]').click

  page.driver.browser.switch_to.alert.accept

  wait_for_ajax(wait_after: 1)
  expect(page).to have_content('Click here to upload movie (Max: 500 MB)')
end

得到了这个错误

 Event movie success delete movie
 Failure/Error: page.driver.browser.switch_to.alert.accept

 Selenium::WebDriver::Error::NoSuchAlertError:
   no alert open
     (Session info: headless chrome=59.0.3071.115)
     (Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.5 x86_64)

使用普通模式时,它可以正常工作。似乎硒不支持无头模式或需要其他方法来做到这一点。有任何想法吗?

更新(解析)

所以,我花了很多时间找工作环境。

您需要持续构建chrome,因为预览版本具有bug。您可以通过这些脚本linuxMac

来获取它

这是我的capybara配置

 Capybara.register_driver(:headless_chrome) do |app|
   capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
                                        chromeOptions: {
                                          args: %w[headless disable-gpu disable-popup-blocking no-sandbox window-size=1920x1920],
                                          binary: "#{Dir.home}/chromium-latest-#{platrofm}/latest/#{chrome_file}"
                                        })
  driver = Capybara::Selenium::Driver.new(app,
                                        browser: :chrome,
                                        desired_capabilities: capabilities)

  driver
end

Capybara.server = :puma
Capybara.javascript_driver = :headless_chrome
Capybara.default_max_wait_time = 20
Capybara.server_port = 55305

def platform
  if /linux/ =~ RUBY_PLATFORM
    "linux"
  else
    "macosx"
  end
end

def platform_linux?
  platform == "linux"
end

def chrome_file
  if platform_linux?
    "chrome"
  else
    "Chromium.app/Contents/MacOS/Chromium"
  end
end

你应该使用 page.driver.browser.switch_to.alert.accept 而不是page.accept_alert

1 个答案:

答案 0 :(得分:0)

有同样的问题。请使用chromeOptions中的disable-popup-blocking。