How can I test a “Remember Me” checkbox feature in Selenium

后端 未结 2 1982
有刺的猬
有刺的猬 2021-01-03 03:24

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

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 04:14

    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 = 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.
    

提交回复
热议问题