Search code examples
javaseleniumselenium-grid

Selenium Grid how to get file from it with Java?


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.


Solution

  • 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());