How To Download Different Files Using Selenium WebDriver

In my PREVIOUS POST, We accept learnt near how to exercise in addition to utilisation custom profile of Firefox browser to utilisation It In selenium webdriver software automation test. Now permit me demo you lot how to create Firefox custom profile run time in addition to educate Its properties to download whatever file using selenium webdriver software testing tool. Many times you lot postulate to download different files from software spider web application similar MS Excel file, MS Word File, Zip file, PDF file, CSV file, Text file, ect..

It Is tricky means to download file using selenium webdriver software testing tool. Manually when you lot click on link to download file, It volition demo you lot dialogue to relieve file In your local crusade every bit shown In bellow given Image.

Now selenium webdriver software automation testing tool exercise non accept whatever characteristic to cause got this relieve file dialogue. But yes, Selenium webdriver has ane to a greater extent than real skillful characteristic past times which you lot exercise non postulate to cause got that dialogue in addition to you lot tin lavatory download whatever file real easily. We tin lavatory exercise It using webdriver's Inbuilt shape FirefoxProfile in addition to Its dissimilar methods. Before looking at event of downloading file, Let me depict you lot about affair near file's MIME types. Yes you lot must know MIME type of file which you lot wants to download using selenium webdriver software testing tool.

What Is MIME of File
MIME Is sum shape of Multi-purpose Internet Mail Extensions which Is useful to Identify file type past times browser or server to transfer online.

You postulate to furnish MIME type of file In your selenium webdriver assay then that you lot must hold out aware near It. There are many online tools available to know MIME type of whatever file. Just google alongside "MIME checker" to uncovering this variety of tools.

In our event given bellow, I accept used MIME types every bit shown bellow for dissimilar file types In bellow given selenium webdriver test.

  1. Text File (.txt) - text/plain
  2. PDF File (.pdf) - application/pdf
  3. CSV File (.csv) - text/csv
  4. MS Excel File (.xlsx) - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  5. MS give-and-take File (.docx) - application/vnd.openxmlformats-officedocument.wordprocessingml.document
Example Of downloading dissimilar files using selenium webdriver

package Testng_Pack;  import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  world shape downloadingfile {  WebDriver driver;    @BeforeTest  world void StartBrowser() {   //Create object of FirefoxProfile inward built shape to access Its properties.   FirefoxProfile fprofile = novel FirefoxProfile();   //Set Location to shop files later downloading.   fprofile.setPreference("browser.download.dir", "D:\\WebDriverdownloads");   fprofile.setPreference("browser.download.folderList", 2);   //Set Preference to non demo file download confirmation dialogue using MIME types Of dissimilar file extension types.   fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk",      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"//MIME types Of MS Excel File.     + "application/pdf;" //MIME types Of PDF File.     + "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" //MIME types Of MS physician File.     + "text/plain;" //MIME types Of text File.     + "text/csv"); //MIME types Of CSV File.   fprofile.setPreference( "browser.download.manager.showWhenStarting", simulated );   fprofile.setPreference( "pdfjs.disabled", truthful );   //Pass fprofile parameter In webdriver to utilisation preferences to download file.   driver = novel FirefoxDriver(fprofile);    }     @Test  world void OpenURL() throws InterruptedException{   driver.get(" ");          //Download Text File   driver.findElement(By.xpath("//a[contains(.,'Download Text File')]")).click();   Thread.sleep(5000);//To hold off till file gets downloaded.                 //Download PDF File   driver.findElement(By.xpath("//a[contains(.,'Download PDF File')]")).click();   Thread.sleep(5000);                 //Download CSV File   driver.findElement(By.xpath("//a[contains(.,'Download CSV File')]")).click();   Thread.sleep(5000);                 //Download Excel File   driver.findElement(By.xpath("//a[contains(.,'Download Excel File')]")).click();   Thread.sleep(5000);                 //Download Doc File   driver.findElement(By.xpath("//a[contains(.,'Download Doc File')]")).click();   Thread.sleep(5000);    }    @AfterTest  world void CloseBrowser() {     driver.quit();    } }

When you lot run higher upward example, It volition download all 5(Text, pdf, CSV, docx in addition to xlsx) files ane past times ane in addition to shop them In D:\WebDriverdownloads folder automatically every bit shown In bellow given example.



InterruptedException Is used alongside method OpenURL to cause got checked exception of Thread.sleep(5000). View detailed tutorials of exception treatment In selenium WebDriver software assay automation on THIS LINK.

This means you lot tin lavatory download whatever file using selenium webdriver similar nil file, exe file, etc.. Just know your file's MIME type in addition to download It every bit shown In higher upward example.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM

http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post