Monday, 26 May 2014

Selenium web driver Save the downloaded file to particular given path.



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.remote.DesiredCapabilities;



public class download {


public static void main(String[] args) throws InterruptedException {

String profilePath = "TestNG";
FirefoxProfile profile = new ProfilesIni().getProfile(profilePath);
String path="E:\\Download";
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", path);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword,application/csv,text/csv,image/png ,image/jpeg, application/pdf, text/html,text/plain,application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.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", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);

profile.setPreference("pdfjs.disabled", true);

WebDriver driver = new FirefoxDriver(profile);

driver.get("http://docs.seleniumhq.org/download/");
 driver.manage().window().maximize();
driver.findElement(By.xpath("//a[@href='http://selenium-release.storage.googleapis.com/2.41/selenium-java-2.41.0.zip']")).click();
 System.out.println("Success");

No comments:

Post a Comment