google-chrome - 将 acceptInsecureCerts 与 Headless Chrome 和 Selenium Webdriver、macOS、Rails、Capybara 结合使用

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

问题

headless Chrome 能否通过 macOS 上的 Selenium Webdriver 使用自签名证书?

信息

我正在尝试通过 SSL 获取由 headless Chrome 驱动的 Rails 系统测试。

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

我可以通过 Chrome、Firefox 和 headless Firefox 获得成功的测试。测试不会在 headless Chrome 下通过。我正在使用(在撰写本文时)我认为是最新版本的 Chrome 及其变体。

尽管人们在 Chromium ticket似乎使用 Selenium webdriver 通过本地签名的 SSL 成功运行无外设 Chrome,我没有发现这适用于此处描述的设置。如果我的配置正确,那么我不确定 macOS 上的 headless 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>

如果我换掉 headless 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 .

调试输出

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

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

系统

  • 苹果操作系统 10.13.6
  • 轨道 5.2.1
  • ruby 2.4.1
  • capybara (gem) 3.5.1
  • selenium-webdriver (gem) 3.14.0
  • chromdriver-helper (gem) 2.34
  • Chrome 68.0.3440.106。也试过
    • 谷歌浏览器 70.0.3524.0 金丝雀版
    • Chrome 70.0.3525.0

最佳答案

从您的日志中可以看出它正在启动 chromedriver v2.34。 acceptInsecureCerts 支持直到 2.35 才添加,您应该真正运行最新版本(当前为 2.41)。更新您的 chromedriver 版本,应该可以正常工作。

关于google-chrome - 将 acceptInsecureCerts 与 Headless Chrome 和 Selenium Webdriver、macOS、Rails、Capybara 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51881206/

相关文章:

python - 如何使用 selenium python 打开新窗口

java - TestNG 类并行运行,但方法则不然。如何配置 testng.xml 文件和测试类,以便方法也可以并行运行?

selenium - 允许通过 chromedriver 运行 Chrome 69 中的 Flash 内容

java - selenium webdriver 中的 Keys.UP 和 Keys.ARROW_UP 有什么区别?

javascript - 负下边距在 IE 中不起作用

android - 如何禁用 GoogleWebLight?

javascript - Selenium:输入样式为 ="display: none;"的文本字段

java - Webdriver - 无法找到 Xpath/CSS 并出现 NoSuchElementException

google-chrome - Chrome 不显示完整网址

google-chrome - 在开发者工具中复制/粘贴会导致 Chrome 崩溃