FenWangNZ / blog

I love learning. This is my wiki.
0 stars 0 forks source link

Selenium: Verify downloaded files on Windows installed on Mac via Parallel Desktop #5

Open FenWangNZ opened 3 years ago

FenWangNZ commented 3 years ago

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

Steps are:

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.

In this part of the code:

//Get download path
            string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            string pathDownload = Path.Combine(pathUser, "Downloads");
            DirectoryInfo downloadDir = new DirectoryInfo(pathDownload);

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. Screen Shot 2020-09-30 at 2 28 40 PM And we can compare the files in these two directories.

The download directory in the windows default directory: Screen Shot 2020-09-30 at 2 31 46 PM

The download directory in Mac default directory: Screen Shot 2020-09-30 at 2 32 02 PM

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.

Screen Shot 2020-09-30 at 2 27 35 PM

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!!!