FenWangNZ / blog

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

Selenium: Verify downloaded file #3

Open FenWangNZ opened 3 years ago

FenWangNZ commented 3 years ago

These days I came across a problem, which is to locate the downloaded file. However, I still haven't figured out why my code is not working??? I will update it once it is resolved...

//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();
            //Get userpath, download path
            string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            string pathDownload = Path.Combine(pathUser, "Downloads");
            DirectoryInfo downloadDir = new DirectoryInfo(pathDownload);
            //Get the files in the downloaded directory
            FileInfo[] files = downloadDir.GetFiles("*.pdf"); --------> Here 'files[0].FullName' threw an exception of type 'System.IndexOutOfRangeException'
            Thread.Sleep(6000);
            //Get the first file
            var filename = files[0].FullName;
            string getFileName = Path.GetFileName(filename);
            // Verify the file name if it is the expected downloaded one
            if (File.Exists(filename))
            {
                Assert.AreEqual(invoice_number_downloaded, getFileName);
            }
            File.Delete(filename);
            //closes all browser windows and ends the WebDriver session.
            driver.Quit();