如何测试“记住我”? Selenium中的复选框功能 [英] How can I test a "Remember Me" checkbox feature in Selenium

查看:172
本文介绍了如何测试“记住我”? Selenium中的复选框功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

对于Chrome(配置):

您必须设置user-dir的路径,这将在您首次登录后保存所有登录信息。下次再次登录时,将使用user-dir的登录信息。

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

第一次登录:

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

以上内容可以使用firefox个人资料为firefox实现。

The above can be implemented for firefox using a firefox profile.

这篇关于如何测试“记住我”? Selenium中的复选框功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆