Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/CookieImplementationTest.java
4018 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import static org.assertj.core.api.Assertions.assertThat;
21
import static org.assertj.core.api.Assertions.assertThatThrownBy;
22
import static org.junit.jupiter.api.Assumptions.assumeTrue;
23
import static org.openqa.selenium.testing.drivers.Browser.ALL;
24
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
25
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
26
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
27
import static org.openqa.selenium.testing.drivers.Browser.IE;
28
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
29
30
import java.net.URI;
31
import java.util.Date;
32
import java.util.Random;
33
import java.util.Set;
34
import org.junit.jupiter.api.BeforeEach;
35
import org.junit.jupiter.api.Test;
36
import org.openqa.selenium.environment.DomainHelper;
37
import org.openqa.selenium.testing.Ignore;
38
import org.openqa.selenium.testing.JupiterTestBase;
39
import org.openqa.selenium.testing.NeedsSecureServer;
40
import org.openqa.selenium.testing.NotWorkingInRemoteBazelBuilds;
41
import org.openqa.selenium.testing.SwitchToTopAfterTest;
42
43
@NeedsSecureServer
44
class CookieImplementationTest extends JupiterTestBase {
45
46
private DomainHelper domainHelper;
47
private String cookiePage;
48
private static final Random random = new Random();
49
50
@BeforeEach
51
public void setUp() {
52
domainHelper = new DomainHelper(appServer);
53
assumeTrue(domainHelper.checkIsOnValidHostname());
54
cookiePage = domainHelper.getUrlForFirstValidHostname("/common/cookie");
55
56
deleteAllCookiesOnServerSide();
57
58
// This page is the deepest page we go to in the cookie tests
59
// We go to it to ensure that cookies with /common/... paths are deleted
60
// Do not write test in this class which use pages other than under /common
61
// without ensuring that cookies are deleted on those pages as required
62
try {
63
driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));
64
} catch (IllegalArgumentException e) {
65
// Ideally we would throw an IgnoredTestError or something here,
66
// but our test runner doesn't pay attention to those.
67
// Rely on the tests skipping themselves if they need to be on a useful page.
68
return;
69
}
70
71
driver.manage().deleteAllCookies();
72
assertNoCookiesArePresent();
73
}
74
75
@Test
76
public void testShouldGetCookieByName() {
77
String key = generateUniqueKey();
78
String value = "set";
79
assertCookieIsNotPresentWithName(key);
80
81
addCookieOnServerSide(new Cookie(key, value));
82
83
Cookie cookie = driver.manage().getCookieNamed(key);
84
assertThat(cookie.getValue()).isEqualTo(value);
85
}
86
87
@Test
88
@NotWorkingInRemoteBazelBuilds(FIREFOX)
89
public void testShouldBeAbleToAddCookie() {
90
String key = generateUniqueKey();
91
String value = "foo";
92
Cookie cookie = new Cookie.Builder(key, value).domain(domainHelper.getHostName()).build();
93
assertCookieIsNotPresentWithName(key);
94
95
driver.manage().addCookie(cookie);
96
97
assertCookieHasValue(key, value);
98
99
openAnotherPage();
100
assertCookieHasValue(key, value);
101
}
102
103
@Test
104
public void testGetAllCookies() {
105
String key1 = generateUniqueKey();
106
String key2 = generateUniqueKey();
107
108
assertCookieIsNotPresentWithName(key1);
109
assertCookieIsNotPresentWithName(key2);
110
111
int countBefore = driver.manage().getCookies().size();
112
113
Cookie one = new Cookie.Builder(key1, "value").build();
114
Cookie two = new Cookie.Builder(key2, "value").build();
115
116
driver.manage().addCookie(one);
117
driver.manage().addCookie(two);
118
119
openAnotherPage();
120
Set<Cookie> cookies = driver.manage().getCookies();
121
assertThat(cookies).hasSize(countBefore + 2);
122
123
assertThat(cookies).contains(one, two);
124
}
125
126
@Test
127
public void testDeleteAllCookies() {
128
addCookieOnServerSide(new Cookie("foo", "set"));
129
assertSomeCookiesArePresent();
130
131
driver.manage().deleteAllCookies();
132
133
assertNoCookiesArePresent();
134
135
openAnotherPage();
136
assertNoCookiesArePresent();
137
}
138
139
@Test
140
public void testDeleteCookieWithName() {
141
String key1 = generateUniqueKey();
142
String key2 = generateUniqueKey();
143
144
addCookieOnServerSide(new Cookie(key1, "set"));
145
addCookieOnServerSide(new Cookie(key2, "set"));
146
147
assertCookieIsPresentWithName(key1);
148
assertCookieIsPresentWithName(key2);
149
150
driver.manage().deleteCookieNamed(key1);
151
152
assertCookieIsNotPresentWithName(key1);
153
assertCookieIsPresentWithName(key2);
154
155
openAnotherPage();
156
assertCookieIsNotPresentWithName(key1);
157
assertCookieIsPresentWithName(key2);
158
}
159
160
@Test
161
public void testShouldNotDeleteCookiesWithASimilarName() {
162
String cookieOneName = "fish";
163
Cookie cookie1 = new Cookie.Builder(cookieOneName, "cod").build();
164
Cookie cookie2 = new Cookie.Builder(cookieOneName + "x", "earth").build();
165
WebDriver.Options options = driver.manage();
166
assertCookieIsNotPresentWithName(cookie1.getName());
167
168
options.addCookie(cookie1);
169
options.addCookie(cookie2);
170
171
assertCookieIsPresentWithName(cookie1.getName());
172
173
options.deleteCookieNamed(cookieOneName);
174
Set<Cookie> cookies = options.getCookies();
175
176
assertThat(cookies).doesNotContain(cookie1);
177
assertThat(cookies).contains(cookie2);
178
}
179
180
@Test
181
public void testAddCookiesWithDifferentPathsThatAreRelatedToOurs() {
182
driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));
183
Cookie cookie1 = new Cookie.Builder("fish", "cod").path("/common/animals").build();
184
Cookie cookie2 = new Cookie.Builder("planet", "earth").path("/common/").build();
185
WebDriver.Options options = driver.manage();
186
options.addCookie(cookie1);
187
options.addCookie(cookie2);
188
189
driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));
190
191
assertCookieIsPresentWithName(cookie1.getName());
192
assertCookieIsPresentWithName(cookie2.getName());
193
194
driver.get(domainHelper.getUrlForFirstValidHostname("/common/simpleTest.html"));
195
assertCookieIsNotPresentWithName(cookie1.getName());
196
}
197
198
@SwitchToTopAfterTest
199
@Test
200
@Ignore(SAFARI)
201
@NotWorkingInRemoteBazelBuilds(CHROME)
202
@NotWorkingInRemoteBazelBuilds(EDGE)
203
@NotWorkingInRemoteBazelBuilds(FIREFOX)
204
public void testGetCookiesInAFrame() {
205
driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));
206
Cookie cookie1 = new Cookie.Builder("fish", "cod").path("/common/animals").build();
207
driver.manage().addCookie(cookie1);
208
209
driver.get(domainHelper.getUrlForFirstValidHostname("frameWithAnimals.html"));
210
assertCookieIsNotPresentWithName(cookie1.getName());
211
212
driver.switchTo().frame("iframe1");
213
assertCookieIsPresentWithName(cookie1.getName());
214
}
215
216
@Test
217
public void testCannotGetCookiesWithPathDifferingOnlyInCase() {
218
String cookieName = "fish";
219
Cookie cookie = new Cookie.Builder(cookieName, "cod").path("/Common/animals").build();
220
driver.manage().addCookie(cookie);
221
222
driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));
223
assertThat(driver.manage().getCookieNamed(cookieName)).isNull();
224
}
225
226
@Test
227
public void testShouldNotGetCookieOnDifferentDomain() {
228
assumeTrue(domainHelper.checkHasValidAlternateHostname());
229
230
String cookieName = "fish";
231
driver.manage().addCookie(new Cookie.Builder(cookieName, "cod").build());
232
assertCookieIsPresentWithName(cookieName);
233
234
driver.get(domainHelper.getUrlForSecondValidHostname("simpleTest.html"));
235
236
assertCookieIsNotPresentWithName(cookieName);
237
}
238
239
@Test
240
@NotWorkingInRemoteBazelBuilds(FIREFOX)
241
public void testShouldBeAbleToAddToADomainWhichIsRelatedToTheCurrentDomain() {
242
String cookieName = "name";
243
assertCookieIsNotPresentWithName(cookieName);
244
245
String shorter = domainHelper.getHostName().replaceFirst(".*?\\.", ".");
246
Cookie cookie = new Cookie.Builder(cookieName, "value").domain(shorter).build();
247
driver.manage().addCookie(cookie);
248
249
assertCookieIsPresentWithName(cookieName);
250
}
251
252
@Test
253
@Ignore(ALL)
254
public void testsShouldNotGetCookiesRelatedToCurrentDomainWithoutLeadingPeriod() {
255
String cookieName = "name";
256
assertCookieIsNotPresentWithName(cookieName);
257
258
String shorter = domainHelper.getHostName().replaceFirst(".*?\\.", "");
259
Cookie cookie = new Cookie.Builder(cookieName, "value").domain(shorter).build();
260
driver.manage().addCookie(cookie);
261
assertCookieIsNotPresentWithName(cookieName);
262
}
263
264
@Test
265
@NotWorkingInRemoteBazelBuilds(FIREFOX)
266
void testShouldBeAbleToIncludeLeadingPeriodInDomainName() {
267
String cookieName = "name";
268
assertCookieIsNotPresentWithName(cookieName);
269
270
String shorter = domainHelper.getHostName().replaceFirst(".*?\\.", ".");
271
Cookie cookie = new Cookie.Builder("name", "value").domain(shorter).build();
272
273
driver.manage().addCookie(cookie);
274
275
assertCookieIsPresentWithName(cookieName);
276
}
277
278
@Test
279
@NotWorkingInRemoteBazelBuilds(FIREFOX)
280
public void testShouldBeAbleToSetDomainToTheCurrentDomain() throws Exception {
281
URI url = new URI(driver.getCurrentUrl());
282
String host = url.getHost() + ":" + url.getPort();
283
284
Cookie cookie = new Cookie.Builder("fish", "cod").domain(host).build();
285
driver.manage().addCookie(cookie);
286
287
driver.get(domainHelper.getUrlForFirstValidHostname("javascriptPage.html"));
288
Set<Cookie> cookies = driver.manage().getCookies();
289
assertThat(cookies).contains(cookie);
290
}
291
292
@Test
293
@NotWorkingInRemoteBazelBuilds(CHROME)
294
@NotWorkingInRemoteBazelBuilds(EDGE)
295
@NotWorkingInRemoteBazelBuilds(FIREFOX)
296
public void testShouldWalkThePathToDeleteACookie() {
297
Cookie cookie1 = new Cookie.Builder("fish", "cod").build();
298
driver.manage().addCookie(cookie1);
299
300
driver.get(domainHelper.getUrlForFirstValidHostname("child/childPage.html"));
301
Cookie cookie2 = new Cookie("rodent", "hamster", "/common/child");
302
driver.manage().addCookie(cookie2);
303
304
driver.get(domainHelper.getUrlForFirstValidHostname("child/grandchild/grandchildPage.html"));
305
Cookie cookie3 = new Cookie("dog", "dalmatian", "/common/child/grandchild/");
306
driver.manage().addCookie(cookie3);
307
308
driver.get(domainHelper.getUrlForFirstValidHostname("child/grandchild/grandchildPage.html"));
309
driver.manage().deleteCookieNamed("rodent");
310
311
assertThat(driver.manage().getCookieNamed("rodent")).isNull();
312
313
Set<Cookie> cookies = driver.manage().getCookies();
314
assertThat(cookies).containsExactlyInAnyOrder(cookie1, cookie3);
315
316
driver.manage().deleteAllCookies();
317
driver.get(domainHelper.getUrlForFirstValidHostname("child/grandchild/grandchildPage.html"));
318
assertNoCookiesArePresent();
319
}
320
321
@Test
322
@NotWorkingInRemoteBazelBuilds(FIREFOX)
323
public void testShouldIgnoreThePortNumberOfTheHostWhenSettingTheCookie() throws Exception {
324
URI uri = new URI(driver.getCurrentUrl());
325
String host = String.format("%s:%d", uri.getHost(), uri.getPort());
326
327
String cookieName = "name";
328
assertCookieIsNotPresentWithName(cookieName);
329
330
Cookie cookie = new Cookie.Builder(cookieName, "value").domain(host).build();
331
driver.manage().addCookie(cookie);
332
333
assertCookieIsPresentWithName(cookieName);
334
}
335
336
@Test
337
@NotWorkingInRemoteBazelBuilds(CHROME)
338
@NotWorkingInRemoteBazelBuilds(EDGE)
339
@NotWorkingInRemoteBazelBuilds(FIREFOX)
340
public void testCookieEqualityAfterSetAndGet() {
341
driver.get(domainHelper.getUrlForFirstValidHostname("animals"));
342
343
driver.manage().deleteAllCookies();
344
345
Cookie addedCookie =
346
new Cookie.Builder("fish", "cod")
347
.path("/common/animals")
348
.expiresOn(someTimeInTheFuture())
349
.build();
350
driver.manage().addCookie(addedCookie);
351
352
Set<Cookie> cookies = driver.manage().getCookies();
353
Cookie retrievedCookie = null;
354
for (Cookie temp : cookies) {
355
if (addedCookie.equals(temp)) {
356
retrievedCookie = temp;
357
break;
358
}
359
}
360
361
assertThat(retrievedCookie).isNotNull();
362
// Cookie.equals only compares name, domain and path
363
assertThat(retrievedCookie).isEqualTo(addedCookie);
364
}
365
366
@Test
367
public void testRetainsCookieExpiry() {
368
Cookie addedCookie =
369
new Cookie.Builder("fish", "cod")
370
.path("/common/animals")
371
.expiresOn(someTimeInTheFuture())
372
.build();
373
driver.manage().addCookie(addedCookie);
374
375
Cookie retrieved = driver.manage().getCookieNamed("fish");
376
assertThat(retrieved).isNotNull();
377
assertThat(retrieved.getExpiry()).isEqualTo(addedCookie.getExpiry());
378
}
379
380
@Test
381
@Ignore(IE)
382
@Ignore(SAFARI)
383
@NotWorkingInRemoteBazelBuilds(CHROME)
384
@NotWorkingInRemoteBazelBuilds(EDGE)
385
@NotWorkingInRemoteBazelBuilds(FIREFOX)
386
public void canHandleSecureCookie() {
387
driver.get(domainHelper.getSecureUrlForFirstValidHostname("animals"));
388
389
Cookie addedCookie =
390
new Cookie.Builder("fish", "cod").path("/common/animals").isSecure(true).build();
391
driver.manage().addCookie(addedCookie);
392
393
driver.navigate().refresh();
394
395
Cookie retrieved = driver.manage().getCookieNamed("fish");
396
assertThat(retrieved).isNotNull();
397
}
398
399
@Test
400
@Ignore(IE)
401
@Ignore(SAFARI)
402
@NotWorkingInRemoteBazelBuilds(CHROME)
403
@NotWorkingInRemoteBazelBuilds(EDGE)
404
@NotWorkingInRemoteBazelBuilds(FIREFOX)
405
public void testRetainsCookieSecure() {
406
driver.get(domainHelper.getSecureUrlForFirstValidHostname("animals"));
407
408
Cookie addedCookie =
409
new Cookie.Builder("fish", "cod").path("/common/animals").isSecure(true).build();
410
driver.manage().addCookie(addedCookie);
411
412
driver.navigate().refresh();
413
414
Cookie retrieved = driver.manage().getCookieNamed("fish");
415
assertThat(retrieved).isNotNull();
416
assertThat(retrieved.isSecure()).isTrue();
417
}
418
419
@Test
420
@Ignore(SAFARI)
421
@NotWorkingInRemoteBazelBuilds(CHROME)
422
@NotWorkingInRemoteBazelBuilds(EDGE)
423
@NotWorkingInRemoteBazelBuilds(FIREFOX)
424
public void canHandleHttpOnlyCookie() {
425
Cookie addedCookie =
426
new Cookie.Builder("fish", "cod").path("/common/animals").isHttpOnly(true).build();
427
428
addCookieOnServerSide(addedCookie);
429
430
driver.get(domainHelper.getUrlForFirstValidHostname("animals"));
431
Cookie retrieved = driver.manage().getCookieNamed("fish");
432
assertThat(retrieved).isNotNull();
433
}
434
435
@Test
436
@Ignore(SAFARI)
437
@NotWorkingInRemoteBazelBuilds(CHROME)
438
@NotWorkingInRemoteBazelBuilds(EDGE)
439
@NotWorkingInRemoteBazelBuilds(FIREFOX)
440
public void testRetainsHttpOnlyFlag() {
441
Cookie addedCookie =
442
new Cookie.Builder("fish", "cod").path("/common/animals").isHttpOnly(true).build();
443
444
addCookieOnServerSide(addedCookie);
445
446
driver.get(domainHelper.getUrlForFirstValidHostname("animals"));
447
Cookie retrieved = driver.manage().getCookieNamed("fish");
448
assertThat(retrieved).isNotNull();
449
assertThat(retrieved.isHttpOnly()).isTrue();
450
}
451
452
@Test
453
public void testSettingACookieThatExpiredInThePast() {
454
long expires = System.currentTimeMillis() - 1000;
455
Cookie cookie = new Cookie.Builder("expired", "yes").expiresOn(new Date(expires)).build();
456
driver.manage().addCookie(cookie);
457
458
cookie = driver.manage().getCookieNamed("fish");
459
assertThat(cookie)
460
.as("Cookie expired before it was set, so nothing should be returned")
461
.isNull();
462
}
463
464
@Test
465
public void testCanSetCookieWithoutOptionalFieldsSet() {
466
String key = generateUniqueKey();
467
String value = "foo";
468
Cookie cookie = new Cookie(key, value);
469
assertCookieIsNotPresentWithName(key);
470
471
driver.manage().addCookie(cookie);
472
473
assertCookieHasValue(key, value);
474
}
475
476
@Test
477
public void testDeleteNotExistedCookie() {
478
String key = generateUniqueKey();
479
assertCookieIsNotPresentWithName(key);
480
481
driver.manage().deleteCookieNamed(key);
482
}
483
484
@Test
485
public void testDeleteEmptyNamedCookie() {
486
assertThatThrownBy(() -> driver.manage().deleteCookieNamed(""))
487
.isInstanceOf(IllegalArgumentException.class)
488
.hasMessage("Cookie name cannot be empty");
489
assertThatThrownBy(() -> driver.manage().deleteCookieNamed(" "))
490
.isInstanceOf(IllegalArgumentException.class)
491
.hasMessage("Cookie name cannot be empty");
492
}
493
494
@Test
495
public void testGetEmptyNamedCookie() {
496
assertThatThrownBy(() -> driver.manage().getCookieNamed(""))
497
.isInstanceOf(IllegalArgumentException.class)
498
.hasMessage("Cookie name cannot be empty");
499
assertThatThrownBy(() -> driver.manage().getCookieNamed(" "))
500
.isInstanceOf(IllegalArgumentException.class)
501
.hasMessage("Cookie name cannot be empty");
502
}
503
504
@Test
505
@Ignore(value = ALL, reason = "Non W3C conformant")
506
public void testShouldDeleteOneOfTheCookiesWithTheSameName() {
507
driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));
508
Cookie cookie1 =
509
new Cookie.Builder("fish", "cod")
510
.domain(domainHelper.getHostName())
511
.path("/common/animals")
512
.build();
513
Cookie cookie2 =
514
new Cookie.Builder("fish", "tune")
515
.domain(domainHelper.getHostName())
516
.path("/common/")
517
.build();
518
WebDriver.Options options = driver.manage();
519
options.addCookie(cookie1);
520
options.addCookie(cookie2);
521
assertThat(driver.manage().getCookies()).hasSize(2);
522
523
driver.manage().deleteCookie(cookie1);
524
525
assertThat(driver.manage().getCookies()).hasSize(1);
526
Cookie retrieved = driver.manage().getCookieNamed("fish");
527
assertThat(retrieved).isEqualTo(cookie2);
528
}
529
530
private String generateUniqueKey() {
531
return String.format("key_%d", random.nextInt());
532
}
533
534
private void assertNoCookiesArePresent() {
535
assertThat(driver.manage().getCookies()).isEmpty();
536
String documentCookie = getDocumentCookieOrNull();
537
if (documentCookie != null) {
538
assertThat(documentCookie).isEmpty();
539
}
540
}
541
542
private void assertSomeCookiesArePresent() {
543
assertThat(driver.manage().getCookies()).isNotEmpty();
544
String documentCookie = getDocumentCookieOrNull();
545
if (documentCookie != null) {
546
assertThat(documentCookie).as("Cookies were empty").isNotEqualTo("");
547
}
548
}
549
550
private void assertCookieIsNotPresentWithName(final String key) {
551
assertThat(driver.manage().getCookieNamed(key)).as("Cookie with name " + key).isNull();
552
String documentCookie = getDocumentCookieOrNull();
553
if (documentCookie != null) {
554
assertThat(documentCookie).as("Cookie with name " + key).doesNotContain((key + "="));
555
}
556
}
557
558
private void assertCookieIsPresentWithName(final String key) {
559
assertThat(driver.manage().getCookieNamed(key)).as("Cookie with name " + key).isNotNull();
560
String documentCookie = getDocumentCookieOrNull();
561
if (documentCookie != null) {
562
assertThat(documentCookie)
563
.as(() -> "Cookie was not present with name " + key + ", got: " + documentCookie)
564
.contains(key + "=");
565
}
566
}
567
568
private void assertCookieHasValue(final String key, final String value) {
569
assertThat(driver.manage().getCookieNamed(key).getValue()).isEqualTo(value);
570
String documentCookie = getDocumentCookieOrNull();
571
if (documentCookie != null) {
572
assertThat(documentCookie)
573
.as("Cookie was present with name " + key)
574
.contains(key + "=" + value);
575
}
576
}
577
578
private String getDocumentCookieOrNull() {
579
if (!(driver instanceof JavascriptExecutor)) {
580
return null;
581
}
582
try {
583
return (String) ((JavascriptExecutor) driver).executeScript("return document.cookie");
584
} catch (UnsupportedOperationException e) {
585
return null;
586
}
587
}
588
589
private Date someTimeInTheFuture() {
590
return new Date(System.currentTimeMillis() + 100000);
591
}
592
593
private void openAnotherPage() {
594
driver.get(domainHelper.getUrlForFirstValidHostname("simpleTest.html"));
595
}
596
597
private void deleteAllCookiesOnServerSide() {
598
driver.get(cookiePage + "?action=deleteAll");
599
}
600
601
private void addCookieOnServerSide(Cookie cookie) {
602
StringBuilder url = new StringBuilder(cookiePage);
603
url.append("?action=add");
604
url.append("&name=").append(cookie.getName());
605
url.append("&value=").append(cookie.getValue());
606
if (cookie.getDomain() != null) {
607
url.append("&domain=").append(cookie.getDomain());
608
}
609
if (cookie.getPath() != null) {
610
url.append("&path=").append(cookie.getPath());
611
}
612
if (cookie.getExpiry() != null) {
613
url.append("&expiry=").append(cookie.getExpiry().getTime());
614
}
615
if (cookie.isSecure()) {
616
url.append("&secure=").append(cookie.isSecure());
617
}
618
if (cookie.isHttpOnly()) {
619
url.append("&httpOnly=").append(cookie.isHttpOnly());
620
}
621
driver.get(url.toString());
622
}
623
624
@Test
625
public void deleteAllCookies() {
626
assumeTrue(domainHelper.checkHasValidAlternateHostname());
627
628
Cookie cookie1 = new Cookie.Builder("fish1", "cod").domain(appServer.getHostName()).build();
629
Cookie cookie2 =
630
new Cookie.Builder("fish2", "tune").domain(appServer.getAlternateHostName()).build();
631
632
String url1 = domainHelper.getUrlForFirstValidHostname("/common");
633
String url2 = domainHelper.getUrlForSecondValidHostname("/common");
634
635
WebDriver.Options options = driver.manage();
636
637
options.addCookie(cookie1);
638
assertCookieIsPresentWithName(cookie1.getName());
639
640
driver.get(url2);
641
options.addCookie(cookie2);
642
assertCookieIsNotPresentWithName(cookie1.getName());
643
assertCookieIsPresentWithName(cookie2.getName());
644
645
driver.get(url1);
646
assertCookieIsPresentWithName(cookie1.getName());
647
assertCookieIsNotPresentWithName(cookie2.getName());
648
649
options.deleteAllCookies();
650
assertCookieIsNotPresentWithName(cookie1.getName());
651
652
driver.get(url2);
653
assertCookieIsPresentWithName(cookie2.getName());
654
}
655
}
656
657