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

如何在Capybara驱动的RSpec测试中将Chrome通知权限设为默认值

配置Chrome通知权限的方案

有对应的配置方式,你可以通过给Selenium驱动的Chrome浏览器添加自定义偏好配置,调整通知权限的默认值,修改后的完整配置如下:

Capybara.configure do |config|
  config.server = :puma, {Silent: true}
  config.enable_aria_role = true
  config.enable_aria_label = true
  config.default_max_wait_time = 10
end

RSpec.configure do |config|
  config.include ActionView::RecordIdentifier, type: :system
  config.before(type: :system) do
    chrome_options = Selenium::WebDriver::Chrome::Options.new
    # 设置通知权限默认行为
    chrome_options.add_preference(:prefs, {
      "profile.default_content_setting_values.notifications" => 0
    })
    driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400], options: chrome_options
  end  
end

参数说明

profile.default_content_setting_values.notifications是Chrome内核专属的配置项,三个可选值对应不同的权限默认行为:

  • 0:使用浏览器默认的询问逻辑,访问需要通知权限的站点时会弹出权限申请提示
  • 1:全局默认允许所有站点的通知权限
  • 2:全局默认阻止所有站点的通知权限

该配置仅适用于Chrome/Chromium内核浏览器,其他浏览器的权限配置项需参考对应驱动的官方说明。


内容的提问来源于stack exchange,提问作者aldrien.h

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 21:39:04