Skip to content

Commit 7b9949c

Browse files
authored
Merge pull request eugenp#8441 from chris9408/feature/selenium-cookies
[BAEL-2948] Using cookies with Selenium WebDriver
2 parents 0c637d8 + d00f690 commit 7b9949c

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

3.4 MB
Binary file not shown.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package test.java.com.baeldung.selenium.junit;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.openqa.selenium.Capabilities;
7+
import org.openqa.selenium.Cookie;
8+
import org.openqa.selenium.WebDriver;
9+
import org.openqa.selenium.firefox.FirefoxDriver;
10+
import org.openqa.selenium.remote.DesiredCapabilities;
11+
12+
import java.util.Set;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.Matchers.*;
17+
18+
public class SeleniumCookiesJUnitLiveTest {
19+
20+
private WebDriver driver;
21+
private String navUrl;
22+
23+
@Before
24+
public void setUp() {
25+
Capabilities capabilities = DesiredCapabilities.firefox();
26+
driver = new FirefoxDriver(capabilities);
27+
navUrl = "https://baeldung.com";
28+
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
29+
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
30+
}
31+
32+
@After
33+
public void teardown() {
34+
driver.quit();
35+
}
36+
37+
@Test
38+
public void whenNavigate_thenCookiesExist() {
39+
driver.navigate().to(navUrl);
40+
Set<Cookie> cookies = driver.manage().getCookies();
41+
42+
assertThat(cookies, is(not(empty())));
43+
}
44+
45+
@Test
46+
public void whenNavigate_thenLpCookieExists() {
47+
driver.navigate().to(navUrl);
48+
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
49+
50+
assertThat(lpCookie, is(not(nullValue())));
51+
}
52+
53+
@Test
54+
public void whenNavigate_thenLpCookieIsHasCorrectValue() {
55+
driver.navigate().to(navUrl);
56+
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
57+
58+
assertThat(lpCookie.getValue(), containsString("www.baeldung.com"));
59+
}
60+
61+
@Test
62+
public void whenNavigate_thenLpCookieHasCorrectProps() {
63+
driver.navigate().to(navUrl);
64+
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
65+
66+
assertThat(lpCookie.getDomain(), equalTo(".baeldung.com"));
67+
assertThat(lpCookie.getPath(), equalTo("/"));
68+
assertThat(lpCookie.getExpiry(), is(not(nullValue())));
69+
assertThat(lpCookie.isSecure(), equalTo(false));
70+
assertThat(lpCookie.isHttpOnly(), equalTo(false));
71+
}
72+
73+
@Test
74+
public void whenAddingCookie_thenItIsPresent() {
75+
driver.navigate().to(navUrl);
76+
Cookie cookie = new Cookie("foo", "bar");
77+
driver.manage().addCookie(cookie);
78+
Cookie driverCookie = driver.manage().getCookieNamed("foo");
79+
80+
assertThat(driverCookie.getValue(), equalTo("bar"));
81+
}
82+
83+
@Test
84+
public void whenDeletingCookie_thenItIsAbsent() {
85+
driver.navigate().to(navUrl);
86+
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
87+
88+
assertThat(lpCookie, is(not(nullValue())));
89+
90+
driver.manage().deleteCookie(lpCookie);
91+
Cookie deletedCookie = driver.manage().getCookieNamed("lp_120073");
92+
93+
assertThat(deletedCookie, is(nullValue()));
94+
}
95+
96+
@Test
97+
public void whenOverridingCookie_thenItIsUpdated() {
98+
driver.navigate().to(navUrl);
99+
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
100+
driver.manage().deleteCookie(lpCookie);
101+
102+
Cookie newLpCookie = new Cookie("lp_120073", "foo");
103+
driver.manage().addCookie(newLpCookie);
104+
105+
Cookie overriddenCookie = driver.manage().getCookieNamed("lp_120073");
106+
107+
assertThat(overriddenCookie.getValue(), equalTo("foo"));
108+
}
109+
110+
}

0 commit comments

Comments
 (0)