Method: Selenium::WebDriver::Manager#add_cookie

Defined in:
lib/selenium/webdriver/common/manager.rb

Add a cookie to the browser

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a cookie with.

Options Hash (opts):

  • :name (String)

    A name

  • :value (String)

    A value

  • :path (String) — default: '/'

    A path

  • :secure (String) — default: false

    A boolean

  • :same_site (String) — default: Strict or Lax

    currently supported only in chrome 80+ versions

  • :expires (Time, DateTime, Numeric, nil) — default: nil

    Expiry date, either as a Time, DateTime, or seconds since epoch.

Raises:

  • (ArgumentError)

    if :name or :value is not specified


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/selenium/webdriver/common/manager.rb', line 45

def add_cookie(opts = {})
  raise ArgumentError, 'name is required' unless opts[:name]
  raise ArgumentError, 'value is required' unless opts[:value]

  # NOTE: This is required because of https://bugs.chromium.org/p/chromedriver/issues/detail?id=3732
  opts[:secure] ||= false

  same_site = opts.delete(:same_site)
  opts[:sameSite] = same_site if same_site

  http_only = opts.delete(:http_only)
  opts[:httpOnly] = http_only if http_only

  obj = opts.delete(:expires)
  opts[:expiry] = seconds_from(obj).to_int if obj

  @bridge.add_cookie opts
end