Flags don't work when running headless?

866 views
Skip to first unread message

Jason Barnabe

unread,
Aug 27, 2020, 4:30:55 PM8/27/20
to ChromeDriver Users
(Originally posted on SeleniumHQ/selenium.)

I'm testing a Rails application using Capybara and Selenium.

I'm trying to set the flags same-site-by-default-cookies@2 and cookies-without-same-site-must-be-secure@2 as the defaults recently changed, causing some of my tests to fail.

If I do:

Capybara.register_driver :headless_chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    'goog:chromeOptions' => {
        args: ['verbose', 'disable-gpu', 'no-sandbox', 'disable-dev-shm-usage', 'window-size=1400,1400'],
        # Turn off SameSite cookies requirement as that requires HTTPS.
        localState: { "browser.enabled_labs_experiments" => ['same-site-by-default-cookies@2','cookies-without-same-site-must-be-secure@2'] },
    },
    'goog:loggingPrefs' => { browser: 'ALL' },
  )

  Capybara::Selenium::Driver.new app,
                                 browser: :chrome,
                                 desired_capabilities: capabilities
end

Then it works. If I go to chrome:flags, I can see they have been changed. However, if if I use headless mode:

Capybara.register_driver :headless_chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    'goog:chromeOptions' => {
        args: ['verbose', 'disable-gpu', 'no-sandbox', 'disable-dev-shm-usage', 'window-size=1400,1400', 'headless'],
        # Turn off SameSite cookies requirement as that requires HTTPS.
        localState: { "browser.enabled_labs_experiments" => ['same-site-by-default-cookies@2','cookies-without-same-site-must-be-secure@2'] },
    },
    'goog:loggingPrefs' => { browser: 'ALL' },
  )

  Capybara::Selenium::Driver.new app,
                                 browser: :chrome,
                                 desired_capabilities: capabilities
end

Then changing the flags doesn't seem to take effect; I get the same result in my test as before I added the flags. (Not sure how to demonstrate the flags aren't applied in headless mode.)

Am I doing something wrong, or is this a bug?

Environment

OS: Ubuntu 20.04
Browser: Chrome
Browser version: 85.0.4183.83
Browser Driver version: ChromeDriver 85.0.4183.83
Language Bindings version: Ruby 2.6.6

Stuart Ferguson

unread,
Sep 8, 2020, 4:00:19 PM9/8/20
to ChromeDriver Users

Hi Jason,

Did you manage to make any progress on this issue?

Regards,

Stuart 

Jason Barnabe

unread,
Sep 8, 2020, 4:02:39 PM9/8/20
to ChromeDriver Users
No, never figured it out. Just switched to running headed.

Shengfa Lin

unread,
Sep 16, 2020, 6:25:14 PM9/16/20
to ChromeDriver Users
It sounds like it might be an issue with Chrome not reading prefs from user data dir in headless mode.
If you enable ChromeDriver verbose log,
Capybara.register_driver :selenium_chrome_verbose do |driver_app|
service = ::Selenium::WebDriver::Service.chrome(args: { verbose: true, log_path: '/tmp/chromedriver.log' })

::Capybara::Selenium::Driver.new(driver_app, browser: :chrome, service: service)
end


To specify the user-data-dir for ChromeDriver, you can try https://gist.github.com/bibendi/047e27a9fb7ed5d720e61f8c1e2b2714
Then after you run the test for the first time, you can go to the user data dir and check if the pref file exist. 
To check if the issue is caused by Chrome, you can manually start Chrome and specify the same --user-data-dir option and test both headed and headless for your test manually and see if you can repro.

If you think it's ChromeDriver issue, you can file an issue at https://bugs.chromium.org/p/chromedriver/issues/list.
If you think it's Chrome issue, you can file an issue at https://bugs.chromium.org/p/chromium/issues/list

Thanks,
Shengfa

John Chen

unread,
Sep 16, 2020, 6:50:09 PM9/16/20
to Shengfa Lin, ChromeDriver Users
Most prefs flags are not implemented in headless Chrome. If your test needs prefs flags, most likely you'll need to run it with headful Chrome.

Thanks,
John

--
You received this message because you are subscribed to the Google Groups "ChromeDriver Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromedriver-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chromedriver-users/4370f8ee-156f-4b8f-bca6-bce62beee200n%40googlegroups.com.

OopsAiee

unread,
Mar 26, 2021, 5:29:45 AM3/26/21
to ChromeDriver Users
Another workaround is to downgrade chrome version on test hosts to 76something.
We having same issue, worked before chrome upgrade. If you in an enterprise project you most probably safe with out of date version for testing since none of gypsy bells and whistles is leaked in.

Reply all
Reply to author
Forward
0 new messages