/**
  * 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();
 }
Example #2
0
 /**
  * Helper method to extract cookie value of Alfresco-CSRFToken
  *
  * @return String token value
  */
 private String getCookieValue() {
   Cookie cookie = drone.getCookie("Alfresco-CSRFToken");
   if (cookie != null) {
     return cookie.getValue();
   }
   return "";
 }
Example #3
0
 public Cookie getCookieNamed(String name) {
   Set<Cookie> allCookies = getCookies();
   for (Cookie cookie : allCookies) {
     if (cookie.getName().equals(name)) {
       return cookie;
     }
   }
   return null;
 }
  public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com");

    // Get All Cookies
    Set<Cookie> cookies = driver.manage().getCookies();
    for (Cookie cookie : cookies) {
      System.out.println(cookie.toString());
    }

    // GetNamedCookies
    Cookie cookieNID = driver.manage().getCookieNamed("NID");
    System.out.println(cookieNID.toString());

    // Add a Cookie
    Cookie cookieNID1 =
        new Cookie(
            "NID1",
            "72=w0IXrx18ZiAMw_uEIq8mHyOCQgrssPqQyBQLYCCRtMQRNA3fFARSAU8STjqutcpZYGgVGRhssuvS4-9clESe2uiWo9VIiwQY3JDdUVIzlV4LOLa0JWQ0YEfqjzP9oYxmXxUvuA; expires=Sat, 26 Mar 2016 07:47:39 IST; path=/; domain=.google.co.in");
    driver.manage().addCookie(cookieNID1);

    driver.manage().deleteCookie(cookieNID1);
    Cookie cNID1 = driver.manage().getCookieNamed("NID1");
    System.out.println(cNID1.toString());

    driver.manage().deleteAllCookies();
    Cookie cookie1 = driver.manage().getCookieNamed("NID");
    System.out.println(cookie1.toString());
  }
 /**
  * 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
 }
 /**
  * Adds a cookie to a URL domain.
  *
  * @param url to add the cookie to
  * @param cookie Cookie to be added
  */
 public void addCookie(String url, Cookie cookie) {
   URL urlObj;
   try {
     urlObj = new URL(url);
   } catch (MalformedURLException e) {
     throw new WebDriverException("Error while adding cookie. ", e);
   }
   String domain = "http://" + urlObj.getHost() + cookie.getPath();
   if (!domain.endsWith("/")) {
     domain = domain + "/";
   }
   cookieManager.setCookie(domain, stringifyCookie(cookie));
 }
Example #7
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;
  }
 protected Cookie verifyLoggedIn(AbstractPage targetPage, Cookie sessionCookieForVerification) {
   // verify on realm path
   masterRealmPage.navigateTo();
   Cookie sessionCookieOnRealmPath = driver.manage().getCookieNamed(KEYCLOAK_SESSION_COOKIE);
   assertNotNull(sessionCookieOnRealmPath);
   assertEquals(sessionCookieOnRealmPath.getValue(), sessionCookieForVerification.getValue());
   // verify on target page
   targetPage.navigateTo();
   assertCurrentUrlStartsWith(targetPage);
   Cookie sessionCookie = driver.manage().getCookieNamed(KEYCLOAK_SESSION_COOKIE);
   assertNotNull(sessionCookie);
   assertEquals(sessionCookie.getValue(), sessionCookieForVerification.getValue());
   return sessionCookie;
 }
  /**
   * 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;
  }
Example #10
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()));
  }
 private String stringifyCookie(Cookie cookie) {
   return String.format("%s=%s", cookie.getName(), cookie.getValue());
 }
Example #12
0
 public void deleteCookie(Cookie cookie) {
   deleteCookieNamed(cookie.getName());
 }