How can I rename a file getting downloaded | Selenium Forum
M
Posted on 07/08/2016
Hi Ashish, how can I rename a file download using profiling concept or robot framework
Whenever a file is received for download, I want to save that particular file to custom location and save it with custom name.

Please let me know what all changes I need to do in the following script:

[code:36jwjwgj] // mime type

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);//0(desktop), 1(downloads folder) , or 2(specified dir)
profile.setPreference("browser.download.manager.showWhenStarting", false);// prevent Download Manager window
profile.setPreference("browser.download.dir", "C:\\Users\\Master\\Desktop\\erd.csv");//path
//profile.setPreference("browser.helperApps.neverAsk.openFile",
//"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms- excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);

WebDriver driver = new FirefoxDriver(profile);
driver.get("http://qtpselenium.com/test/testdownload.php");
driver.findElement(By.xpath("html/body/a[1]")).click();
driver.findElement(By.xpath("html/body/a[2]")).click();
driver.findElement(By.xpath("html/body/a[4]")).click();


/*System.setProperty("webdriver.ie.driver", "f:\\IEDriverServer.exe");
WebDriver driver= new InternetExplorerDriver();
driver.get("http://qtpselenium.com/test/testdownload.php");
driver.findElement(By.xpath("html/body/a[1]")).click();*/

Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_LEFT);
Thread.sleep(3000);
rb.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(3000);
rb.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(3000);
rb.keyPress(KeyEvent.VK_ENTER);[/code:36jwjwgj]

M
Replied on 08/08/2016

why do you want to change the name?

renaming can be done through java

http://stackoverflow.com/questions/1158777/rename-a-file-using-java


to download at specific location change the location in the given code.

[code:3cc0brcf]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.Test;

public class FileDownloadExample {

public static String downloadPath = "D:\\seleniumdownloads";
@Test
public void testDownload() throws Exception {
WebDriver driver = new FirefoxDriver(FirefoxDriverProfile());
driver.manage().window().maximize();
driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/");
driver.findElement(By.linkText("smilechart.xls")).click();
}

public static FirefoxProfile FirefoxDriverProfile() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
[color=#FF0000]profile.setPreference("browser.download.dir", downloadPath);[/color]
profile.setPreference("browser.helperApps.neverAsk.openFile",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
return profile;
}
}[/code:3cc0brcf]


M
Replied on 08/08/2016

Renaming the file name is the requirement of my project, also how can I find the old name of file in the folder when I have around thousands of files lying there in that specific folder?

I need to change the file name at the time of downloading the file. Please see how can I achieve this either through profiling concept by changing the browser preferences or through robot framework?


M
Replied on 08/08/2016

[quote:1e5hhqm4]Please see how can I achieve this either through profiling concept by changing the browser preferences or through robot framework?
[/quote:1e5hhqm4]

cannot be done through profile.

and it can be done through robot framework.

when download box appears you have to send the letters(arora) this way. it's very hard i won't recommend it.
[code:1e5hhqm4]
Robot robot;
robot = new Robot();
robot.keyPress(KeyEvent.VK_a);
robot.keyPress(KeyEvent.VK_r);
robot.keyPress(KeyEvent.VK_o);
robot.keyPress(KeyEvent.VK_r);
robot.keyPress(KeyEvent.VK_a);[/code:1e5hhqm4]