在Selenium IDE中清除Firefox缓存

izkcnapc  于 2023-06-23  发布在  其他
关注(0)|答案(5)|浏览(145)

我正在使用Selenium IDE测试一个Web应用程序。有时我的测试成功了,即使它们应该失败。原因是浏览器碰巧从缓存中加载页面的先前版本,而不是加载该页面的较新版本。换句话说,我可能会在没有意识到的情况下将一个bug引入到我的应用程序中,因为测试可能会在加载以前的工作版本而不是加载新的有bug的版本后通过。
我能想到的最佳解决方案是在运行测试之前删除浏览器缓存。我有一个Selenium脚本,在运行测试之前,我在其中运行设置selenium命令。有没有selenium命令可以清除Firefox缓存?或者,是否有其他方法可以防止在测试期间从缓存加载页面?

zvms9eto

zvms9eto1#

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

希望这对某人有帮助。

ygya80vv

ygya80vv2#

你可以在firefox配置文件中禁用该高速缓存。请参阅此链接了解更多详情。

4dc9hkyq

4dc9hkyq3#

对于那些用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);
hiz5n14c

hiz5n14c4#

免责声明:我以前从来没有这样做过(清除cookie对我来说已经足够了),但从我所看到的来看,这是Selenium当前版本中缺乏的功能,尽管从最近的更新日志来看,开发人员似乎正在推动制定一种标准的方式来做到这一点。在iedriverserver2.33中,它们具有以下changenote:
引入了在启动IE之前清理浏览器缓存的功能。这个版本引入了ie.ensureCleanSession功能,它将在启动IE之前清除浏览器缓存、历史记录和cookie。使用此功能时,请注意这会清除所有正在运行的Internet Explorer示例该高速缓存。尝试运行IE驱动程序的多个示例时使用此功能可能会导致意外行为。请注意,这也将导致性能下降时,启动浏览器,因为驱动程序将等待该高速缓存清除过程完成之前,实际启动IE
http://selenium.googlecode.com/git/cpp/iedriverserver/CHANGELOG
为此,您可以在驱动程序创建时使用ensureCleanSessionDesiredCapabilities Map中指定此参数。
http://code.google.com/p/selenium/wiki/DesiredCapabilities
因为你使用的是firefox,所以看起来你不太可能用原生的方式来做这件事。如果你还没有尝试过driver.manage().deleteAllCookies();,我会尝试一下,看看它是否能让你达到你需要的目的。

6gpjuf90

6gpjuf905#

适用于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;
            });
        }

相关问题