我正在使用Java的Selenium Web Driver API。每次我想要调试我的测试用例时,Firefox会在临时文件目录中创建一个临时配置文件,这会带来两个问题:
- 它肯定需要花费不必要的时间来创建配置文件,并且占用了不必要的空间。
- 我无法安装在下一次启动我的测试用例时可用的任何插件。
我该如何解决这个问题?
我正在使用Java的Selenium Web Driver API。每次我想要调试我的测试用例时,Firefox会在临时文件目录中创建一个临时配置文件,这会带来两个问题:
我该如何解决这个问题?
您可以控制Firefox驱动程序如何选择配置文件。将webdriver.firefox.profile
属性设置为要使用的配置文件的名称。大多数人认为这是一个不好的主意,因为您会继承先前使用该配置文件的所有Cookie、缓存内容等,但如果确实需要这样做,也是允许的。
例如:
System.setProperty("webdriver.firefox.profile", "MySeleniumProfile");
WebDriver driver = new FirefoxDriver(...);
更新 - 由Ranhiru提供
我如何处理Java中的此问题
FirefoxProfile profile = new FirefoxProfile(new File("D:\\Selenium Profile"));
WebDriver driver = new FirefoxDriver(profile);
然后我在Firefox中更改了设置,使其在退出时清除所有的cookie和缓存。查看这里了解如何操作。
确保你打电话
driver.quit();
而不是
driver.close();
close()不会删除临时文件
close()
与quit()
不同。 - user202729在我查看了这个问题上的文档之后,答案其实很简单。我发现了FirefoxProfile
类,并且构造函数需要传入Firefox配置文件的路径。
WebDriver driver = null;
FirefoxProfile profile = new FirefoxProfile(new File("C:\\Users\\Ranhiru\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\qp1nizdo.Selenium"));
driver = new FirefoxDriver(profile);
我通过运行带有-p标志的 "Firefox.exe" 创建了一个新的配置文件。
Firefox.exe -p
webdriver.firefox.profile 在启动 Firefox 时要使用的配置文件的名称
。但它没有提到它仍然会创建新的匿名配置文件,只是将指定的配置文件作为源使用。哦,好吧,感谢您的更新,现在我明白了。 - Vsevolod GolovanovFirefoxProfile
类中调用addExtension(File)
方法来加载所需插件的FirefoxWebDriver
。
示例:
try {
File firebug = new File("C:\\FFPlugins\\firebug-1.7.3.xpi");
File xpathChecker = new File("C:\\FFPlugins\\xpath_checker-0.4.4-fx.xpi");
profile.addExtension(firebug);
profile.setPreference("extensions.firebug.currentVersion", "1.7.3");
profile.addExtension(xpathChecker);
profile.setPreference("extensions.xpath_checker.currentVersion", "0.4.4");
} catch(IOException e) {
e.printStackTrace();
}
driver = new FirefoxDriver(profile);
firefox -profileManager
我需要添加任何所需的异常情况,因为由于Selenium每次打开干净的配置文件/实例,这些异常都会丢失。
然后,我直接使用以下内容创建我的Firefox浏览器:
private static String profilePath = "C:\\fitnesse\\Selenesse\\FFProfiles";
private static FirefoxProfile ffprofile = new FirefoxProfile(profilePath);
private static IWebDriver driver = new FirefoxDriver(ffprofile);
将以下代码添加到您的测试用例中:
DesiredCapabilities desiredCapabilities =
new DesiredCapabilities("firefox", "", Platform.ANY);
FirefoxProfile profile = new ProfilesIni().getProfile("webdriver1");
desiredCapabilities.setCapability("firefox_profile", profile);
WebDriver webdriver = new RemoteWebDriver(desiredCapabilities);
即使您明确指定了一个临时文件,也无法阻止Selenium创建临时文件。但是您可以在测试完成后清除它。
TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles()
在 MacOS 和 Ubuntu 上测试通过。
您可以直接告诉Selenium使用特定的配置文件。以下是一个示例: http://automationtricks.blogspot.com/2010/05/how-to-run-test-cases-in-specified.html
在程序开始之前,您可以指定不同的临时文件位置,以便您的程序不会因为“内存空间不足”而停止。
if(!new File("c:/temp_data").isDirectory()) //to check dir exist
FileUtils.forceMkdir(new File("c:/temp_data")); //create dir if not exists
TemporaryFilesystem.setTemporaryDirectory(new File("c:/temp_data")); //set new dir as temp file path for selenium
FirefoxProfile profile = new FirefoxProfile();