如何断言所需文件已下载 Selenium C# chromedriver
How to assert that needed file has been downloaded Selenium C# chromedriver
我正在使用 chromedriver 编写基于 Selenium C# 的自动化测试。我正在测试单击按钮后是否下载了某个文件。
每次下载时文件名都会更改,但始终为 .pdf
下载路径预设为c:\Users\testcase\Downloads
我根本找不到 verifies/asserts 文件已下载的工作 C# 代码。
任何形式的帮助将不胜感激。
我的代码如下
class FileIsDownloaded : OpenCloseDriver
{
[TestCase]
public void UserIsAbleToDownloadTheFile()
{
driver.Url = "https://MYWEBPAGE.COM";
IWebElement InitiateDownload = driver.findelement(By.Xpath(my-xpath);
InitiateDownload.Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
????????????
试试这个,应该可以。但是这是另一个问题,你需要在断言后每次都删除这个文件,因为测试用例总是会通过,即使你下载了任何 .pdf 文件一次。让我知道它是否适合你,然后我们可以尝试删除文件。
string pdfFile = @"c:\Users\testcase\Downloads\", "*.pdf)";
Console.WriteLine(File.Exists(pdfFile) ? "File exists." : "File does not exist.");
所以我想出了如何验证文件是否已下载并在验证后文件被删除的方法。代码在超时后跟随。 C# 中的代码
int PDFdownloaded = Directory.GetFiles(@"C:\Users\testcase\Downloads\", "*.pdf", SearchOption.AllDirectories).Length;
if (PDFdownloaded > 0)
{
Assert.IsTrue(PDFdownloaded > 0);
}
else
{
Assert.IsTrue(PDFdownloaded < 0);
}
string[] PDF = Directory.GetFiles(@"C:\Users\testcase\Downloads\", "*.pdf");
foreach (string file in PDF)
{
File.Delete(file);
我正在使用 chromedriver 编写基于 Selenium C# 的自动化测试。我正在测试单击按钮后是否下载了某个文件。 每次下载时文件名都会更改,但始终为 .pdf 下载路径预设为c:\Users\testcase\Downloads 我根本找不到 verifies/asserts 文件已下载的工作 C# 代码。 任何形式的帮助将不胜感激。 我的代码如下
class FileIsDownloaded : OpenCloseDriver
{
[TestCase]
public void UserIsAbleToDownloadTheFile()
{
driver.Url = "https://MYWEBPAGE.COM";
IWebElement InitiateDownload = driver.findelement(By.Xpath(my-xpath);
InitiateDownload.Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
????????????
试试这个,应该可以。但是这是另一个问题,你需要在断言后每次都删除这个文件,因为测试用例总是会通过,即使你下载了任何 .pdf 文件一次。让我知道它是否适合你,然后我们可以尝试删除文件。
string pdfFile = @"c:\Users\testcase\Downloads\", "*.pdf)";
Console.WriteLine(File.Exists(pdfFile) ? "File exists." : "File does not exist.");
所以我想出了如何验证文件是否已下载并在验证后文件被删除的方法。代码在超时后跟随。 C# 中的代码
int PDFdownloaded = Directory.GetFiles(@"C:\Users\testcase\Downloads\", "*.pdf", SearchOption.AllDirectories).Length;
if (PDFdownloaded > 0)
{
Assert.IsTrue(PDFdownloaded > 0);
}
else
{
Assert.IsTrue(PDFdownloaded < 0);
}
string[] PDF = Directory.GetFiles(@"C:\Users\testcase\Downloads\", "*.pdf");
foreach (string file in PDF)
{
File.Delete(file);