I'm trying to test the "Remember Me" functionality of a login form. I'm able to type in the user name and password, click the checkbox, click submit, and quit()
or close()
the browser. But when I reopen the browser with new ChromeDriver()
(or any other WebDriver
implementation), the test site does not remember anything because all cookies are deleted when the browser is closed and cannot be accessed when the browser is reopened.

- 4,418
- 3
- 25
- 45

- 87
- 2
- 10
-
In such a scenario why don't you use your own browser profile to work with. In webdriver any browser launch will be like a new profile so if you quit / close browser remember me won't work. So using your existing firefox/ chrome profile will let you check on the functionality of this. – Vivek Singh Mar 30 '15 at 07:08
-
cant get you clearly bro.. is there any option in selenium webdriver or not? can you explain more. @VivekSingh – boopathi Mar 30 '15 at 07:38
-
How about you go for existing profile for [chrome](http://stackoverflow.com/questions/14480717/load-chrome-profile-using-selenium-webdriver) and [firefox](http://stackoverflow.com/questions/6787095/how-to-stop-selenium-from-creating-temporary-firefox-profiles-using-web-driver) that already exist in your local. And then check upon for **remember me** functionality. – Vivek Singh Mar 30 '15 at 07:46
-
This is that functionality of the REMEMBER ME. if that checkbox is working as fine meaning .we can go for existing profile.@VivekSingh – boopathi Mar 30 '15 at 07:50
-
What have you done so far ? – Shamik Mar 31 '15 at 10:29
-
There are some related questions here: https://stackoverflow.com/questions/29176528/how-to-start-selenium-remotewebdriver-or-webdriver-without-clearing-cookies-or-c https://stackoverflow.com/questions/29455393/in-selenium-how-can-i-open-firefox-browser-but-with-the-existing-cookie – stiemannkj1 Oct 26 '17 at 15:15
-
Another related question here: https://sqa.stackexchange.com/questions/15594/selenium-how-to-access-the-same-session-in-a-new-window – stiemannkj1 Oct 26 '17 at 15:46
2 Answers
For Chrome (config):
You have to set the path to user-dir which will save all the login info after you login for the first time. The next time you login again, login info from the user-dir will be taken.
System.setProperty("webdriver.chrome.driver", "res/chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("user-data-dir=D:/temp/");
capabilities.setCapability("chrome.binary","res/chromedriver.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
WebDriver driver = new ChromeDriver(capabilities);
Login for the first time:
driver.get("https://gmail.com");
//Your login script typing username password, check 'keep me signed in' and so on
Close the driver (do NOT quit):
driver.close();
Re-initialize the driver and navigate to the site. You should not be asked for username and password again:
driver = new ChromeDriver(capabilities);
driver.get("http://gmail.com");
The above can be implemented for firefox using a firefox profile.

- 2,496
- 1
- 21
- 33
If the "Remember Me" feature is implemented using persistent cookies (I doubt that there is any other way to implement it), then you can actually test the feature in a cross-browser compatible way by programmatically manipulating the cookies. Cookies with an expiration date (or Expiry
in the Selenium API) are persistent cookies and are stored when the browser is closed and retrieved when the browser is re-opened. Non-persistent cookies are not stored when the browser is closed. With this information, we can simulate what should happen when the browser closes, by programmatically deleting all non-persistent cookies:
// Check the "Remember Me" checkbox and login here.
Set<Cookies> cookies = webDriver.manage().getCookies();
for (Cookie cookie : cookies) {
// Simulate a browser restart by removing all non-persistent cookies.
if (cookie.getExpiry() == null) {
webDriver.manage().deleteCookie(cookie);
}
}
// Reload the login page.
webDriver.get(currentLoginPageURL);
// Assert that some text like "You are logged in as..." appears on the page to
// indicate that you are still logged in.

- 4,418
- 3
- 25
- 45
-
1Thank you for your answer. Even though the code you provided is not complete, it helped me a lot for figuring out my use case. – George Smith Jul 29 '23 at 12:00