2

Background:

I'm currently writing tests for a Rails project using RSpec + Capybara. We're using Selenium + Headless Chrome to simulate browser interactions.

Problem:

I want to disable browser cookies for a test. We're currently instantiating the web driver this way in our spec/spec_helper.rb


chrome_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
  chromeOptions: {
    w3c: false,
    args: %w[headless no-sandbox window-size=1280,2000 no-proxy-server disable-gpu disable-dev-shm-usage],
  },
  loggingPrefs:{browser: 'ALL'},
)

Capybara.register_driver :headless_chrome do |app|
  Capybara::Selenium::Driver.new(app, 
    browser: :chrome, 
    desired_capabilities: chrome_capabilities
  )
end

I've seen many Java examples that disable cookies by passing this setting when instantiating Headless Chrome: "profile.default_content_setting_values.cookies" => 2

However, I haven't been able to get to this to work.

Does anyone know the correct way to instantiate the Headless Chrome instance so that cookies are disabled?

Nate Rubin
  • 21
  • 4

1 Answers1

0

It depends on your chromedriver version. In release 2.31 developers changed:

Feature request: change chromeOptions to goog:chromeOptions

So for now it would look like:

  caps = Selenium::WebDriver::Remote::Capabilities.chrome(
    'goog:chromeOptions' => {
      'args' => [
        '--lang=en',
        '--no-default-browser-check',
        '--start-maximized',
        '--no-sandbox'
      ],
      'prefs' => { 
          "profile.default_content_settings.cookies" => 2 } 
        }
      }
    }
  )

Give it a try!

Roman Alekseiev
  • 1,854
  • 16
  • 24