Selenium - 存储会话数据 [英] Selenium - Stored Session Data

查看:591
本文介绍了Selenium - 存储会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个互联网上搜索,但我找到的答案都没有明确的解决方案。

I have search throughout the internet but none of the answers I found have a clear solution.

我正在使用带有Java的selenium webdriver。

I am using selenium webdriver with Java.

我的测试需要验证您何时保存登录信息并关闭浏览器并重新打开它,然后这些凭据将保留并保存在新会话中。因此,我想关闭当前会话并重新打开它以验证cookie是否仍然存在于页面上,但Selenium会删除所有存储的会话数据,因此测试用例将始终失败。有没有办法阻止Selenium在关闭特定测试用例的浏览器后删除存储的会话数据?

My test needs to verify when you save your login information and close the browser and reopen it then those credentials remain and are saved on a new session. So I want to close the current session and reopen it in order to verify if a cookie persists on the page however Selenium deletes all stored session data so the test case will always fail. Is there any way to prevent Selenium from deleting the stored session data after closing the browser for the specific test case?

当我运行它时,我得到一个没有这样的会话错误。

When I do run it I get a no such session error.

推荐答案

此解决方案适用于Chrome。

This solution works for chrome .

启动时chrome(我认为同样适用于其他浏览器)selenium创建一个临时配置文件,当你关闭它时它会删除它。因此,当您重新启动浏览器时,新配置文件将不具有会话信息。您可以做的是使用个人资料。

When you launch chrome (I think same applies for other browsers ) selenium creates a temporary profile and when you close it it deletes it. So when you relaunch the browser the new profile will not have the session information. What you can do is use profiles.

 DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
 ChromeOptions options = new ChromeOptions();

 options.addArguments("user-data-dir=C:\\Users\\"+System.getenv("USERNAME")+"\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1");
            desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
 driver = new ChromeDriver(desiredCapabilities);

这种方式如果您创建驱动程序,每次使用相同的配置文件启动chrome并且如果你不这样做

This way if you create your driver, every time chrome will be launched with the same profile and if you dont do

driver.manage().deleteAllCookies();

您的会话信息将保留在新的Chrome驱动程序会话中。如果您不希望它持续存在,您可以使用上面的命令清理cookie或只是简单注销。这应该可以解决您的问题。其他浏览器也应该有类似的东西。

Your session information will persist in the new session of chrome driver. If you dont want it to persist you can clean the cookies using the above command or just simple log out. This should solve your problem. Similar things should also be possible in other browsers.

要知道当前使用的是哪个配置文件,请输入 chrome:// version / 在您的地址栏中,它包含当前使用的配置文件的信息。要了解更多信息,请参阅

To know which profile is currently used type chrome://version/ in your address bar it has the information of the current profile being usesd. To know more about see this

这篇关于Selenium - 存储会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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