Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test a "Remember Me" checkbox feature in Selenium

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.

like image 225
boopathi Avatar asked Mar 30 '15 05:03

boopathi


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.

like image 184
LittlePanda Avatar answered Oct 19 '22 23:10

LittlePanda


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.
like image 1
stiemannkj1 Avatar answered Oct 20 '22 00:10

stiemannkj1