|
package com.example; |
|
|
|
import java.util.Date; |
|
import java.util.GregorianCalendar; |
|
|
|
import org.openqa.selenium.By; |
|
import org.openqa.selenium.Cookie; |
|
import org.openqa.selenium.WebDriver; |
|
import org.openqa.selenium.support.ui.ExpectedConditions; |
|
import org.openqa.selenium.support.ui.WebDriverWait; |
|
import org.testng.annotations.AfterMethod; |
|
import org.testng.annotations.BeforeMethod; |
|
import org.testng.annotations.Test; |
|
|
|
public class CookieTest { |
|
|
|
WebDriver driver; |
|
WebDriverWait wait; |
|
|
|
@BeforeMethod |
|
private void setup() { |
|
// initialize driver here |
|
wait = new WebDriverWait(driver, 10); |
|
} |
|
|
|
@AfterMethod |
|
private void teardown() { |
|
if (driver != null) { |
|
driver.quit(); |
|
driver = null; |
|
} |
|
wait = null; |
|
} |
|
|
|
private Cookie cookieSetup(String cookieName) { |
|
driver.get("https://google.com"); |
|
wait.until(ExpectedConditions.presenceOfElementLocated(By.name("q"))); |
|
Date expiry = new GregorianCalendar(2019, 6, 6).getTime(); |
|
Cookie cookie = new Cookie(cookieName, "value", "", expiry); |
|
driver.manage().addCookie(cookie); |
|
return wait.until(d -> driver.manage().getCookieNamed(cookieName)); |
|
} |
|
|
|
@Test |
|
public void testSessionCookieShort() { |
|
String cookieName = System.getProperty("cookie", "session"); |
|
Cookie cookie = cookieSetup(cookieName); |
|
// Android fails here |
|
driver.manage().deleteCookie(cookie); |
|
wait.until(d -> driver.manage().getCookieNamed(cookieName) == null); |
|
} |
|
|
|
@Test |
|
public void testSessionCookieDeleteAllShort() { |
|
String cookieName = System.getProperty("cookie", "session"); |
|
cookieSetup(cookieName); |
|
// This doesn't fail on Android |
|
driver.manage().deleteAllCookies(); |
|
wait.until(d -> driver.manage().getCookieNamed(cookieName) == null); |
|
} |
|
|
|
} |