James Croft

Selenium::WebDriver::Error::NoSuchCollectionError

I wanted to manually set a cookie when using ChromeDriver to run an integration test and was running into this error:

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

I was trying to set the cookie with this code:

  def sign_in(user_id)
    page.driver.browser.manage.delete_all_cookies
    session_id   = DB[:sessions].insert(user_id: user_id)
    cookie_value = DB[:cookies].first(session_id: session_id).fetch(:cookie)
    page.driver.browser.manage.add_cookie(
      :name => 'ring-session',
      :value => cookie_value,
    )
    visit "/dashboard"
  end

The error was being thrown because I was trying to set the cookie before visiting the site, it turns out this isn’t possible.

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

  def sign_in(user_id)
    page.driver.browser.manage.delete_all_cookies
    session_id   = DB[:sessions].insert(user_id: user_id)
    cookie_value = DB[:cookies].first(session_id: session_id).fetch(:cookie)
    visit "/" # Make sure we visit the site before setting the cookie
    page.driver.browser.manage.add_cookie(
      :name => 'ring-session',
      :value => cookie_value,
    )
    visit "/dashboard"
  end