将acceptInsecureCerts与无头Chrome和Selenium Webdriver,macOS,Rails和Capybara结合使用

时间:2018-08-16 16:03:57

标签: google-chrome selenium selenium-webdriver capybara google-chrome-headless

问题

无头Chrome可以通过macOS上的Selenium Webdriver使用自签名证书吗?

信息

我正在尝试通过无头Chrome over SSL驱动的Rails系统测试。

我有一个本地自签名证书,该证书通过了ruby Puma应用服务器以终止SSL请求。为了允许驱动程序忽略本地签名证书上的SSL警告,我使用acceptInsecureCerts标志来配置驱动程序功能。 this ticket in Chromium使我相信,该标志应从Chrome 64+开始被识别。

我可以通过Chrome,Firefox和无头Firefox获得成功的测试。在无头Chrome上无法通过测试。我正在使用(在撰写本文时)我认为是最新版本的Chrome及其变体。

尽管Chromium ticket中的人们似乎已经使用Selenium Webdriver成功地在本地签名的SSL上运行了无头Chrome,但我发现它无法与此处描述的设置一起使用。如果我的配置正确,那么我不确定macOS上的无头Chrome,Selenium webdriver ruby​​ gem或其他我没有考虑的限制。如果有人可以在macOS上使用Rails进行类似操作,那么我很想了解您的设置。

来源

下面是一些代码,以显示我如何配置和运行RSpec / Capybara测试。

测试设置

# 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

样本测试

# 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

页面内容:

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

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

如果我将驱动程序换成配置如下的无头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

可重现问题并包含上面代码的应用程序的完整源代码位于:https://bitbucket.org/rossta/system-test-demo

调试输出

以下是在无头Chrome或无头Firefox中运行测试的一些调试输出的链接:https://gist.github.com/rossta/b160204baa87a520e7888c19c8b1ed98

在输出中请注意,会话响应不包含Chrome的“ acceptInsecureCerts”功能(test-headless-chrome.log,第15行),而在Firefox中,我们确实看到会话包含了标记(test-headless-firefox) .log,第22行)。

系统

  • MacOS 10.13.6
  • 导轨5.2.1
  • Ruby 2.4.1
  • capybara(宝石)3.5.1
  • selenium-webdriver(宝石)3.14.0
  • chromdriver-helper(宝石)2.34
  • Chrome 68.0.3440.106。也尝试过
    • Google Chrome 70.0.3524.0金丝雀
    • 铬70.0.3525.0

2 个答案:

答案 0 :(得分:2)

从您的日志中显示它正在启动chromedriver v2.34。 acceptInsecureCerts支持直到2.35才被添加,并且您确实应该运行最新版本(当前为2.41)。更新您的chromedriver版本,一切正常。

答案 1 :(得分:0)

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

caps ['acceptInsecureCerts'] = true