[英]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. 我在我的Ruby on Rails(5.2.1)项目中使用了带有Selenium(3.14.0)和Capybara(3.8.0)的chrome headless,我有一个测试工作在Non-headless chrome但不在无头chrome中。 I'm using the '--headless' flag on google chrome stable version 69. 我在谷歌Chrome稳定版69上使用'--headless'标志。
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. 我已经读过我应该向selenium chrome驱动程序发送命令以允许下载,但我无法弄清楚如何使用我的设置。 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? 如何在我的设置中访问selenium网桥,以便我可以发送此http呼叫?
You don't need to send that manually anymore it was added to selenium as Selenium::WebDriver::Chrome::Server#download_path=
. 您不需要再手动发送它作为Selenium::WebDriver::Chrome::Server#download_path=
添加到selenium。 You can set it in your driver registration via the Capybara::Selenium::Driver
instance 您可以通过Capybara::Selenium::Driver
实例在驱动程序注册中进行设置
...
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
).tap { |d| d.browser.download_path = <your download path> }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.