Wednesday, January 6, 2016

Selenium handling cookies



Retrieving cookie from website


 public class CookieExample {  
      static WebDriver driver;  
      static String webAddress = "http://xxxxxxxx:8080/xxxx/shared/config/config.jsp";  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           System.setProperty("webdriver.chrome.driver", "C:\\Java_Source_Code\\chromedriver_win32\\chromedriver.exe");  
           driver = new ChromeDriver();  
           Iterator<Cookie> cookies = login(driver, webAddress, null, "arsystem");  
           while (cookies.hasNext()) {  
 Cookie cookie = cookies.next();  
 System.out.println(cookie.getDomain());  
 System.out.println(cookie.getName());  
 System.out.println(cookie.getPath());  
 System.out.println(cookie.getExpiry());  
 System.out.println(cookie.getValue());  
 System.out.println(cookie.isSecure());  
           }  
           driver.close();  
      }  
      public static Iterator<Cookie> login(WebDriver driver, String webAddress, String loginID, String password) {  
           driver.get(webAddress);  
           driver.findElement(By.name("password")).clear();  
           driver.findElement(By.name("password")).sendKeys(password);  
           driver.findElement(By.className("button")).click();  
           new WebDriverWait(driver, 10)  
                     .until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("Logout"))));  
           driver.findElement(By.name("Logout")).click();  
           Set<Cookie> cookies = driver.manage().getCookies();  
           Iterator<Cookie> cookiesIterator = cookies.iterator();  
           return cookiesIterator;  
      }  
 }  

No comments:

Post a Comment