Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
7.1k views
by The go-to Tester (324 points)
retagged by
Customize the name of file going to be downloaded?

please discribe with code.
by The go-to Tester (181 points)
Are you looking for specific browser?
by The go-to Tester (324 points)
Yes, For 'Chrome' and 'Firefox' browser.

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)

This is divided into two parts, 1. Download the file 2. Change the file name.

To download the file using Chrome, you can use below code.

String downloadFilepath = "/path/to/download";
 
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
 
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
 
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOptions("prefs", chromePrefs);
options.addArguments("--test-type");
 
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
 
WebDriver driver = new ChromeDriver(cap);
 
To download file using Firefox, you can use below code
 
FirefoxProfile fprofile= new FirefoxProfile();
fprofile.setPreference("browser.download.folderList",2);  //0-desktop,1-file download folder,2-specified location
fprofile.setPreference("browser.download.manager.showWhenStarting", false); //prevent download file window
fprofile.setPreference("browser.download.dir","E:\\Downloadfilebyprogram");
fprofile.setPreference("browser.download.manager.focusWhenStarting", false);
fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/vnd.ms-excel");
fprofile.setPreference("browser.download.manager.alertOnExeOpen", false); //prevent from opening a file
 
cap.setCapability(FirefoxDriver.PROFILE, fprofile);
 
WebDriver driver=new FirefoxDriver(cap);

Once files are downloaded to your local machine, you can change the name of the file.

File oldfile =new File("oldfile.ext");
File newfile =new File("newfile.ext");
 
if(oldfile.renameTo(newfile)){
System.out.println("Renamed");
}else{
System.out.println("Not renamed");
}

Renaming of file will work only if you are executing tests on local machine, to change file name on the remote machine, you will have to implement FTP server  and create FTP client to make changes.

Hope that helps!


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...