Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/manager_spec.rb
1864 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
require_relative 'spec_helper'
21
22
module Selenium
23
module WebDriver
24
describe Manager, exclusive: {bidi: false, reason: 'Not yet implemented with BiDi'} do
25
describe 'cookie management' do
26
before { driver.navigate.to url_for('xhtmlTest.html') }
27
28
after do
29
if GlobalTestEnv.rbe? && GlobalTestEnv.browser == :chrome
30
reset_driver!
31
else
32
driver.manage.delete_all_cookies
33
end
34
end
35
36
it 'sets correct defaults' do
37
driver.manage.add_cookie name: 'default',
38
value: 'value'
39
40
cookie = driver.manage.cookie_named('default')
41
expect(cookie[:value]).to eq('value')
42
expect(cookie[:path]).to eq('/')
43
expect(cookie[:domain]).to eq('localhost')
44
expect(cookie[:http_only]).to be(false)
45
expect(cookie[:secure]).to be(false)
46
end
47
48
it 'sets samesite property of Lax by default',
49
except: {browser: :firefox,
50
reason: 'https://github.com/mozilla/geckodriver/issues/1841'},
51
only: {browser: %i[chrome edge firefox]} do
52
driver.manage.add_cookie name: 'samesite',
53
value: 'default'
54
55
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Lax')
56
end
57
58
it 'respects path' do
59
driver.manage.add_cookie name: 'path',
60
value: 'specified',
61
path: '/child'
62
63
expect(driver.manage.all_cookies.size).to eq(0)
64
65
driver.navigate.to url_for('child/childPage.html')
66
67
expect(driver.manage.cookie_named('path')[:path]).to eq '/child'
68
end
69
70
it 'respects setting on domain from a subdomain',
71
exclusive: {driver: :none,
72
reason: 'Can only be tested on site with subdomains'} do
73
driver.get('https://opensource.saucelabs.com')
74
75
driver.manage.add_cookie name: 'domain',
76
value: 'specified',
77
domain: 'saucelabs.com'
78
79
expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')
80
81
driver.get('https://accounts.saucelabs.com')
82
expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')
83
84
driver.get('https://saucelabs.com')
85
expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')
86
end
87
88
it 'does not allow setting on a different domain', except: {browser: %i[safari safari_preview]} do
89
expect {
90
driver.manage.add_cookie name: 'domain',
91
value: 'different',
92
domain: 'selenium.dev'
93
}.to raise_error(Error::InvalidCookieDomainError)
94
end
95
96
it 'does not allow setting on a subdomain from parent domain',
97
exclusive: {driver: :none,
98
reason: 'Can not run on our test server; needs subdomains'} do
99
driver.get('https://saucelabs.com')
100
101
expect {
102
driver.manage.add_cookie name: 'domain',
103
value: 'subdomain',
104
domain: 'opensource.saucelabs.com'
105
}.to raise_exception(Error::InvalidCookieDomainError)
106
end
107
108
it 'is not visible to javascript when http_only is true' do
109
driver.manage.add_cookie name: 'httponly',
110
value: 'true',
111
http_only: true
112
113
expect(driver.execute_script('return document.cookie')).to be_empty
114
expect(driver.manage.cookie_named('httponly')[:http_only]).to be true
115
end
116
117
it 'does not add secure cookie when http',
118
except: {browser: :firefox,
119
reason: 'https://github.com/mozilla/geckodriver/issues/1840'},
120
exclusive: {driver: :none,
121
reason: 'Cannot be tested on localhost'} do
122
driver.get 'http://watir.com'
123
driver.manage.add_cookie name: 'secure',
124
value: 'http',
125
secure: true
126
127
expect(driver.manage.all_cookies.size).to eq(0)
128
end
129
130
it 'adds secure cookie when https',
131
exclusive: {driver: :none,
132
reason: 'Can only be tested on https site'} do
133
driver.get 'https://www.selenium.dev'
134
135
driver.manage.add_cookie name: 'secure',
136
value: 'https',
137
secure: true
138
139
expect(driver.manage.cookie_named('secure')[:secure]).to be(true)
140
end
141
142
describe 'sameSite' do
143
it 'allows adding with value Strict' do
144
driver.manage.add_cookie name: 'samesite',
145
value: 'strict',
146
same_site: 'Strict'
147
148
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Strict')
149
end
150
151
it 'allows adding with value Lax' do
152
driver.manage.add_cookie name: 'samesite',
153
value: 'lax',
154
same_site: 'Lax'
155
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Lax')
156
end
157
158
it 'allows adding with value None',
159
exclusive: {driver: :none,
160
reason: 'Can only be tested on https site'} do
161
driver.get 'https://selenium.dev'
162
163
driver.manage.add_cookie name: 'samesite',
164
value: 'none-secure',
165
same_site: 'None',
166
secure: true
167
168
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('None')
169
end
170
171
it 'does not allow adding with value None when secure is false',
172
except: [{browser: %i[safari safari_preview]}] do
173
expect {
174
driver.manage.add_cookie name: 'samesite',
175
value: 'none-insecure',
176
same_site: 'None',
177
secure: false
178
}.to raise_exception(Error::UnableToSetCookieError)
179
end
180
end
181
182
describe 'expiration' do
183
it 'allows adding with DateTime value' do
184
expected = (Date.today + 2).to_datetime
185
driver.manage.add_cookie name: 'expiration',
186
value: 'datetime',
187
expires: expected
188
189
actual = driver.manage.cookie_named('expiration')[:expires]
190
expect(actual).to be_a(DateTime)
191
expect(actual).to eq(expected)
192
end
193
194
it 'allows adding with Time value' do
195
expected = (Date.today + 2).to_datetime
196
driver.manage.add_cookie name: 'expiration',
197
value: 'time',
198
expires: expected.to_time
199
200
actual = driver.manage.cookie_named('expiration')[:expires]
201
expect(actual).to be_a(DateTime)
202
expect(actual).to eq(expected)
203
end
204
205
it 'allows adding with Number value' do
206
expected = (Date.today + 2).to_datetime
207
driver.manage.add_cookie name: 'expiration',
208
value: 'number',
209
expires: expected.to_time.to_f
210
211
actual = driver.manage.cookie_named('expiration')[:expires]
212
expect(actual).to be_a(DateTime)
213
expect(actual).to eq(expected)
214
end
215
216
it 'does not allow adding when value is in the past' do
217
expected = (Date.today - 2).to_datetime
218
driver.manage.add_cookie name: 'expiration',
219
value: 'datetime',
220
expires: expected
221
222
expect(driver.manage.all_cookies.size).to eq(0)
223
end
224
end
225
226
it 'gets one' do
227
driver.manage.add_cookie name: 'foo', value: 'bar'
228
229
expect(driver.manage.cookie_named('foo')[:value]).to eq('bar')
230
end
231
232
it 'gets all' do
233
driver.manage.add_cookie name: 'foo', value: 'bar'
234
235
cookies = driver.manage.all_cookies
236
237
expect(cookies.size).to eq(1)
238
expect(cookies.first[:name]).to eq('foo')
239
expect(cookies.first[:value]).to eq('bar')
240
end
241
242
it 'deletes one' do
243
driver.manage.add_cookie name: 'foo', value: 'bar'
244
driver.manage.delete_cookie('foo')
245
expect(driver.manage.all_cookies.find { |c| c[:name] == 'foo' }).to be_nil
246
end
247
248
it 'deletes all' do
249
driver.manage.add_cookie name: 'foo', value: 'bar'
250
driver.manage.add_cookie name: 'bar', value: 'foo'
251
driver.manage.delete_all_cookies
252
expect(driver.manage.all_cookies).to be_empty
253
end
254
255
it 'throws error when fetching non-existent cookie' do
256
expect { driver.manage.cookie_named('non-existent') }
257
.to raise_exception(Error::NoSuchCookieError)
258
end
259
260
it 'throws an error when cookie name is an empty string' do
261
expect { driver.manage.delete_cookie('') }
262
.to raise_error(ArgumentError, /Cookie name cannot be null or empty/)
263
end
264
265
it 'throws an error when cookie name is a nil string' do
266
expect { driver.manage.delete_cookie(nil) }
267
.to raise_error(ArgumentError, /Cookie name cannot be null or empty/)
268
end
269
270
it 'allows deleting a cookies using a symbol' do
271
driver.manage.add_cookie name: :foo, value: 'bar'
272
driver.manage.delete_cookie(:foo)
273
expect(driver.manage.all_cookies).to be_empty
274
end
275
end
276
end # Options
277
end # WebDriver
278
end # Selenium
279
280