Can anyone suggest how can I restore cookies in new browser window using Webdriver?

0 votes
Can anyone suggest how can I restore cookies in new browser window using Webdriver?
Aug 14 in Selenium by Akshay
36 views

1 answer to this question.

0 votes

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();
    }
}
answered Aug 14 by Abha
• 24,980 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to open a browser window in full screen using Selenium WebDriver with C#

Simply Use this: driver.Manage().Window.Maximize(); READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,130 points
3,766 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
768 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,011 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
719 views
0 votes
1 answer
0 votes
1 answer

How can I read a CSV file in Selenium using Webdriver?

Hey Trisha, you can read a CSV ...READ MORE

answered Jul 17 in Selenium by Abha
• 24,980 points
52 views