我想知道自动化测试是否有任何方法可以下载文件(在我的情况下为 excel 和 pdf)并使用 selenium Web 驱动程序保存在所需的位置。我尝试使用 Firefox 配置文件,但没有奏效。测试运行时,会弹出窗口询问是否打开或保存文件。因此,当我们单击一个按钮时,我不希望显示窗口弹出窗口,而是自动允许它在所需位置(本地和 Selenium 服务器上)下载我们正在使用 C# 编写测试。附件是弹出窗口。有人可以帮忙吗?
public static IWebDriver Build(SeleniumInstanceContext context)
{
IWebDriver instance;
var capabilities = new DesiredCapabilities();
var profile = CreateFirefoxProfile();
//Pass the Firefox profile to be used by RemoteWebDriver
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
switch (context.TestingBrowser.ToUpperInvariant())
{
case "CHROME":
instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
? new RemoteWebDriver(new Uri(context.SeleniumServerUrl), DesiredCapabilities.Chrome())
: new ChromeDriver();
break;
case "IE":
instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
? new RemoteWebDriver(new Uri(context.SeleniumServerUrl),
DesiredCapabilities.InternetExplorer())
: new InternetExplorerDriver();
break;
default:
instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
? new RemoteWebDriver(new Uri(context.SeleniumServerUrl), capabilities,
TimeSpan.FromMinutes(5))
: new FirefoxDriver(profile);
break;
}
return instance;
}
[![enter image description here][1]][1]public static FirefoxProfile CreateFirefoxProfile()
{
//Create FireFox Profile object
var profile = new FirefoxProfile();
//Set Location to store files after downloading.
const string path = "C:\\Users\\abc.xyz\\Downloads";
profile.SetPreference("browser.download.dir", path);
profile.SetPreference("browser.download.folderList", 2);
//Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/csv");
//profile.SetPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream");
//profile.SetPreference("browser.download.manager.showWhenStarting", false);
profile.SetPreference("pdfjs.disabled", true);
profile.SetPreference("browser.download.alertOnEXEOpen", false);
profile.SetPreference("browser.download.manager.focusWhenStarting", false);
profile.SetPreference("browser.helperApps.alwaysAsk.force", true);
profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
profile.SetPreference("browser.download.manager.closeWhenDone", false);
profile.SetPreference("browser.download.manager.showAlertOnComplete", false);
profile.SetPreference("browser.download.manager.useWindow", false);
return profile;
}