Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Selenium Webdriver - to handle file downloading

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.36k
    Comment on it

    Sometimes you have to download file by clicking on link and button on your application.In webdriver you can not handle window dialogue box for this you have to do some browser setting.

     

    • Webdriver provides FirefoxProfile inbuilt class and its different methods to download the file.

    First let me describe about file's MIME types, because you have to know about MIME type of file which you want to download.


    MIME:

    • MIME Is  Multi-purpose Internet Mail Extensions, It is useful to Identify file type by browser or server to transfer online.
    • In Webdriver test You have to give MIME type, so you have to knowledge about it.You can get the details about MIME type of any file by searching 'MIME checker' in google.

    MIME types of different file types:

    Text File (.txt) - text/plain
    PDF File (.pdf) - application/pdf
    CSV File (.csv) - text/csv
    MS Excel File (.xlsx) - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    MS word File (.docx) - application/vnd.openxmlformats-officedocument.wordprocessingml.document

    Preferences:

    setPreference("browser.download.folderList", 2);


    Value can be 0,1 and 2. When it set to '0' downloaded files will save in user's desktop. When it set to '1' downloaded file will save in user's downloads folder. When it set to '2 'it will save downloaded files to location specified for most recent download.

    Steps:-

    1. Create a firefox Profile.

    2. Set Preferences as per requirement.

    3. Open Firefox with firefox profile.

    Sample code:

    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;
    
    
    public class fileDownload {
     WebDriver driver;
      
      @BeforeTest
      public void openBrowser() {
       
       FirefoxProfile fprofile = new FirefoxProfile();//Create object of FirefoxProfile in built class to access Its properties.
       
       fprofile.setPreference("browser.download.dir", "D:\\WebDriverdownloads");//Set Location to store files after downloading.
       fprofile.setPreference("browser.download.folderList", 2);
       //Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
       fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
         "application/pdf");//MIME types Of PDF File.
         // "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;" //MIME types Of MS Excel File.
         // + "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" //MIME types Of MS doc File.
         // + "text/plain;" //MIME types Of text File.
         // + "text/csv"); //MIME types Of CSV File.
       fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
       fprofile.setPreference( "pdfjs.disabled", true );
       //Pass fprofile parameter In webdriver to use preferences to download file.
       driver = new FirefoxDriver(fprofile);  
      } 
      
      @Test
      public void OpenURL() throws InterruptedException{
       driver.get("http://www.escortsgroup.com/investor-information/annual-reports.html");
              
                 
       driver.findElement(By.xpath(".//*[@id='InnerRight']/div[1]/div/div/ul/li[1]/a")).click();//To Download PDF File
       Thread.sleep(5000);
       
       //driver.findElement(By.xpath("x path of the text file link ")).click();//To Download Text File
       //Thread.sleep(5000);//To wait till file gets downloaded.
                     
       //driver.findElement(By.xpath("x path of the CSV File link")).click();// To Download CSV File
       //Thread.sleep(5000);
                     
       //driver.findElement(By.xpath("x path of the Excel File link")).click();// To Download Excel File
       //Thread.sleep(5000);
                     
       //driver.findElement(By.xpath("x path of the doc file link")).click();// To Download Doc File
       //Thread.sleep(5000);  
      }
      
      @AfterTest
      public void CloseBrowser() {  
       driver.quit();  
      }
     }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: