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
1,067 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
• 28,140 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 29, 2019 in Selenium by Anvi
• 14,150 points
3,253 views
0 votes
1 answer

How can I delete all the cookies present on a browser using Webdriver?

Hey jasmine, to delete all the cookies ...READ MORE

answered May 30, 2019 in Selenium by Rajeshwari
1,220 views
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 6, 2020 in Selenium by Sri
• 3,190 points
16,099 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,500 points
13,762 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,184 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,910 points
7,257 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 3, 2020 in Selenium by Sri
• 3,190 points
7,895 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
• 28,140 points
13,092 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP