You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL环境下Rails项目配置Capybara时如何指定chrome.exe路径

解决方案

方案1:直接指定Windows Chrome二进制路径运行测试

该方案可以直接复用已安装的Windows Chrome,无需额外安装浏览器:

  1. 编辑项目下的test/application_system_test_case.rb文件,添加Chrome二进制路径配置,适配WSL环境:
require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  # 配置Chrome参数
  options = Selenium::WebDriver::Chrome::Options.new
  # 指定Windows Chrome的二进制路径
  options.binary = "/mnt/c/Program Files/Google/Chrome/Application/chrome.exe"
  # 添加WSL环境必要的启动参数
  options.add_argument "--no-sandbox"
  options.add_argument "--disable-dev-shm-usage"
  options.add_argument "--disable-gpu"
  # 如需后台运行可以添加无头参数
  # options.add_argument "--headless=new"

  driven_by :selenium, using: :chrome, options: {
    browser: :chrome,
    capabilities: options
  }

  # 配置测试服务监听所有网卡,允许Windows Chrome访问WSL内的测试服务
  Capybara.server_host = "0.0.0.0"
  Capybara.app_host = "http://#{`hostname`.strip&.downcase || "localhost"}"
end
  1. 配置webdrivers gem使用对应版本的Windows版ChromeDriver,在test_helper.rb中添加:
Webdrivers::Chromedriver.platform = "win"

调整后重新运行rake test:system即可正常调用Windows Chrome执行测试。

方案2(更推荐):WSL内安装无头Chrome

该方案兼容性和运行稳定性更高,避免跨系统调用的各类兼容问题:

  • 对于WSL内的Ubuntu/Debian系统,执行以下命令安装Chrome和对应驱动:
sudo apt update
sudo apt install -y chromium-browser chromium-chromedriver
  • 安装完成后无需额外修改配置,webdrivers会自动识别到WSL内的Chrome二进制文件,直接运行测试即可。
    该方案的优势为:无需处理跨系统路径、网络映射问题,运行速度更快,测试表现和Linux生产环境一致,不会出现Windows Chrome特有的兼容性异常。

内容的提问来源于stack exchange,提问作者Blaine Lafreniere

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 10:36:09