Cookies using Rails Rspec and Headless Chrome Selenium::WebDriver::Error::NoSuchCollectionError

While attempting to set a required cookie manually using ChromeDriver to run an integration test, I came across this error:

Selenium::WebDriver::Error::NoSuchCollectionError:
     unable to set cookie

I was using the right approach to set the cookie, but chromedriver was not having it:

page.driver.browser.manage.add_cookie({
  name: :thing,
  value: 'value'
})

Because chromedriver is an actual browser, you can't set a cookie before visiting the site.

The fix was to make sure that we visit a page on the site before trying to set the cookie.

before do
  visit root_path
  page.driver.browser.manage.add_cookie({
    name: :thing,
    value: 'value'
  })
 end

Then the cookie was set as expected, and I was able to carry on with my day.