@Test public void shouldRetainCookieInfo() { server.setGetHandler(EMPTY_CALLBACK); goToPage(); // Added cookie (in a sub-path - allowed) Cookie addedCookie = new Cookie.Builder("fish", "cod") .expiresOn(new Date(System.currentTimeMillis() + 100 * 1000)) // < now + 100sec .path("/404") .domain("localhost") .build(); driver.manage().addCookie(addedCookie); // Search cookie on the root-path and fail to find it Cookie retrieved = driver.manage().getCookieNamed("fish"); assertNull(retrieved); // Go to the "/404" sub-path (to find the cookie) goToPage("404"); retrieved = driver.manage().getCookieNamed("fish"); assertNotNull(retrieved); // Check that it all matches assertEquals(addedCookie.getName(), retrieved.getName()); assertEquals(addedCookie.getValue(), retrieved.getValue()); assertEquals(addedCookie.getExpiry(), retrieved.getExpiry()); assertEquals(addedCookie.isSecure(), retrieved.isSecure()); assertEquals(addedCookie.getPath(), retrieved.getPath()); assertTrue(retrieved.getDomain().contains(addedCookie.getDomain())); }
/** * Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie * state * * @param seleniumCookieSet Set<Cookie> */ private BasicCookieStore mimicCookieState(Set<Cookie> seleniumCookieSet) { BasicCookieStore copyOfWebDriverCookieStore = new BasicCookieStore(); for (Cookie seleniumCookie : seleniumCookieSet) { BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue()); duplicateCookie.setDomain(seleniumCookie.getDomain()); duplicateCookie.setSecure(seleniumCookie.isSecure()); duplicateCookie.setExpiryDate(seleniumCookie.getExpiry()); duplicateCookie.setPath(seleniumCookie.getPath()); copyOfWebDriverCookieStore.addCookie(duplicateCookie); } return copyOfWebDriverCookieStore; }
/** * Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie * state * * @param seleniumCookieSet * @return */ private BasicCookieStore mimicCookieState(Set seleniumCookieSet) { BasicCookieStore mimicWebDriverCookieStore = new BasicCookieStore(); for (Iterator iterator = seleniumCookieSet.iterator(); iterator.hasNext(); ) { Cookie seleniumCookie = (Cookie) iterator.next(); BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue()); duplicateCookie.setDomain(seleniumCookie.getDomain()); duplicateCookie.setSecure(seleniumCookie.isSecure()); duplicateCookie.setExpiryDate(seleniumCookie.getExpiry()); duplicateCookie.setPath(seleniumCookie.getPath()); mimicWebDriverCookieStore.addCookie(duplicateCookie); } /* for (Cookie seleniumCookie : seleniumCookieSet) { BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue()); duplicateCookie.setDomain(seleniumCookie.getDomain()); duplicateCookie.setSecure(seleniumCookie.isSecure()); duplicateCookie.setExpiryDate(seleniumCookie.getExpiry()); duplicateCookie.setPath(seleniumCookie.getPath()); mimicWebDriverCookieStore.addCookie(duplicateCookie); }*/ return mimicWebDriverCookieStore; }