@@ -23,103 +23,221 @@ module Selenium
23
23
module WebDriver
24
24
describe Manager do
25
25
describe 'cookie management' do
26
+ before { driver . navigate . to url_for ( 'xhtmlTest.html' ) }
27
+
26
28
after { driver . manage . delete_all_cookies }
27
29
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'
33
33
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 )
35
40
end
36
41
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'
43
50
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' )
46
52
end
47
53
48
54
it 'should respect path' do
49
- driver . navigate . to url_for ( 'xhtmlTest.html' )
50
55
driver . manage . add_cookie name : 'path' ,
51
56
value : 'specified' ,
52
57
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 )
55
60
56
61
driver . navigate . to url_for ( 'child/childPage.html' )
62
+
57
63
expect ( driver . manage . cookie_named ( 'path' ) [ :path ] ) . to eq '/child'
58
64
end
59
65
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' )
62
76
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' )
67
79
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' )
71
82
end
72
83
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
75
93
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
80
103
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 )
84
116
end
85
117
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
88
126
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
93
133
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 )
97
135
end
98
136
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'
102
141
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 )
104
147
end
105
148
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
112
232
end
113
233
114
234
it 'should get one' do
115
- driver . navigate . to url_for ( 'xhtmlTest.html' )
116
235
driver . manage . add_cookie name : 'foo' , value : 'bar'
117
236
118
237
expect ( driver . manage . cookie_named ( 'foo' ) [ :value ] ) . to eq ( 'bar' )
119
238
end
120
239
121
240
it 'should get all' do
122
- driver . navigate . to url_for ( 'xhtmlTest.html' )
123
241
driver . manage . add_cookie name : 'foo' , value : 'bar'
124
242
125
243
cookies = driver . manage . all_cookies
@@ -130,16 +248,13 @@ module WebDriver
130
248
end
131
249
132
250
it 'should delete one' do
133
- driver . navigate . to url_for ( 'xhtmlTest.html' )
134
251
driver . manage . add_cookie name : 'foo' , value : 'bar'
135
252
136
253
driver . manage . delete_cookie ( 'foo' )
137
254
expect ( driver . manage . all_cookies . find { |c | c [ :name ] == 'foo' } ) . to be_nil
138
255
end
139
256
140
257
it 'should delete all' do
141
- driver . navigate . to url_for ( 'xhtmlTest.html' )
142
-
143
258
driver . manage . add_cookie name : 'foo' , value : 'bar'
144
259
driver . manage . add_cookie name : 'bar' , value : 'foo'
145
260
driver . manage . delete_all_cookies
0 commit comments