I have a rails app, and am running capybara & selenium, and using chrome for end-to-end testing. I want to set the default download directory while running chrome headless.
Here's how I have it setup in my support/env.rb:
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {
'args' => %w(headless disable-gpu window-size=1920,1080),
'prefs' => {
'download.default_directory' => DownloadHelpers::PATH.to_s
}
}
)
Capybara::Selenium::Driver.new app,
browser: :chrome,
#args: ["--window-size=1024,768"], #UNCOMMENT TO HAVE BROWSER WINDOW POP UP
desired_capabilities: capabilities
end
I have noticed that if I leave off
'args' => %w(headless disable-gpu window-size=1920,1080)
Then the default download directory is set, however, the browser automatically pops up, which I want to disable.
When I have both:
'args' => %w(headless disable-gpu window-size=1920,1080),
'prefs' => {
'download.default_directory' => DownloadHelpers::PATH.to_s
}
It runs headless, but the default download path is not set.
I have the following versions:
capybara (2.14.2)
selenium-webdriver (3.6.0)
chromedriver-helper (1.1.0)
I used https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c78 and
Changed my setup to:
Capybara.register_driver :selenium do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-popup-blocking')
options.add_argument('--window-size=1366,768')
options.add_preference(:download, directory_upgrade: true,
prompt_for_download: false,
default_directory:
'/User/paulo/projects/app/tmp')
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
bridge = driver.browser.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: '/User/paulo/projects/app/tmp'
})
driver
end
Note you will have to put in your own downloadPath.
In case anyone comes across this, I tried using user43395's solution, and could not get it to work. However, I finally discovered the issue - in my particular case, the download directory needed to have BACKSLASHES instead of forward slashes. So for example:
downloadPath: '/User/paulo/projects/app/tmp'
needed to be
downloadPath: '\\User\\paulo\\projects\\app\\tmp'
Otherwise, everything else in their answer worked like a charm! I wanted to put this here in case anyone else has been struggling with not getting it to work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.