如何启动Selenium RemoteWebDriver或WebDriver,而不清除Cookie或缓存? [英] How to start Selenium RemoteWebDriver or WebDriver without clearing cookies or cache?

查看:5627
本文介绍了如何启动Selenium RemoteWebDriver或WebDriver,而不清除Cookie或缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用案例:使用用户名登录,导航到第二要素认证页面以执行多项操作之一(即回答基于知识的问题),然后导航到最后一页以输入密码。关闭浏览器并尝试使用用户名重新登录。这一次,第二因素认证页面被绕过,因为应用程序识别cookie,并提示用户直接输入他们的密码。

Use case: Login with a username, navigated to a 2nd factor authentication page to do one of a number of things (i.e. answer a knowledge based question), and then navigated to a final page to enter a password. Close the browser and attempt to login again with the username. This time the 2nd factor authentication page is bypassed because the app recognizes the cookies and the user is prompted to enter their password directly.

问题:我使用Selenium RemoteWebDriver运行这些测试在单独的测试机上,当我关闭第一个浏览器并打开一个新的RemoteWebDriver实例时,它开始通过清除cookie和缓存,每次我尝试登录第二个因素认证页面。

Problem: I am using Selenium RemoteWebDriver to run these tests on a separate test machine and when I close the first browser and open a new instance of RemoteWebDriver it appears it starts by clearing cookies and cache and the 2nd factor authentication page comes up every time I attempt to login.

我需要:帮助找出如何创建一个新的RemoteWebDriver实例,而不会自动清除任何cookie或缓存,所以第二个因素的身份验证页将被绕过。我需要这个IE,Chrome,Firefox和Safari。

What I need: Help to figure out how to create a new instance of RemoteWebDriver without it automatically clearing any cookies or cache, so the 2nd factor authentication page will be bypassed. I need this for IE, Chrome, Firefox and Safari.

我没有任何代码明确地清除这一点,但我也没有任何尝试强制它不清楚(如果这是甚至存在。)我没有尝试任何其他,因为我不知道该尝试什么。

I don’t have any code that clears this explicitly, but I also don’t have anything that attempts to force it to not clear (if that’s even in existence.) I haven’t tried much of anything else because I have no idea what to try.

版本:
Selenium WebDriver:2.45.0.0,
Selenium Grid:2.45.0

Versions: Selenium WebDriver: 2.45.0.0, Selenium Grid: 2.45.0

谢谢!

推荐答案

保留Firefox上的Cookie;创建自定义配置文件(有关示例,请参见此处)并使用它每次启动新的RemoteWebDriver实例时。这样,Firefox将重用相同的配置文件与会话中的所有现有Cookie。然而,这不会保存在测试期间接收的cookie本身。有关解决方案,请参阅以下替代方法。

To preserve cookies on Firefox; create a custom profile (see here for an example) and use it each time when starting new RemoteWebDriver instance. That way Firefox will reuse same profile with all the existing cookies across sessions. This however doesn't save cookies received during the test itself. See the alternative approach below for a solution.

类似的方法适用于Chrome - 链接

A similar approach works for Chrome - link.

对于Internet Explorer而不是自定义配置文件,请确保CleanSession功能设置为false,以防止在会话开始时清除Cookie - 链接

For Internet Explorer instead of a custom profile, ensureCleanSession capability needs to be set to false to prevent cleaning cookies on session start - link.

替代解决方案
Cookie也可以从测试本身操作:

Alternative solution: Cookies can be also manipulated from within the test itself:


  1. 在测试结束时获取所有cookies:

  1. Get all the cookies when test ends:

ReadOnlyCollection< Cookie> cookies = driver.Manage()。Cookies.AllCookies;

在测试开始时取消序列化您的cookies,并添加它们通过WebDriver:

De-serialize your cookies on test start and add them through the WebDriver:

foreach(cookie中的var cookie)driver.Manage()。Cookies.AddCookie(cookie);

这篇关于如何启动Selenium RemoteWebDriver或WebDriver,而不清除Cookie或缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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