【问题标题】:How can I test a "Remember Me" checkbox feature in Selenium如何在 Selenium 中测试“记住我”复选框功能
【发布时间】:2015-06-03 00:57:02
【问题描述】:

我正在尝试测试登录表单的“记住我”功能。我可以输入用户名和密码,单击复选框,单击提交,然后quit()close() 浏览器。但是当我用new ChromeDriver()(或任何其他WebDriver实现)重新打开浏览器时,测试站点不记得任何内容,因为所有cookie在浏览器关闭时都会被删除,并且在浏览器重新打开时无法访问。

【问题讨论】:

  • 在这种情况下,您为什么不使用自己的浏览器配置文件来使用。在 webdriver 中,任何浏览器启动都会像一个新的配置文件,所以如果你退出/关闭浏览器,记住我将无法工作。因此,使用您现有的 firefox/chrome 配置文件可以让您检查其功能。
  • 不能让你清楚兄弟.. selenium webdriver 中是否有任何选项?你能解释更多吗? @VivekSingh
  • 您如何为chromefirefox 寻找本地已经存在的现有配置文件。然后检查记住我功能。
  • 这就是记住我的功能。如果该复选框工作正常。我们可以选择现有的配置文件。@VivekSingh
  • 到目前为止你做了什么?

标签: java selenium cookies selenium-webdriver testng


【解决方案1】:

对于 Chrome(配置):

您必须设置用户目录的路径,这将在您第一次登录后保存所有登录信息。下次再次登录时,将获取用户目录中的登录信息。

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);

第一次登录:

driver.get("https://gmail.com");
//Your login script typing username password, check 'keep me signed in' and so on

关闭驱动(不要退出):

driver.close();

重新初始化驱动程序并导航到该站点。不应再次要求您输入用户名和密码:

driver = new ChromeDriver(capabilities);
driver.get("http://gmail.com");

以上可以使用 firefox 配置文件为 firefox 实现。

【讨论】:

    【解决方案2】:

    如果“记住我”功能是使用持久性 cookie 实现的(我怀疑是否有任何其他方式来实现它),那么您实际上可以通过编程操作 cookie 以跨浏览器兼容的方式测试该功能。具有到期日期(或 Expiry in the Selenium API)的 Cookie 为 persistent cookies and are stored when the browser is closed and retrieved when the browser is re-opened。关闭浏览器时不会存储非持久性 cookie。有了这些信息,我们可以通过编程删除所有非持久性 cookie 来模拟浏览器关闭时应该发生的情况:

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

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 1970-01-01
      • 2015-03-26
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多