Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/manager.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
class Manager
23
#
24
# @api private
25
#
26
27
def initialize(bridge)
28
@bridge = bridge
29
end
30
31
#
32
# Add a cookie to the browser
33
#
34
# @param [Hash] opts the options to create a cookie with.
35
# @option opts [String] :name A name
36
# @option opts [String] :value A value
37
# @option opts [String] :path ('/') A path
38
# @option opts [String] :secure (false) A boolean
39
# @option opts [String] :same_site (Strict or Lax) currently supported only in chrome 80+ versions
40
# @option opts [Time,DateTime,Numeric,nil] :expires (nil) Expiry date, either as a Time, DateTime, or seconds since epoch.
41
#
42
# @raise [ArgumentError] if :name or :value is not specified
43
#
44
45
def add_cookie(opts = {})
46
raise ArgumentError, 'name is required' unless opts[:name]
47
raise ArgumentError, 'value is required' unless opts[:value]
48
49
# NOTE: This is required because of https://bugs.chromium.org/p/chromedriver/issues/detail?id=3732
50
opts[:secure] ||= false
51
52
same_site = opts.delete(:same_site)
53
opts[:sameSite] = same_site if same_site
54
55
http_only = opts.delete(:http_only)
56
opts[:httpOnly] = http_only if http_only
57
58
obj = opts.delete(:expires)
59
opts[:expiry] = seconds_from(obj).to_int if obj
60
61
@bridge.add_cookie opts
62
end
63
64
#
65
# Get the cookie with the given name
66
#
67
# @param [String] name the name of the cookie
68
# @return [Hash] the cookie, or throws a NoSuchCookieError if it wasn't found.
69
#
70
71
def cookie_named(name)
72
convert_cookie(@bridge.cookie(name))
73
end
74
75
#
76
# Delete the cookie with the given name
77
#
78
# @param [String] name the name of the cookie to delete
79
#
80
81
def delete_cookie(name)
82
raise ArgumentError, 'Cookie name cannot be null or empty' if name.nil? || name.to_s.strip.empty?
83
84
@bridge.delete_cookie name
85
end
86
87
#
88
# Delete all cookies
89
#
90
91
def delete_all_cookies
92
@bridge.delete_all_cookies
93
end
94
95
#
96
# Get all cookies
97
#
98
# @return [Array<Hash>] list of cookies
99
#
100
101
def all_cookies
102
@bridge.cookies.map { |cookie| convert_cookie(cookie) }
103
end
104
105
def timeouts
106
@timeouts ||= Timeouts.new(@bridge)
107
end
108
109
def window
110
@window ||= Window.new(@bridge)
111
end
112
113
private
114
115
SECONDS_PER_DAY = 86_400.0
116
117
def datetime_at(int)
118
DateTime.civil(1970) + (int / SECONDS_PER_DAY)
119
end
120
121
def seconds_from(obj)
122
case obj
123
when Time
124
obj.to_f
125
when DateTime
126
(obj - DateTime.civil(1970)) * SECONDS_PER_DAY
127
when Numeric
128
obj
129
else
130
raise ArgumentError, "invalid value for expiration date: #{obj.inspect}"
131
end
132
end
133
134
def strip_port(str)
135
str.split(':', 2).first
136
end
137
138
def convert_cookie(cookie)
139
{
140
name: cookie['name'],
141
value: cookie['value'],
142
path: cookie['path'],
143
domain: cookie['domain'] && strip_port(cookie['domain']),
144
expires: cookie['expiry'] && datetime_at(cookie['expiry']),
145
same_site: cookie['sameSite'],
146
http_only: cookie['httpOnly'],
147
secure: cookie['secure']
148
}
149
end
150
end # Options
151
end # WebDriver
152
end # Selenium
153
154