Prabu
Prabu

Reputation: 3728

download file stored Location and handling Download Popup using selenium webdriver with JAVA

Please suggest an idea with following points implementations

1.how to handle the Download popup in IE with Selenium Webdriver with JAVA? want to save that Xml file

2.how to store that xml file in different location by using JAVA?

Note: we will pass 'n' number of Inputs and each input have an xml file , required all the xml file download and save in different location

Upvotes: 4

Views: 12295

Answers (2)

praneel
praneel

Reputation: 1902

Try setting below set of preferences in your DesiredCapabilityObject before initializing driver object -

File ffProfileFolder = new File("." + File.separator + "src" + File.separator
                    + "test" + File.separator + "resources" + File.separator + "FFProfiles" + File.separator + "AutoUser" + File.separator);
File workspacePath = new File(".."+File.separator);
String workspaceCanPath = workspacePath.getCanonicalPath();
String downloadDir = workspaceCanPath+File.separator+"Downloads";
OSInteractions.createDir(downloadDir);

profileAutoUser.setPreference("browser.download.manager.showWhenStarting",false);
profileAutoUser.setPreference("browser.download.dir",downloadDir);
profileAutoUser.setPreference("browser.download.defaultFolder",downloadDir);
profileAutoUser.setPreference("browser.download.lastDir",downloadDir);
profileAutoUser.setPreference("browser.download.folderList",2);
profileAutoUser.setPreference("browser.download.useDownloadDir",true);
profileAutoUser.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream,application/msexcel");

DesiredCapabilities capFF = DesiredCapabilities.firefox();
capFF.setCapability(FirefoxDriver.PROFILE, profileAutoUser);


driver = new FirefoxDriver(profileAutoUser);

Note that this works for FF only.

Upvotes: 0

nilesh
nilesh

Reputation: 14307

I would recommend you NOT to automate file download using selenium. It's a trap you don't want to fall for. File download works differently in different browsers. People will recommend using AutoIT, but it only works for windows, so cross platform testing is not possible. Since you use Java bindings, you could use the Robot class, to move your mouse pointer to a certain location on the window and send a native click. In my experience this solution is really flaky. You don't know the exact location where you must click and with Robot you are blindly clicking on things. To add to this, when you are running tests on remote machine using selenium grid, things get even more difficult.

So how do you download the file? Just get the underlying link to download the file available in your DOM and fire a GET request. Download the content if you want to validate the file. If you don't want to validate the content, just response code is fine. Here is a fantastic blog with Java examples on how to download files in the background using http requests and detailed explanation on why downloading files using selenium is a bad idea.

Upvotes: 10

Related Questions