Introduction
Actually I got this problem 2 days ago. I don't think it is a tricky one. I thought I would resolve it in an hour. However, I was wrong. It took me about 4 hours to get the answer.
The task is to verify downloaded files in Selenium with C#.
Steps are:
[ ] 1. Download the file by clicking on the pdf downloaded button beside the invoice number(Let's say: ABNZ000080);
[ ] 2. Find the downloaded file "ABNZ000080.pdf"
[ ] 3. Assert if the file exists
Codes are actually very simple and I got it from the website:
//Select an Issued invoice, Get this invoice number
string invoice_number_downloaded = driver.FindElement(By.XPath("//*[@class='table table-bordered table-striped table-hover table-light']/tbody/tr[1]/td[1]")).Text;
//Click PDF icon and download it
driver.FindElement(By.XPath("//*[@id='pdf_ico']")).Click();
Thread.Sleep(6000);
//Get download path
string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload = Path.Combine(pathUser, "Downloads");
DirectoryInfo downloadDir = new DirectoryInfo(pathDownload);
//get files in this path
FileInfo[] files = downloadDir.GetFiles("*.pdf");
//get full path name of the first/latest files
var filename = files[0].FullName;
//get filename
string getFileName = Path.GetFileName(filename);
//Assert filename if it contains invoice number
if (File.Exists(filename))
{
Assert.IsTrue(filename.Contains(invoice_number_downloaded));
}
File.Delete(filename);
//closes all browser windows and ends the WebDriver session.
driver.Quit();
However, I run many times and it always says:FileInfo[] files: System.IndexOutOfRangeException. Index was outside the bounds of the array.
Today, I finally found out the reason.
As I run 2 systems on my MacBook, so normally for a single Windows OS this would not be the case.
It is because the default download directory for windows is different from what is in MacOS.
These lines are to find the local download path in chrome browse. The debugging result is C:\Users\wangfen\Downloads.
The matter is here. Exactly!!
I was wrongly thinking this path is the same as where Chrome's default download directory. But actually it is not.
I installed the Chrome browser in both Windows and MAC, and the default download directory is MAC\Home\Downloads.
And we can compare the files in these two directories.
The download directory in the windows default directory:
The download directory in Mac default directory:
That's why I am always not able to find the downloaded file while the file has been downloaded into the mac chrome's default directory. So I have to change the default download directory to C:\Users\wangfen\Downloads.
Then I debugged, built and run, of course, the issue is resolved.
So running 2 OS to learn Selenium Webdriver with C# needs to consider more than normal using VS on WindowsOS.
But it is worth it!!!
Introduction Actually I got this problem 2 days ago. I don't think it is a tricky one. I thought I would resolve it in an hour. However, I was wrong. It took me about 4 hours to get the answer.
The task is to verify downloaded files in Selenium with C#.![Screen Shot 2020-09-30 at 2 15 04 PM](https://user-images.githubusercontent.com/48224242/94632538-5e8f7580-0327-11eb-88c5-e3f6710dfe6e.png)
Steps are:
Codes are actually very simple and I got it from the website:
However, I run many times and it always says:
FileInfo[] files: System.IndexOutOfRangeException. Index was outside the bounds of the array.
Today, I finally found out the reason. As I run 2 systems on my MacBook, so normally for a single Windows OS this would not be the case. It is because the default download directory for windows is different from what is in MacOS.
In this part of the code:
These lines are to find the local download path in chrome browse. The debugging result is
And we can compare the files in these two directories.
C:\Users\wangfen\Downloads
. The matter is here. Exactly!! I was wrongly thinking this path is the same as where Chrome's default download directory. But actually it is not. I installed the Chrome browser in both Windows and MAC, and the default download directory isMAC\Home\Downloads
.The download directory in the windows default directory:![Screen Shot 2020-09-30 at 2 31 46 PM](https://user-images.githubusercontent.com/48224242/94633539-1887e100-032a-11eb-8391-cb452c742360.png)
The download directory in Mac default directory:![Screen Shot 2020-09-30 at 2 32 02 PM](https://user-images.githubusercontent.com/48224242/94633545-1c1b6800-032a-11eb-9752-6a6b55fb09ce.png)
That's why I am always not able to find the downloaded file while the file has been downloaded into the mac chrome's default directory. So I have to change the default download directory to
C:\Users\wangfen\Downloads
.Then I debugged, built and run, of course, the issue is resolved.
So running 2 OS to learn Selenium Webdriver with C# needs to consider more than normal using VS on WindowsOS. But it is worth it!!!