Reputation: 55
I'm writing an automated test based on Selenium C#, using chromedriver. I'm testing that a certain file is downloaded after clicking the button. The filename changes everytime you download it, but it's always a .pdf The download path is preset to c:\Users\testcase\Downloads I simply cannot find a working C# code that verifies/asserts that the file has been downloaded. Any kind of help would be greatly appreciated. My code below
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);
????????????
Upvotes: 0
Views: 1201
Reputation: 55
So I managed to figure it out how to validate that the file has been downloaded & after validation the file is deleted. Code follows after the timeout. Code in 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);
Upvotes: 1
Reputation: 649
Try this, should work. But here is another problem u need to delete this file every time after assertion, because test case will always pass, if u downloaded any .pdf file even once. Let me know if it works for u, then we can try to delete file.
string pdfFile = @"c:\Users\testcase\Downloads\", "*.pdf)";
Console.WriteLine(File.Exists(pdfFile) ? "File exists." : "File does not exist.");
Upvotes: 1