[BAEL-2948] Using cookies with Selenium WebDriver

This commit is contained in:
chris9408 2019-12-24 21:20:23 +02:00
parent b8e73d5b8c
commit 030c763425
1 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,107 @@
package test.java.com.baeldung.selenium.junit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class SeleniumCookiesJUnitLiveTest {
private WebDriver driver;
private String navUrl;
@Before
public void setUp() {
Capabilities capabilities = DesiredCapabilities.firefox();
driver = new FirefoxDriver(capabilities);
navUrl = "https://baeldung.com";
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@After
public void teardown() {
driver.close();
}
@Test
public void givenHomePage_whenNavigate_thenCookiesExist() {
driver.navigate().to(navUrl);
Set<Cookie> cookies = driver.manage().getCookies();
assertThat(cookies, is(not(empty())));
}
@Test
public void givenHomePage_whenNavigate_thenLpCookieExists() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(lpCookie, is(not(nullValue())));
}
@Test
public void givenHomePage_whenNavigate_thenLpCookieIsHasCorrectValue() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(lpCookie.getValue(), containsString("www.baeldung.com"));
}
@Test
public void givenHomePage_whenNavigate_thenLpCookieHasCorrectProps() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(lpCookie.getDomain(), equalTo(".baeldung.com"));
assertThat(lpCookie.getPath(), equalTo("/"));
assertThat(lpCookie.getExpiry(), is(not(nullValue())));
assertThat(lpCookie.isSecure(), equalTo(false));
assertThat(lpCookie.isHttpOnly(), equalTo(false));
}
@Test
public void givenHomePage_whenAddingCookie_thenItIsPresent() {
driver.navigate().to(navUrl);
Cookie cookie = new Cookie("foo", "bar");
driver.manage().addCookie(cookie);
Cookie driverCookie = driver.manage().getCookieNamed("foo");
assertThat(driverCookie.getValue(), equalTo("bar"));
}
@Test
public void givenHomePage_whenDeletingCookie_thenItIsAbsent() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(lpCookie, is(not(nullValue())));
driver.manage().deleteCookie(lpCookie);
Cookie deletedCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(deletedCookie, is(nullValue()));
}
@Test
public void givenHomePage_whenOverridingCookie_thenItIsUpdated() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
driver.manage().deleteCookie(lpCookie);
lpCookie = new Cookie("lp_120073", "foo");
driver.manage().addCookie(lpCookie);
assertThat(lpCookie.getValue(), equalTo("foo"));
}
}