Skip to content

Commit 9d2fc1d

Browse files
committed
[rb] update default cookie setting behavior with thorough documentation in specs
1 parent 630f25f commit 9d2fc1d

File tree

4 files changed

+177
-62
lines changed

4 files changed

+177
-62
lines changed

rb/lib/selenium/webdriver/common/manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def add_cookie(opts = {})
4646
raise ArgumentError, 'name is required' unless opts[:name]
4747
raise ArgumentError, 'value is required' unless opts[:value]
4848

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

5252
same_site = opts.delete(:same_site)

rb/spec/integration/selenium/webdriver/guard_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ module SpecSupport
122122

123123
it 'Uses correct message for exclusive' do
124124
guard = Guards::Guard.new({reason: "Foo is bad"}, :exclusive)
125-
expect(guard.message).to eq 'Test does not apply to this configuration'
125+
expect(guard.message).to eq 'Test does not apply to this configuration; Foo is bad'
126126
end
127127

128128
it 'Uses correct message for exclude' do

rb/spec/integration/selenium/webdriver/manager_spec.rb

Lines changed: 174 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,103 +23,221 @@ module Selenium
2323
module WebDriver
2424
describe Manager do
2525
describe 'cookie management' do
26+
before { driver.navigate.to url_for('xhtmlTest.html') }
27+
2628
after { driver.manage.delete_all_cookies }
2729

28-
it 'should show http only when insecure' do
29-
driver.navigate.to url_for('xhtmlTest.html')
30-
driver.manage.add_cookie name: 'security',
31-
value: 'insecure',
32-
http_only: true
30+
it 'should set correct defaults' do
31+
driver.manage.add_cookie name: 'default',
32+
value: 'value'
3333

34-
expect(driver.manage.cookie_named('security')[:http_only]).to eq true
34+
cookie = driver.manage.cookie_named('default')
35+
expect(cookie[:value]).to eq('value')
36+
expect(cookie[:path]).to eq('/')
37+
expect(cookie[:domain]).to eq('localhost')
38+
expect(cookie[:http_only]).to eq(false)
39+
expect(cookie[:secure]).to eq(false)
3540
end
3641

37-
it 'should not add secure when http', except: {browser: :firefox,
38-
reason: 'https://github.com/mozilla/geckodriver/issues/1840'} do
39-
driver.navigate.to url_for('xhtmlTest.html')
40-
driver.manage.add_cookie name: 'security',
41-
value: 'secure',
42-
secure: true
42+
it 'should set samesite property of Default by default',
43+
only: {browser: %i[chrome edge firefox]},
44+
except: [{browser: :chrome,
45+
reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=3732'},
46+
{browser: :firefox,
47+
reason: 'https://github.com/mozilla/geckodriver/issues/1841'}] do
48+
driver.manage.add_cookie name: 'samesite',
49+
value: 'default'
4350

44-
cookies = driver.manage.all_cookies
45-
expect(cookies.size).to eq(0)
51+
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Default')
4652
end
4753

4854
it 'should respect path' do
49-
driver.navigate.to url_for('xhtmlTest.html')
5055
driver.manage.add_cookie name: 'path',
5156
value: 'specified',
5257
path: '/child'
53-
cookies = driver.manage.all_cookies
54-
expect(cookies.size).to eq(0)
58+
59+
expect(driver.manage.all_cookies.size).to eq(0)
5560

5661
driver.navigate.to url_for('child/childPage.html')
62+
5763
expect(driver.manage.cookie_named('path')[:path]).to eq '/child'
5864
end
5965

60-
it 'should add expiration with DateTime' do
61-
driver.navigate.to url_for('xhtmlTest.html')
66+
it 'should respect setting on domain from a subdomain',
67+
exclusive: {driver: :none,
68+
reason: "Can only be tested on site with subdomains"} do
69+
driver.get("https://opensource.saucelabs.com")
70+
71+
driver.manage.add_cookie name: 'domain',
72+
value: 'specified',
73+
domain: 'saucelabs.com'
74+
75+
expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')
6276

63-
expected = (Date.today + 2).to_datetime
64-
driver.manage.add_cookie name: 'expiration',
65-
value: 'datetime',
66-
expires: expected
77+
driver.get("https://accounts.saucelabs.com")
78+
expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')
6779

68-
actual = driver.manage.cookie_named('expiration')[:expires]
69-
expect(actual).to be_kind_of(DateTime)
70-
expect(actual).to eq(expected)
80+
driver.get("https://saucelabs.com")
81+
expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')
7182
end
7283

73-
it 'should add expiration with Time' do
74-
driver.navigate.to url_for('xhtmlTest.html')
84+
it 'should not allow domain to be set for localhost',
85+
exclude: {browser: :chrome,
86+
reason: "https://bugs.chromium.org/p/chromedriver/issues/detail?id=3733"} do
87+
expect {
88+
driver.manage.add_cookie name: 'domain',
89+
value: 'localhost',
90+
domain: 'localhost'
91+
}.to raise_error(Error::UnableToSetCookieError)
92+
end
7593

76-
expected = (Date.today + 2).to_datetime
77-
driver.manage.add_cookie name: 'expiration',
78-
value: 'time',
79-
expires: expected.to_time
94+
it 'should not allow setting on a different domain',
95+
except: {browser: :chrome,
96+
reason: "https://bugs.chromium.org/p/chromedriver/issues/detail?id=3734"} do
97+
expect {
98+
driver.manage.add_cookie name: 'domain',
99+
value: 'different',
100+
domain: 'selenium.dev'
101+
}.to raise_error(Error::InvalidCookieDomainError)
102+
end
80103

81-
actual = driver.manage.cookie_named('expiration')[:expires]
82-
expect(actual).to be_kind_of(DateTime)
83-
expect(actual).to eq(expected)
104+
it 'should not allow setting on a subdomain from parent domain',
105+
exclusive: {driver: :none,
106+
reason: "Can only be tested on site with subdomains"},
107+
except: {browser: :chrome,
108+
reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=3734'} do
109+
driver.get("https://saucelabs.com")
110+
111+
expect {
112+
driver.manage.add_cookie name: 'domain',
113+
value: 'subdomain',
114+
domain: 'opensource.saucelabs.com'
115+
}.to raise_exception(Error::InvalidCookieDomainError)
84116
end
85117

86-
it 'should add expiration with Number' do
87-
driver.navigate.to url_for('xhtmlTest.html')
118+
it 'should not be visible to javascript when http_only is true' do
119+
driver.manage.add_cookie name: 'httponly',
120+
value: 'true',
121+
http_only: true
122+
123+
expect(driver.execute_script("return document.cookie")).to be_empty
124+
expect(driver.manage.cookie_named('httponly')[:http_only]).to eq true
125+
end
88126

89-
expected = (Date.today + 2).to_datetime
90-
driver.manage.add_cookie name: 'expiration',
91-
value: 'number',
92-
expires: expected.to_time.to_f
127+
it 'should not add secure cookie when http',
128+
except: {browser: :firefox,
129+
reason: 'https://github.com/mozilla/geckodriver/issues/1840'} do
130+
driver.manage.add_cookie name: 'secure',
131+
value: 'http',
132+
secure: true
93133

94-
actual = driver.manage.cookie_named('expiration')[:expires]
95-
expect(actual).to be_kind_of(DateTime)
96-
expect(actual).to eq(expected)
134+
expect(driver.manage.all_cookies.size).to eq(0)
97135
end
98136

99-
it 'should add sameSite cookie with attribute Strict', only: {browser: %i[chrome edge firefox]} do
100-
driver.navigate.to url_for('xhtmlTest.html')
101-
driver.manage.add_cookie name: 'samesite', value: 'strict', same_site: 'Strict'
137+
it 'should add secure cookie when https',
138+
exclusive: {driver: :none,
139+
reason: "Can only be tested on https site"} do
140+
driver.get 'https://www.selenium.dev'
102141

103-
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Strict')
142+
driver.manage.add_cookie name: 'secure',
143+
value: 'https',
144+
secure: true
145+
146+
expect(driver.manage.cookie_named('secure')[:secure]).to eq(true)
104147
end
105148

106-
it 'should add sameSite cookie with attribute Lax', only: {browser: %i[chrome edge firefox]} do
107-
driver.navigate.to url_for('xhtmlTest.html')
108-
driver.manage.add_cookie name: 'samesite',
109-
value: 'lax',
110-
same_site: 'Lax'
111-
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Lax')
149+
context 'sameSite' do
150+
it 'should allow adding with value Strict', only: {browser: %i[chrome edge firefox]} do
151+
driver.manage.add_cookie name: 'samesite',
152+
value: 'strict',
153+
same_site: 'Strict'
154+
155+
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Strict')
156+
end
157+
158+
it 'should allow adding with value Lax', only: {browser: %i[chrome edge firefox]} do
159+
driver.manage.add_cookie name: 'samesite',
160+
value: 'lax',
161+
same_site: 'Lax'
162+
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Lax')
163+
end
164+
165+
it 'should allow adding with value None',
166+
exclusive: {driver: :none,
167+
reason: "Can only be tested on https site"} do
168+
driver.get 'https://selenium.dev'
169+
170+
driver.manage.add_cookie name: 'samesite',
171+
value: 'none-secure',
172+
same_site: 'None',
173+
secure: true
174+
175+
expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('None')
176+
end
177+
178+
it 'should not allow adding with value None when secure is false',
179+
except: {browser: :firefox,
180+
reason: "https://github.com/mozilla/geckodriver/issues/1842"} do
181+
expect {
182+
driver.manage.add_cookie name: 'samesite',
183+
value: 'none-insecure',
184+
same_site: 'None',
185+
secure: false
186+
}.to raise_exception(Error::UnableToSetCookieError)
187+
end
188+
end
189+
190+
context 'expiration' do
191+
it 'should allow adding with DateTime value' do
192+
expected = (Date.today + 2).to_datetime
193+
driver.manage.add_cookie name: 'expiration',
194+
value: 'datetime',
195+
expires: expected
196+
197+
actual = driver.manage.cookie_named('expiration')[:expires]
198+
expect(actual).to be_kind_of(DateTime)
199+
expect(actual).to eq(expected)
200+
end
201+
202+
it 'should allow adding with Time value' do
203+
expected = (Date.today + 2).to_datetime
204+
driver.manage.add_cookie name: 'expiration',
205+
value: 'time',
206+
expires: expected.to_time
207+
208+
actual = driver.manage.cookie_named('expiration')[:expires]
209+
expect(actual).to be_kind_of(DateTime)
210+
expect(actual).to eq(expected)
211+
end
212+
213+
it 'should allow adding with Number value' do
214+
expected = (Date.today + 2).to_datetime
215+
driver.manage.add_cookie name: 'expiration',
216+
value: 'number',
217+
expires: expected.to_time.to_f
218+
219+
actual = driver.manage.cookie_named('expiration')[:expires]
220+
expect(actual).to be_kind_of(DateTime)
221+
expect(actual).to eq(expected)
222+
end
223+
224+
it 'should not allow adding when value is in the past' do
225+
expected = (Date.today - 2).to_datetime
226+
driver.manage.add_cookie name: 'expiration',
227+
value: 'datetime',
228+
expires: expected
229+
230+
expect(driver.manage.all_cookies.size).to eq(0)
231+
end
112232
end
113233

114234
it 'should get one' do
115-
driver.navigate.to url_for('xhtmlTest.html')
116235
driver.manage.add_cookie name: 'foo', value: 'bar'
117236

118237
expect(driver.manage.cookie_named('foo')[:value]).to eq('bar')
119238
end
120239

121240
it 'should get all' do
122-
driver.navigate.to url_for('xhtmlTest.html')
123241
driver.manage.add_cookie name: 'foo', value: 'bar'
124242

125243
cookies = driver.manage.all_cookies
@@ -130,16 +248,13 @@ module WebDriver
130248
end
131249

132250
it 'should delete one' do
133-
driver.navigate.to url_for('xhtmlTest.html')
134251
driver.manage.add_cookie name: 'foo', value: 'bar'
135252

136253
driver.manage.delete_cookie('foo')
137254
expect(driver.manage.all_cookies.find { |c| c[:name] == 'foo' }).to be_nil
138255
end
139256

140257
it 'should delete all' do
141-
driver.navigate.to url_for('xhtmlTest.html')
142-
143258
driver.manage.add_cookie name: 'foo', value: 'bar'
144259
driver.manage.add_cookie name: 'bar', value: 'foo'
145260
driver.manage.delete_all_cookies

rb/spec/integration/selenium/webdriver/spec_support/guards/guard.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def message
5454
when :exclude
5555
"Test not guarded because it breaks test run; #{details}"
5656
when :exclusive
57-
'Test does not apply to this configuration'
57+
"Test does not apply to this configuration; #{details}"
5858
else
5959
"Test guarded; #{details}"
6060
end

0 commit comments

Comments
 (0)