c# - Selenium Webdriver 不尊重 cookie 或缓存图像

标签 c# selenium webdriver mstest

我正在使用 Selenium (2.24) 生成单元测试(用于 Visual Studio 单元测试框架)。在使用 FireFox 的 C# WebDriver 时,驱动程序启动的浏览器似乎没有通过 javascript 找到我的网站 cookie(我在网站中包含一个 javascript 文件,用于查找 cookie 并让我知道是否找到它们).此外,它不使用浏览器图像缓存,而是始终从服务器请求新图像。当我从“正常”(不是由 Selenium 启动)FireFox 运行我的网站时,不会发生这种行为。

奇怪的是,在我的单元测试中调用下面的代码确实返回了我的 cookie(我的 JavaScript 无法找到它)

driver.Manage().Cookies.GetCookieNamed("MyCookie");

如何配置驱动程序以尊重我的 cookie 并使用浏览器图像缓存?此功能是测试我的网站的关键。

最佳答案

默认情况下,每次启动 Firefox 时,FirefoxDriver 都会创建一个新的匿名配置文件。如果您希望它使用现有配置文件,您需要告诉它。

在 Java 中你可以这样做:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("MyProfile");
WebDriver driver = new FirefoxDriver(profile);

我假设 C# 中有类似的东西

关于c# - Selenium Webdriver 不尊重 cookie 或缓存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11353216/

相关文章:

c# - Azure Function 输入和输出绑定(bind)对象在每个请求中共享或创建

java - 如何在 Cucumber 测试套件中订购功能文件?

java - Selenium-Java Webdriver : I am getting error as Element should have been "select" but was "input" when i select the value form dropdown box

automated-tests - 如何使用 Selenium/WebDriver 可靠地等待 JavaScript 警报?

java - 使用我的 Selenium 代码在我的网站的表格中搜索文本

c# - 代码行数

c# - 一般比较两个对象

c# - 即使在启用 CORS 后,我也无法访问客户端上的 Response.Headers ("Content-Disposition")

python - 在选项卡之间切换并关闭窗口中的选项卡会出错

python - 美丽汤无法使用 Div 从网站找到数据