Hey Akshay, following code snippet would help you in understanding how to restore cookies in new browser window:
public class CookieTest {
WebDriver driver;
@Test
public void login_state_should_be_restored() {
driver = new FirefoxDriver();
driver.get("http://www.facebook.com");
driver.findElement(By.id("email")).sendKeys("test123");
driver.findElement(By.id("pass")).sendKeys("pass123");
driver.findElement(By.id("u_0_2")).click();
Assert.assertTrue(
driver.findElement(By.id("_2md")).isDisplayed());
//Before closing the browser, read the cookies
Set allCookies = driver.manage().getCookies();
driver.close();
//open a new browser window
driver = new FirefoxDriver();
//restore all cookies from previous session
for(Cookie cookie : allCookies) {
driver.manage().addCookie(cookie);
}
driver.get("http://www.facebook.com/");
//Login page should not be disaplyed
Assert.assertTrue(
driver.findElement(By.id("_2md")).isDisplayed());
driver.close();
}
}