. Code My Day

Thursday, August 29, 2013

Cookie operation in selenium webdriver

Adding our own cookie
  method 1
         Cookie one = new Cookie.Builder("key1", "value").build();
         Cookie two = new Cookie.Builder("key2", "value").build();
         driver.manage().addCookie(one);
         driver.manage().addCookie(two);

   method 2

 ((JavascriptExecutor) driver).executeScript("document.cookie = arguments[0] + '=' +                arguments[1];","key", "setting");

 ((JavascriptExecutor) driver).executeScript("document.cookie = arguments[0] + '=set';", key);

  System.out.println(driver.manage().getCookieNamed("key").getValue())

  will give you the newly created cookie.


Checking if created cookie is present or not
          System.out.println(cookies.contains(one));

Getting total cookie count

         Set<Cookie> cookies = driver.manage().getCookies();
         int countBefore = cookies.size();


No comments:

Post a Comment