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

Ruby 3.0升级后运行Capybara测试出现ArgumentError问题求助

解决方案

1. 修正代码中的中文引号

先排查测试代码的语法错误:

state = "new"
visit status_path(state: “state") # 此处为中文引号,会导致参数解析异常

修正为英文双引号:

state = "new"
visit status_path(state: "state")

2. 适配Ruby 3.0关键字参数规则调整驱动初始化

Ruby 3.0对关键字参数与普通哈希的区分更严格,旧写法可能触发参数传递错误,修改Capybara驱动注册代码:

Capybara.register_driver :headless_chrome do |app|
  Capybara::Selenium::Driver.load_selenium

  browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |options|
    options.add_argument("--headless=new") # Chrome 112+ 推荐使用新版无头模式参数
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-gpu")
  end

  # 明确拆分位置参数与关键字参数,适配Ruby 3.0规则
  Capybara::Selenium::Driver.new(
    app,
    **{ browser: :chrome, options: browser_options }
  )
end

3. 升级兼容Ruby 3.0的gem版本

更新相关gem到适配Ruby 3.0的稳定版:

# 修改Gemfile
gem "capybara", ">= 3.39.0"
gem "selenium-webdriver", ">= 4.18.1"
gem "webdrivers", ">= 5.3.0"

执行更新命令:

bundle update capybara selenium-webdriver webdrivers

4. 补充调用栈排查定位

如果以上步骤无效,添加调用栈打印定位错误根源:
在测试代码中加入异常捕获:

begin
  state = "new"
  visit status_path(state: "state")
rescue Exception => e
  puts e.backtrace.join("\n")
  raise
end

通过完整调用链可以找到具体触发net/protocol参数错误的代码位置。


内容的提问来源于stack exchange,提问作者I. Khan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 11:35:20