Sunday, 5 February 2017

Selenium Script To Auto Download Media Files


Selenium is having drawback of not interacting with elements outside web browser.
While testing application there might come requirement of downloading report or other media file.
With the help of selenium we can click on button which will initiate file downloading. On button click browser will open dialog box which will ask user to open or save file.
Selenium won't be able to interact with that dialog box as it is outside web browser.
Only way to interact is to use external libraries like robot or autoit.

But why to use other libraries if we can set browser preference to auto download media files as below.

public static void openBrowser(String object,String data){ Log.info("Opening Browser"); try{ if(data.equals("Mozilla")) { DesiredCapabilities capabilities = DesiredCapabilities.firefox(); FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.download.folderList", 2); profile.setPreference("browser.download.dir", "set downloadpath"); profile.setPreference("browser.download.manager.alertOnEXEOpen", false); profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, image/jpeg, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream"); profile.setPreference("browser.download.manager.showWhenStarting", false); profile.setPreference("browser.download.meanager.focusWhenStarting", false); profile.setPreference("browser.download.useDownloadDir", true); profile.setPreference("browser.helperApps.alwaysAsk.force", false); profile.setPreference("browser.download.manager.alertOnEXEOpen", false); profile.setPreference("browser.download.manager.closeWhenDone", true); profile.setPreference("browser.download.manager.showAlertOnComplete", false); profile.setPreference("browser.download.manager.useWindow", false); profile.setPreference("services.sync.prefs.sync.browser.download.manager. showWhenStarting",false); profile.setPreference("pdfjs.disabled", true); capabilities.setCapability(FirefoxDriver.PROFILE, profile); driver = new FirefoxDriver(capabilities); driver.manage().window().maximize(); Log.info("Mozilla browser started"); } else if(data.equals("IE")){ //Need to specify exe driver=new InternetExplorerDriver(); driver.manage().window().maximize(); Log.info("IE browser started"); } else if(data.equals("Chrome")){ //Need to specify exe System.setProperty("webdriver.chrome.driver", Constants.dir +"//chromedriver.exe"); driver=new ChromeDriver(); driver.manage().window().maximize(); Log.info("Chrome browser started"); } int implicitWaitTime=(20); driver.manage().timeouts().implicitlyWait(implicitWaitTime, TimeUnit.SECONDS); }catch (Exception e){ Log.info("Not able to open the Browser --- " + e.getMessage()); DriverScript.bResult = false; } }

The job is not over yet, we need to verify whether file is downloaded in the specified folder or not.


Here we will first check download directory is empty or not. If it is empty then no need to check further. If it contain files then we can compare name of files in the folder with our media file name. If it matches with expected file name then it is downloaded successfully.


public static void VerifyFileDownloaded(String DownloadedFileName)

{
try{
File dir = new File("downloadpath");
System.out.println("downloadpath");
System.out.println(DownloadedFileName);
    File[] files = dir.listFiles();
   if (files == null || files.length == 0) {
    DriverScript.bResult = false;
    DriverScript.Message = "Not able to Verify file download--- ";
   }
   for (int i = 1; i < files.length; i++) {
    if(files[i].getName().contains(DownloadedFileName)) {
    DriverScript.bResult = true;
DriverScript.Message = "Downloaded Image " + DownloadedFileName; 
    }
   }
  Thread.sleep(3000);
}catch(Exception e)
{
Log.error("Not able to Verify file download---  " + e.getMessage());
DriverScript.Message = "Not able to Verify file download--- " + e.getMessage();
DriverScript.bResult = false;
}
}


No comments:

Post a Comment