Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check downloaded files Selenium WebDriver?

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

like image 899
Elexsandro Rangel dos Santos Avatar asked Aug 08 '17 16:08

Elexsandro Rangel dos Santos


People also ask

How does Selenium verify downloaded files?

Now, open Google Chrome (we are using Google Chrome as our default browser). Copy the URL: D:\data in the URL of your browser. Once you copy the URL, you will observe that the URL is updated as “file:///D:/data/ “. You will also observe the sample file created by you in the browser.

How do I find files downloaded from a website?

Press Ctrl+J to view browser downloads If you double-click a file in the list, it opens or runs. You can also click a button to open the folder on your computer where the file was downloaded. In the example pictured below, a downloaded file is shown in the Firefox downloads window (called the "Library").

Can we upload/download file using Selenium?

The most basic way of uploading files in Selenium is using the sendKeys method. It is an inbuilt feature for file upload in Selenium.


1 Answers

I found the solution with following source code:

string currentPage = Browser.Current.Url;
string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string downloadPath = Path.Combine(userPath, "Downloads");

DirectoryInfo dirInfo = new DirectoryInfo(downloadPath);

if (!dirInfo.Exists)
{
     dirInfo.Create();
}

int directoryFiles = dirInfo.EnumerateFiles().Count();

string elementXpath = "//div[@id='myDiv']/div/div/div[@class='atalhos']/a[1]";

bool isFirefox = (Browser.Current as FirefoxDriver) != null;
bool isChrome = (Browser.Current as ChromeDriver) != null;

IWebDriver browserDriver = null;

if (isChrome)
{
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.AddUserProfilePreference("download.default_directory", downloadPath);
    chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");

    browserDriver = new ChromeDriver(chromeOptions);
}
else if (isFirefox)
{               
    FirefoxProfile profile = new FirefoxProfile();                
    profile.SetPreference("browser.download.folderList", 2);                
    profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

    browserDriver = new FirefoxDriver(profile);
}

browserDriver.Navigate().GoToUrl(currentPage);

WebDriverWait wait = new WebDriverWait(browserDriver, TimeSpan.FromSeconds(15));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(elementXpath)));

IWebElement elemento = browserDriver.FindElement(By.XPath(elementXpath));

elemento.Click();

Thread.Sleep(7000);

dirInfo = new DirectoryInfo(downloadPath);

int currentFiles = dirInfo.EnumerateFiles().Count();

Assert.Greater(currentFiles, directoryFiles);
like image 152
Elexsandro Rangel dos Santos Avatar answered Nov 01 '22 15:11

Elexsandro Rangel dos Santos