如何检查Selenium WebDriver下载的文件? [英] How to check downloaded files Selenium WebDriver?
本文介绍了如何检查Selenium WebDriver下载的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用C#在Selenium Webdriver中编写了一个自动化测试,其中一个步骤要求从服务器下载XLSX文件.如何验证文件是否已成功下载并获得他的名字?
I wrote an automation test in Selenium webdriver using C#, and one of steps requires to download a XLSX file from server. How to validate if file has downloaded successfully and get his name?
致谢
推荐答案
我找到了带有以下源代码的解决方案:
I found the solution with following source code:
string currentPage = Browser.Current.Url;
string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string downloadPath = Path.Combine(userPath, "Downloads");
DirectoryInfo dirInfo = new DirectoryInfo(downloadPath);
if (!dirInfo.Exists)
{
dirInfo.Create();
}
int directoryFiles = dirInfo.EnumerateFiles().Count();
string elementXpath = "//div[@id='myDiv']/div/div/div[@class='atalhos']/a[1]";
bool isFirefox = (Browser.Current as FirefoxDriver) != null;
bool isChrome = (Browser.Current as ChromeDriver) != null;
IWebDriver browserDriver = null;
if (isChrome)
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", downloadPath);
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
browserDriver = new ChromeDriver(chromeOptions);
}
else if (isFirefox)
{
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
browserDriver = new FirefoxDriver(profile);
}
browserDriver.Navigate().GoToUrl(currentPage);
WebDriverWait wait = new WebDriverWait(browserDriver, TimeSpan.FromSeconds(15));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(elementXpath)));
IWebElement elemento = browserDriver.FindElement(By.XPath(elementXpath));
elemento.Click();
Thread.Sleep(7000);
dirInfo = new DirectoryInfo(downloadPath);
int currentFiles = dirInfo.EnumerateFiles().Count();
Assert.Greater(currentFiles, directoryFiles);
这篇关于如何检查Selenium WebDriver下载的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文