13

I am using selenium webdriver with Java.

My test needs to verify when you save your login information and close the browser and reopen it then those credentials remain and are saved on a new session. So I want to close the current session and reopen it in order to verify if a cookie persists on the page however Selenium deletes all stored session data so the test case will always fail. Is there any way to prevent Selenium from deleting the stored session data after closing the browser for the specific test case?

When I do run it I get a no such session error.

16

Well, I believe you cannot prevent deletion of Session data once you close the browser. But, you can store the cookies of your first instance and copy it to new instances using a driver.manage().getCookies() method.

Before calling driver.close() method in your test, make sure to save the cookies using following piece of code :

Set<Cookie> allCookies = driver.manage().getCookies();

The above allCookies variable you can define global as you wish.

So for next instance onward,in the beginning of your test use the below piece of code :

driver = new FirefoxDriver();
for(Cookie cookie : allCookies)
{
    driver.manage().addCookie(cookie);
}

Now this will copy all cookies present earlier to this session, so after this proceed with further logic as per your requirements.

  • This obviously will not work for cookies with HttpOnly flag: stackoverflow.com/questions/15952262/… – dzieciou Mar 13 '16 at 12:44
  • 2
    getCookies() returns only cookies for the current domain, while addCookie() allows to add only a cookie with domain same as current URL's domain. So when getting or setting cookies it is important to go to the right URL first. – dzieciou Mar 13 '16 at 12:46
  • Thanks @dzieciou . I was initially trying to do this before any pages loaded at all (About: Blank) and it was causing an exception – gorbysbm May 14 at 21:31
1

Well the session of a web driver instance is over once that particular instance is closed. Thus you can't access the session of the previous instance in a new one. However you can still achieve the session by storing the session value in a variable and then adding the session in the new instance. Look at the below mentioned code for better understanding:

public void useStoredSessionInNewWindow() {
 // initiate web driver and go to an website
 _webDriver = new FirefoxDriver();
 _webDriver.navigate().to("www.abc.com");

 // add code to login in the website

 // store the current session
 Set<Cookie> cookiesInstance1 = _webDriver.manage().getCookies();
 System.out.println("Coockies = "+cookiesInstance1);

 // close the web driver instance
 _webDriver.close();

 // again initiate web driver and go to the same website. This will open the login page
 _webDriver = new FirefoxDriver();
 _webDriver.navigate().to("www.abc.com");

 // add the stored session in the bew web driver instance
 for(Cookie cookie : cookiesInstance1)
 {
  _webDriver.manage().addCookie(cookie);
 }

 // re-visit the page
 _webDriver.navigate().to("www.abc.com");

 // get the current session of new web driver instance
 Set<Cookie> cookiesInstance2 = _webDriver.manage().getCookies();
 System.out.println("Coockies = "+cookiesInstance2);

 // notice that session of previous web driver instanse is achieved
 Assert.assertEquals(cookiesInstance1, cookiesInstance2);

 }

Hope this helps :)

0

you could try using an implicitly specified profile path - then all data that your browser would have saved in a real-life situation when you close the browser will still be saved.

options.addArguments("--user-data-dir=" + PROFILE_PATH);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.