简体   繁体   English

使用带有Headless Chrome和Selenium Webdriver,macOS,Rails,Capybara的acceptInsecureCerts

[英]Using acceptInsecureCerts with Headless Chrome and Selenium Webdriver, macOS, Rails, Capybara

Question

Does headless Chrome work with self-signed certificates via the Selenium Webdriver on macOS? 无头Chrome是否可以通过macOS上的Selenium Webdriver使用自签名证书?

Info 信息

I am attempting to get Rails system tests driven by headless Chrome over SSL. 我试图让无头Chrome通过SSL驱动Rails系统测试。

I have a locally self-signed certificate that I pass the the ruby Puma application server to terminate SSL requests. 我有一个本地自签名证书,我通过ruby Puma应用程序服务器来终止SSL请求。 In order to allow drivers to ignore SSL warnings on the locally signed certificate, I am using the acceptInsecureCerts flag to configure the driver capabilities. 为了允许驱动程序忽略本地签名证书上的SSL警告,我使用acceptInsecureCerts标志来配置驱动程序功能。 I'm led to believe by this ticket in Chromium that this flag should be recognized as of Chrome 64+. 我被Chromium这张票证相信,这个标志应该被识别为Chrome 64+。

I can get tests to succeed with Chrome, Firefox, and headless Firefox. 我可以通过Chrome,Firefox和无头Firefox获得成功的测试。 Tests do not pass under headless Chrome. 测试不会通过无头Chrome。 I am using (at the time of this writing) what I believe to be the latest versions of Chrome and its variants. 我正在使用(在撰写本文时)我认为最新版本的Chrome及其变体。

Though folks in the Chromium ticket appear to be successfully running headless Chrome over locally-signed SSL with the Selenium webdriver, I have not found this to work with the setup described here. 虽然Chromium票证中的人员似乎是通过Selenium webdriver在本地签名的SSL上成功运行无头Chrome,但我还没有发现这可以使用此处描述的设置。 If my configuration is correct, then I am unsure if there is a limitation in headless Chrome on macOS, in Selenium webdriver ruby gem, or something else I haven't considered. 如果我的配置是正确的,那么我不确定macOS上的无头Chrome,Selenium webdriver ruby​​ gem或其他我没有考虑的其他内容是否有限制。 If anyone has something similar working with Rails on macOS, I'd be interested to learn about your setup. 如果有人在macOS上使用类似Rails的东西,我有兴趣了解你的设置。

Source 资源

Here is some code to show how I am configuring and running my RSpec/Capybara tests. 下面是一些代码,用于说明我如何配置和运行RSpec / Capybara测试。

Test setup 测试设置

# rails_helper.rb
# ... standard rspec rails helper setup omitted ...

Capybara.register_driver(:headless_chrome) do |app|
  options = Selenium::WebDriver::Chrome::Options.new(
    args: %w[--headless --disable-gpu --no-sandbox --disable-web-security]
  )
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    acceptInsecureCerts: true,
  )
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    options: options,
    desired_capabilities: capabilities
  )
end

RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :headless_firefox
  end
end

module SystemTestHelpers
  def key_file_path
    Rails.root.join("config", "ssl", "ssl-lvh.me.key")
  end

  def cert_file_path
    Rails.root.join("config", "ssl", "ssl-lvh.me.crt")
  end

  def using_app_host(host)
    original_host = Capybara.app_host
    Capybara.app_host = host

    Capybara.server = :puma, {
      Host: "ssl://#{Capybara.server_host}?key=#{key_file_path}&cert=#{cert_file_path}"
    }
    yield
  ensure
    Capybara.app_host = original_host
  end
end

RSpec.configure do |config|
  config.include SystemTestHelpers, type: :system
end

Sample test 样品测试

# spec/system/welcome_spec.rb

require 'rails_helper'

RSpec.feature "Welcome", :js, type: :system do
  scenario "Visit homepage" do
    using_app_host('https://subdomain.lvh.me') do
      visit "/"

      expect(page).to have_content('Welcome')

      expect(page).to have_content('Your domain: subdomain.lvh.me')
      expect(page).to have_content('Your protocol: https://')
    end
  end
end

Page content: 页面内容:

<div>
  <h2>Welcome!</h2>

  <p>Your protocol: <%= request.protocol %></p>
  <p>Your domain: <%= request.host %></p>
</div>

If I swap out the driver for headless Firefox, configured as below, the tests will pass. 如果我换掉无头Firefox的驱动程序,配置如下,测试将通过。

Capybara.register_driver(:headless_firefox) do |app|
  options = Selenium::WebDriver::Firefox::Options.new(args: %w[--headless])

  capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(
    acceptInsecureCerts: true,
  )
  Capybara::Selenium::Driver.new(
    app,
    browser: :firefox,
    options: options,
    desired_capabilities: capabilities
  )
end

The complete source code for an app that reproduces the issue, and includes the code above is located here: https://bitbucket.org/rossta/system-test-demo . 重现问题的应用程序的完整源代码,包括上面的代码,请访问: https//bitbucket.org/rossta/system-test-demo

Debug output 调试输出

Here's a link to some debug output from running the test in either headless Chrome or headless Firefox: https://gist.github.com/rossta/b160204baa87a520e7888c19c8b1ed98 . 以下是在无头Chrome或无头Firefox中运行测试的一些调试输出的链接: https//gist.github.com/rossta/b160204baa87a520e7888c19c8b1ed98

Note in the output that the session response does not include the 'acceptInsecureCerts' capability for Chrome (test-headless-chrome.log, line 15) while in Firefox we do see the session include the flag (test-headless-firefox.log, line 22). 请注意输出中的会话响应不包含Chrome的“acceptInsecureCerts”功能(test-headless-chrome.log,第15行),而在Firefox中我们确实看到会话包含标志(test-headless-firefox.log,第22行)。

System 系统

  • MacOS 10.13.6 MacOS 10.13.6
  • Rails 5.2.1 Rails 5.2.1
  • Ruby 2.4.1 Ruby 2.4.1
  • capybara (gem) 3.5.1 capybara (宝石)3.5.1
  • selenium-webdriver (gem) 3.14.0 selenium-webdriver (gem)3.14.0
  • chromdriver-helper (gem) 2.34 chromdriver-helper (gem)2.34
  • Chrome 68.0.3440.106. Chrome 68.0.3440.106。 Have also tried 也试过了
    • Google Chrome 70.0.3524.0 canary 谷歌Chrome 70.0.3524.0金丝雀
    • Chromium 70.0.3525.0 铬70.0.3525.0

From your log it shows that it's starting chromedriver v2.34. 从您的日志中可以看出它正在启动chromedriver v2.34。 acceptInsecureCerts support wasn't added until 2.35 and you should really be running the latest (2.41 currently). 在2.35之前没有添加acceptInsecureCerts支持,你应该真正运行最新的(当前为2.41)。 Update your version of chromedriver and things should work. 更新你的chromedriver版本,事情应该有效。

caps = Selenium::WebDriver::Remote::Capabilities.firefox caps = Selenium :: WebDriver :: Remote :: Capabilities.firefox

caps['acceptInsecureCerts'] = true caps ['acceptInsecureCerts'] = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Headless Chrome Webdriver 运行 Selenium - Running Selenium with Headless Chrome Webdriver 水豚,硒,无头Chrome,Ubuntu Net :: ReadTimeout - Capybara, Selenium, headless Chrome, Ubuntu Net::ReadTimeout 如何使用硒在无头铬中取消设置navigator.webdriver? - How to unset navigator.webdriver in headless chrome using selenium? --headless 不是 selenium python chrome webdriver 中的一个选项 - --headless is not an option in chrome webdriver for selenium python 无法使用 Selenium Webdriver java 在 Linux 机器上运行 Headless Chrome 浏览器 - Unable to run Headless Chrome Browser on Linux Machine using Selenium Webdriver java 使用带有 Python 的 Selenium Webdriver 在 Headless chrome 浏览器上运行脚本时发生超时错误 - Timeout Error occurred When run a script on Headless chrome browser by using Selenium Webdriver with Python 使用无头 chrome webdriver 时出现超时异常错误 - timeout exception error on using headless chrome webdriver 将铬用于水豚,护栏4.2 - Using chrome for capybara, rails 4.2 无法通过 Heroku 中的 Selenium webdriver(Java) 调用无头 chrome 驱动程序 - Unable to invoke headless chrome driver through Selenium webdriver(Java) in Heroku 在Selenium WebDriver中启动Chrome Headless仍会导致浏览器窗口空白 - Launching Chrome Headless in Selenium WebDriver still results in a blank browser window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM