I have Selenium Grid on remote machine with IP. One of my test cases has to download the file and in assertion I want to compare the name of downloaded file, also in another test case I have to import file to application from Windows. How to do that in Java? Selenium Grid is on Windows Server 2008.
Asked
Active
Viewed 3,730 times
2 Answers
2
As far as I'm aware that's not possible with selenium alone. You might be able to get browser logs, but what I do is enable file access to a shared server and check that the file is downloaded there. First I set Chrome's download directory:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "\\remote-ip\path\to\download\directory");
options.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://gridhubhost:4444/wd/hub"), capabilities);
Then after the test makes the browser download the file I check the filesystem on the remote server:
File downloadedFile = new File("\\remote-ip\path\to\download\directory\file");
assertEquals(downloadedFile.getName(), "expected-name");
[edit]: you might be better off asserting the file exists, e.g:
assertTrue(downloadedFile.exists());

Mark Lapierre
- 1,067
- 9
- 15
-
thank you very much, it works for me perfectly, I thought that I wille have to configure putty and ssh connection between machines. – robmax Apr 07 '17 at 07:21
-
Hmm, doesn't this just assert that the file's name is the same one as you specified in the new File(String path) constructor? – khriskooper Mar 29 '19 at 18:57
-
Ugh, I think you're right /facepalm. Might also need to assert the file exists. – Mark Lapierre Mar 29 '19 at 22:26
0
You can check if the downloaded file exists in the path Using java.io.File
:
File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) {
Sustem.out.printLn("File is downloaded");
}
also, if you want to import then you'll have to check whether there exists an editable Input field for file import, if yes then you can directly use sendkeys as per below:
driver.findElement(By.xpath("upload input path")).sendKeys("C:/Users/1.pdf");
otherwise you'll have to make use of AutoIt or Robot class if upload link opens windows dialogs.

Kushal Bhalaik
- 3,349
- 5
- 23
- 46