How to check downloaded files Selenium WebDriver?

Viewed 20876

I wrote an automation test in Selenium webdriver using C#, and one of steps requires to download a XLSX file from server. How to validate if file has downloaded successfully and get his name?

Regards

2 Answers

In the below code, i have taken the list of excel files in the download folder. If you have only one file then use file.name property or if you have multiple files try the below code.

 private static string GetDownloadedFileName()
    {
          var fileName = ConfigurationManager.AppSettings["excelName"].ToString();
          string pathUser=Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
          string pathDownload = Path.Combine(pathUser, "Downloads");

            DirectoryInfo downloadDir = new DirectoryInfo(pathDownload);
            FileInfo[] files = downloadDir.GetFiles("*.xls");
            var file = files.Where(x => x.Name.Replace(" ", "") == fileName + ".xls").FirstOrDefault();
            fileName = file.FullName;

            return fileName;
    }
Related