|
1 | | -package dev.selenium.interactions; |
| 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. |
2 | 17 |
|
3 | | -import dev.selenium.BaseChromeTest; |
4 | | -import org.junit.jupiter.api.Assertions; |
5 | 18 | import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.Assertions; |
6 | 20 | import org.openqa.selenium.Cookie; |
| 21 | +import org.openqa.selenium.WebDriver; |
| 22 | +import org.openqa.selenium.chrome.ChromeDriver; |
7 | 23 | import java.util.Set; |
8 | 24 |
|
9 | | -public class CookiesTest extends BaseChromeTest { |
10 | | - @Test |
11 | | - public void addCookie() { |
12 | | - try { |
13 | | - driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
14 | | - // Add cookie into current browser context |
15 | | - driver.manage().addCookie(new Cookie("key", "value")); |
16 | | - } finally { |
17 | | - driver.quit(); |
18 | | - } |
19 | | - } |
20 | | - @Test |
21 | | - public void getNamedCookie() { |
22 | | - try { |
23 | | - driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
24 | | - // Add cookie into current browser context |
25 | | - driver.manage().addCookie(new Cookie("foo", "bar")); |
26 | | - // Get cookie details with named cookie 'foo' |
27 | | - Cookie cookie = driver.manage().getCookieNamed("foo"); |
28 | | - Assertions.assertEquals(cookie.getValue(), "bar"); |
29 | | - } finally { |
30 | | - driver.quit(); |
31 | | - } |
32 | | - } |
| 25 | +public class CookiesTest { |
| 26 | + |
| 27 | + WebDriver driver = new ChromeDriver(); |
| 28 | + @Test |
| 29 | + public void addCookie() { |
| 30 | + driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
| 31 | + // Add cookie into current browser context |
| 32 | + driver.manage().addCookie(new Cookie("key", "value")); |
| 33 | + driver.quit(); |
| 34 | + } |
| 35 | + @Test |
| 36 | + public void getNamedCookie() { |
| 37 | + |
| 38 | + driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
| 39 | + // Add cookie into current browser context |
| 40 | + driver.manage().addCookie(new Cookie("foo", "bar")); |
| 41 | + // Get cookie details with named cookie 'foo' |
| 42 | + Cookie cookie = driver.manage().getCookieNamed("foo"); |
| 43 | + Assertions.assertEquals(cookie.getValue(), "bar"); |
| 44 | + |
| 45 | + driver.quit(); |
| 46 | + } |
| 47 | + |
33 | 48 |
|
34 | | - @Test |
35 | | - public void getAllCookies() { |
36 | | - try { |
37 | | - driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
38 | | - // Add cookies into current browser context |
39 | | - driver.manage().addCookie(new Cookie("test1", "cookie1")); |
40 | | - driver.manage().addCookie(new Cookie("test2", "cookie2")); |
41 | | - // Get cookies |
42 | | - Set<Cookie> cookies = driver.manage().getCookies(); |
43 | | - for (Cookie cookie : cookies) { |
44 | | - if (cookie.getName().equals("test1")) { |
45 | | - Assertions.assertEquals(cookie.getValue(), "cookie1"); |
46 | | - } |
| 49 | + @Test |
| 50 | + public void getAllCookies() { |
| 51 | + |
| 52 | + driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
| 53 | + // Add cookies into current browser context |
| 54 | + driver.manage().addCookie(new Cookie("test1", "cookie1")); |
| 55 | + driver.manage().addCookie(new Cookie("test2", "cookie2")); |
| 56 | + // Get cookies |
| 57 | + Set<Cookie> cookies = driver.manage().getCookies(); |
| 58 | + for (Cookie cookie : cookies) { |
| 59 | + if (cookie.getName().equals("test1")) { |
| 60 | + Assertions.assertEquals(cookie.getValue(), "cookie1"); |
| 61 | + } |
47 | 62 |
|
48 | | - if (cookie.getName().equals("test2")) { |
49 | | - Assertions.assertEquals(cookie.getValue(), "cookie2"); |
50 | | - } |
51 | | - } |
52 | | - } finally { |
53 | | - driver.quit(); |
54 | | - } |
55 | | - } |
| 63 | + if (cookie.getName().equals("test2")) { |
| 64 | + Assertions.assertEquals(cookie.getValue(), "cookie2"); |
| 65 | + } |
| 66 | + } |
| 67 | + driver.quit(); |
| 68 | + } |
| 69 | + |
56 | 70 |
|
57 | | - @Test |
58 | | - public void deleteCookieNamed() { |
59 | | - try { |
60 | | - driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
61 | | - driver.manage().addCookie(new Cookie("test1", "cookie1")); |
62 | | - // delete cookie named |
63 | | - driver.manage().deleteCookieNamed("test1"); |
64 | | - } finally { |
65 | | - driver.quit(); |
66 | | - } |
67 | | - } |
| 71 | + @Test |
| 72 | + public void deleteCookieNamed() { |
| 73 | + |
| 74 | + driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
| 75 | + driver.manage().addCookie(new Cookie("test1", "cookie1")); |
| 76 | + // delete cookie named |
| 77 | + driver.manage().deleteCookieNamed("test1"); |
| 78 | + driver.quit(); |
| 79 | + } |
68 | 80 |
|
69 | | - @Test |
70 | | - public void deleteCookieObject() { |
71 | | - try { |
72 | | - driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
73 | | - Cookie cookie = new Cookie("test2", "cookie2"); |
74 | | - driver.manage().addCookie(cookie); |
75 | | - /* |
76 | | - Selenium Java bindings also provides a way to delete |
77 | | - cookie by passing cookie object of current browsing context |
78 | | - */ |
79 | | - driver.manage().deleteCookie(cookie); |
80 | | - } finally { |
81 | | - driver.quit(); |
82 | | - } |
83 | | - } |
| 81 | + @Test |
| 82 | + public void deleteCookieObject() { |
| 83 | + |
| 84 | + driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
| 85 | + Cookie cookie = new Cookie("test2", "cookie2"); |
| 86 | + driver.manage().addCookie(cookie); |
| 87 | + /* |
| 88 | + Selenium Java bindings also provides a way to delete |
| 89 | + cookie by passing cookie object of current browsing context |
| 90 | + */ |
| 91 | + driver.manage().deleteCookie(cookie); |
| 92 | + |
| 93 | + driver.quit(); |
| 94 | + } |
| 95 | + |
84 | 96 |
|
85 | | - @Test |
86 | | - public void deleteAllCookies() { |
87 | | - try { |
88 | | - driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
89 | | - // Add cookies into current browser context |
90 | | - driver.manage().addCookie(new Cookie("test1", "cookie1")); |
91 | | - driver.manage().addCookie(new Cookie("test2", "cookie2")); |
92 | | - // Delete All cookies |
93 | | - driver.manage().deleteAllCookies(); |
94 | | - } finally { |
95 | | - driver.quit(); |
96 | | - } |
97 | | - } |
| 97 | + @Test |
| 98 | + public void deleteAllCookies() { |
| 99 | + |
| 100 | + driver.get("https://www.selenium.dev/selenium/web/blank.html"); |
| 101 | + // Add cookies into current browser context |
| 102 | + driver.manage().addCookie(new Cookie("test1", "cookie1")); |
| 103 | + driver.manage().addCookie(new Cookie("test2", "cookie2")); |
| 104 | + // Delete All cookies |
| 105 | + driver.manage().deleteAllCookies(); |
| 106 | + |
| 107 | + driver.quit(); |
| 108 | + } |
98 | 109 | } |
0 commit comments