gpt4 book ai didi

selenium - 如何检查Selenium WebDriver下载的文件?

转载 作者:行者123 更新时间:2023-12-03 06:34:42 27 4
gpt4 key购买 nike

我使用 C# 在 Selenium webdriver 中编写了一个自动化测试,其中一个步骤需要从服务器下载 XLSX 文件。如何验证文件是否已成功下载并获取其名称?

问候

最佳答案

我找到了以下源代码的解决方案:

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 - 如何检查Selenium WebDriver下载的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45573483/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com