在Selenium IDE中清除Firefox缓存

14
我正在使用Selenium IDE测试一个Web应用程序。 有时我的测试即使应该失败也会成功。原因是浏览器恰好从缓存中加载页面的先前版本而不是加载该页面的新版本。换句话说,我可能会在不知情的情况下向我的应用程序引入错误,因为测试在加载以前的工作版本而不是加载新的有问题的版本后可能通过。
我能想到的最好解决方案是在运行测试之前删除浏览器缓存。我有一个Selenium脚本,在其中运行设置Selenium命令以在运行测试之前进行配置。是否有Selenium命令可以清除Firefox缓存?或者,在测试期间有没有防止加载缓存页面的其他方法?
5个回答

14

在Python中,这应该禁用Firefox缓存:

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.cache.disk.enable", False)
profile.set_preference("browser.cache.memory.enable", False)
profile.set_preference("browser.cache.offline.enable", False)
profile.set_preference("network.http.use-cache", False)
driver = webdriver.Firefox(profile)

希望这能帮助到某人。


2
您可以在Firefox配置文件中禁用缓存。 更多细节请参见此链接

3
如果你懒得点击链接的话,可以这样创建一个Firefox配置文件并设置浏览器缓存的偏好设置:user_pref("browser.cache.disk.enable", false);user_pref("browser.cache.memory.enable", false);user_pref("browser.cache.offline.enable", false);user_pref("network.http.use-cache", false);对我来说很有效。 - John Smith
我有什么遗漏吗?我通过谷歌找到了如何创建新配置文件的方法,但是在哪里添加这些设置呢?我该如何确保Selenium使用此配置文件而不是我的常规配置文件来打开Firefox? - GMA
仅禁用Cookie并不能完全达到目的。因为有一些页面,例如结账页面,需要您使用Cookie。是否有一种方法可以像使用Selenium手动删除历史记录一样进行删除? - decoder

1
如果你正在使用Java编程,这就是我解决问题的方法:
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.cache.disk.enable", false);
    profile.setPreference("browser.cache.memory.enable", false);
    profile.setPreference("browser.cache.offline.enable", false);
    profile.setPreference("network.http.use-cache", false);
    FirefoxOptions options = new FirefoxOptions().setProfile(profile);
    driver = new FirefoxDriver(options);

0
免责声明:我以前从未做过这件事(清除Cookie对我来说总是足够的),但据我所见,这是当前Selenium版本中缺少的功能,尽管从最近的更改日志中可以看出,开发人员正在努力制定一种标准方法来解决这个问题。在iedriverserver2.33版本中,他们有以下变更说明:

引入了在启动IE之前清除浏览器缓存的功能。此版本引入了ie.ensureCleanSession功能,它将在启动IE之前清除浏览器缓存、历史记录和Cookie。使用此功能时,请注意,这会清除所有运行中的Internet Explorer的缓存。在尝试运行多个IE驱动程序实例时使用此功能可能会导致意外行为。请注意,这也会导致启动浏览器时性能下降,因为驱动程序将在实际启动IE之前等待缓存清除过程完成。

http://selenium.googlecode.com/git/cpp/iedriverserver/CHANGELOG

要做到这一点,您需要在驱动程序创建时在DesiredCapabilities Map中使用ensureCleanSession指定。

http://code.google.com/p/selenium/wiki/DesiredCapabilities

由于您正在使用Firefox,似乎没有本地方法可以实现此操作。如果您尚未尝试过driver.manage().deleteAllCookies();,建议您尝试一下,看看是否能够达到您需要的效果。


0

针对 C# 和 Geckodriver v0.31.0

public Task<WebDriver> newInstance()
        {
            return Task.Run(() =>
            {
                foreach (var process in Process.GetProcessesByName("geckodriver"))
                {                   
                    process.Kill();
                }

                FirefoxProfileManager profilemanager = new FirefoxProfileManager();

                System.Collections.ObjectModel.ReadOnlyCollection<String> profilesList = profilemanager.ExistingProfiles;

                foreach (String profileFound in profilesList)
                {
                    Console.WriteLine(profileFound);
                }

                FirefoxOptions options = new FirefoxOptions();

                FirefoxProfile profile = profilemanager.GetProfile("default");

                //profile = webdriver.FirefoxProfile()

                profile.SetPreference("browser.cache.disk.enable", false);
                profile.SetPreference("browser.cache.memory.enable", false);
                profile.SetPreference("browser.cache.offline.enable", false);
                profile.SetPreference("network.http.use-cache", false);

                WebDriver driver = new FirefoxDriver(options);

                return driver;
            });
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接