Selenium Webdriver - Handle Upload and Download Dialog
This blog is to provide one stop solution to handle Upload and Download in Webdriver driver script.
To Handle to Upload
& Download
To upload
files through Webdriver script:
driver.setFileDetector(new LocalFileDetector());
driver.get("http://<AUT Url>/");
driver.findElement(By.id("upfile_0")).sendKeys("<FilePath>");
driver.findElement(By.id("readTermsOfUse")).click();
driver.findElement(By.name("form_uploads")).submit();
To
download files through the test files, you need to create a profile
capability containing all the necessary parameters, and then associate it with
the WebDriver.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList",
0);
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",
true);
profile.setPreference("browser.download.manager.showAlertOnComplete",
false);
profile.setPreference("browser.download.manager.useWindow",
false);
/* you will need to find the content-type of your
app and set it here. */
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream");
DesiredCapabilities caps = new
DesiredCapabilities();
caps.setCapability("browserName",
"firefox");
caps.setCapability("browserstack.debug",
"true");
caps.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new RemoteWebDriver(new
URL(URL), caps);
driver.get("https://rubygems.org/gems/selenium-webdriver");
WebElement element =
driver.findElement(By.id("download"));
element.click();
Thread.sleep(50000);