示例#1
0
  @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()));
  }
 /**
  * Removes cookie by name for a URL.
  *
  * @param url to remove cookie for
  * @param name of the cookie to remove
  */
 public void remove(String url, String name) {
   List<String> domains;
   try {
     domains = getDomainsFromUrl(new URL(url));
   } catch (MalformedURLException e) {
     throw new WebDriverException("Error while adding cookie. " + e);
   }
   for (String domain : domains) {
     List<Cookie> cookies = getCookies(domain);
     for (Cookie c : cookies) {
       if (c.getName().equals(name)) {
         cookies.remove(c);
         // To remove a cookie we set the date somewhere in the past.
         cookieManager.setCookie(
             domain,
             String.format(
                 "%s=; expires=%s",
                 name,
                 new SimpleDateFormat(COOKIE_DATE_FORMAT)
                     .format(System.currentTimeMillis() - 500)));
         break;
       }
     }
   }
   cookieManager.removeExpiredCookie();
 }
示例#3
0
 public Cookie getCookieNamed(String name) {
   Set<Cookie> allCookies = getCookies();
   for (Cookie cookie : allCookies) {
     if (cookie.getName().equals(name)) {
       return cookie;
     }
   }
   return null;
 }
示例#4
0
  /**
   * Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie
   * state
   *
   * @param seleniumCookieSet Set&lt;Cookie&gt;
   */
  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;
  }
 /**
  * Gets a cookie with specific name for a URL.
  *
  * @param url
  * @param name Cookie name to search
  * @return Cookie object (if found) or null
  */
 public Cookie getCookie(String url, String name) {
   List<Cookie> cookies;
   try {
     cookies = getCookies(getDomainsFromUrl(new URL(url)).get(0));
   } catch (MalformedURLException e) {
     throw new WebDriverException("Error while adding cookie. " + e);
   }
   // No cookies for given domain
   if (cookies == null || cookies.size() == 0) {
     return null;
   }
   for (Cookie cookie : cookies)
     if (cookie.getName().equals(name)) {
       return cookie;
     }
   return null; // No cookie with given name
 }
  /**
   * 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;
  }
 private String stringifyCookie(Cookie cookie) {
   return String.format("%s=%s", cookie.getName(), cookie.getValue());
 }
示例#8
0
 public void deleteCookie(Cookie cookie) {
   deleteCookieNamed(cookie.getName());
 }