Friday 29 August 2014

Downloading a file using webdriver

Download a file using web driver :

    It is always a challenge to work with open source tools,as we do not get support from the author of the application and will have lot of limitations,one of the limitation of the web driver is that,it cannot interact with other than web applications,in such cases ,saving a file to your required location need to interact with Firefox popups to pick the location,but selenium(web driver) cannot support to do it

Solution : We can over come this problem using a simple trick (i.e) changing the browser settings ,when ever you download a file from browser(here it is firefox ),it will give you the popup with the options  "Open with" or "Save as ",this popup can be turned off using the mime types. See the below screenshot



    
If we have the above settings,then Firefox will not gives you the popup ,But question here,where this file can be saved? ,answer will be Firefox will save the file in a folder path given in your Firefox settings i.e 

browser.download.dir=<your downlod location>
browser.download.folderList=2;
browser.helperApps.neverAsk.saveToDisk=application/zip

We can achieve these settings with the web driver using the Firefox profile concept,below is the example which will download the selenium client libraries using the web driver:

 
Note : need to change the mime types for the type of the file  you are downloading.I have used the mime type for .zip file,that is 

    Profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");


Happy Automation!!!


 


No comments:

Post a Comment