1

The issue is this: I run selenium tests in threads for several cases. during test a button "download" is clicked. This button calls an Ajax that generates a PDF and serve it to download. It download to "Download" as default.

I need to move those download to an specific location (each test has one) and I don't know the name of the file.

I have tried to change downlad directory during tests, but seems impossible.

I have tried to open "downloads" tab (chrome://downloads/) and explore it, but it seems impossible, it does not find web elements

I have tried to move (copy and delete from origin) the las file, but with severan tests runing in threads, it could be a problem.

Any ideas?

Thanks in advance

  • http://stackoverflow.com/questions/21935696/protractor-e2e-test-case-for-downloading-pdf-file – Barney Mar 16 '17 at 12:51
  • Hi there!, thanks for the link, but it did not work. – Miguel Abreu Mar 22 '17 at 12:30
  • What I finally have done is configure a different download path for every test `(String rutaDescarga ="C:\\Users\\XXX\\Downloads"+System.currentTimeMillis() +Math.random();) `whit this, every test case has its own path, and there is no error when the files are copied – Miguel Abreu Mar 22 '17 at 12:33

2 Answers2

1

What I finally have done is configure a different download path for every test

String rutaDescarga ="C:\\Users\\XXX\\Downloads"+System.currentTimeMillis() +Math.random();
File creaRuta = new File(rutaDescarga);
if(!creaRuta.exists()){
    creaRuta.mkdirs();
}
downloadPath = rutaDescarga;    
chromePref.put("download.default_directory",rutaDescarga);
options.setExperimentalOptions("prefs", chromePref);` 

whit this, every test case has its own path, and there is no error when the files are copied

SiKing
  • 10,003
  • 10
  • 39
  • 90
0

You can specify the download location. Below code might help you try it

File file = new File("./downloads");

        boolean b = false;

        if (!file.exists()) {

          b = file.mkdirs();
        }

        FileUtils.cleanDirectory(file);
        String downloadFolder = System.getProperty("user.dir")+"/downloads";

    if (browser.equalsIgnoreCase("Chrome")) {

        HashMap<String, Object> chromePref = new HashMap<>();
        chromePref.put("download.default_directory", downloadFolder);
        chromePref.put("download.prompt_for_download", "false");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePref);
        driver = new ChromeDriver(options);

    } else if (browser.equalsIgnoreCase("Firefox")) {

        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.dir",downloadFolder );  // folder
        profile.setPreference("pdfjs.disabled", true);  // disable the built-in viewer
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.panel.shown", false);
        profile.setPreference("browser.helperApps.neverAsksaveToDisk", "application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel");

        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.setCapability(FirefoxDriver.PROFILE, profile);
        firefoxOptions.setCapability(FirefoxDriver.MARIONETTE, true);
        firefoxOptions.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR, 0);

        driver = new FirefoxDriver(firefoxOptions);

    }