Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capybara chrome headless confirm dialog

I'm trying to use headless mode of chrome with capybara/selenium in rspec Ruby on Rails and getting a error when try to click on confirm dialog

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

And got this error

 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)

With common mode it works without errors. Seems the selenium don't support headless mode or need to other approach to do it. Any ideas?

Update(resolved)

So, I spent a lot of time and find working environments.

You need to last build of chromium because preview versions have a bug. You can get it by these scripts linux or Mac

Here is my capybara config

 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

And you should use page.driver.browser.switch_to.alert.accept instead of page.accept_alert

like image 547
Alex Dmitriev Avatar asked Nov 19 '22 16:11

Alex Dmitriev


1 Answers

Had the same problem. Please use the disable-popup-blocking in chromeOptions.

like image 67
Zeev Schneider Avatar answered Dec 09 '22 19:12

Zeev Schneider