Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome headless download pdf using capybara and selenium

I'm using chrome headless with Selenium (3.14.0) and Capybara (3.8.0) in my Ruby on Rails (5.2.1) project and I have a test which works in Non-headless chrome but not in headless chrome. I'm using the '--headless' flag on google chrome stable version 69.

I've setup my headless chrome with the following and this works for all tests which don't download files.

download_path="#{Rails.root}/tmp/downloads"

Capybara.register_driver(:headless_chrome) do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: {
      prefs: {
        'download.default_directory' => download_path,
        "download.extensions_to_open" => "applications/pdf",
        'download.directory_upgrade' => true,
        'download.prompt_for_download' => false,
        'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
      },
      binary: "/opt/google/chrome/google-chrome",
      args: %w[headless disable-gpu window-size=1920,1080]
    }
  )
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: caps
  )
end

I've read that I should be sending a command to selenium chrome driver to allow downloads but I cannot figure out how to do that with my setup. Here is what I'm trying to get working, but with my setup; (not from my code base);

@driver = Selenium::WebDriver.for :chrome, options: options

bridge = @driver.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
                 params: {
                   behavior: 'allow',
                   downloadPath: download_path
                 })

How do I access the selenium bridge in my setup so that I can send this http call?

like image 502
map7 Avatar asked Oct 04 '18 06:10

map7


People also ask

Can selenium download Chrome in headless mode?

Downloading with chrome headless and selenium. We can download Chrome in headless mode in Selenium. The headless execution is one of the ways saving resources by not utilizing the complete graphical interface. After the version 59, Chrome can be used in headless mode.

Why are my files not downloading in chrome headless mode?

Files aren't downloaded. Launch chrome in headless mode and try to download any file. Looks like it is a feature to prevent sites from downloading files when running chrome in headless mode. To permit downloads it's necessary to send a command to chrome.

What is headless execution in chrome?

The headless execution is one of the ways saving resources by not utilizing the complete graphical interface. After the version 59, Chrome can be used in headless mode. The ChromeOptions class is used to modify the default character of the browser. The parameter headless is passed as a parameter to the addArgument method for headless execution.

What is permit files to be downloaded in headless mode?

Permit files to be downloaded in headless mode. Files aren't downloaded. Launch chrome in headless mode and try to download any file. Looks like it is a feature to prevent sites from downloading files when running chrome in headless mode.


1 Answers

You don't need to send that manually anymore it was added to selenium as Selenium::WebDriver::Chrome::Server#download_path=. You can set it in your driver registration via the Capybara::Selenium::Driver instance

...
Capybara::Selenium::Driver.new(
  app,
  browser: :chrome,
  desired_capabilities: caps
).tap { |d| d.browser.download_path = <your download path> }
like image 179
Thomas Walpole Avatar answered Sep 30 '22 08:09

Thomas Walpole