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, 2019 in Selenium by Akshay
204 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, 2019 by Abha
• 27,930 points

Related Questions In Selenium

0 votes
1 answer

How can I refresh a browser window in different ways using Selenium Webdriver?

Hello Piyush, you can refresh a browser ...READ MORE

answered May 28, 2019 in Selenium by Anvi
• 14,090 points
1,029 views
0 votes
1 answer
0 votes
2 answers

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

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 5 in Selenium by Sri
• 2,970 points
9,441 views
0 votes
2 answers

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,510 points
3,517 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,522 views
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,890 points
2,589 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
2,847 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, 2019 in Selenium by Abha
• 27,930 points
4,831 views