Chuchoo
Chuchoo

Reputation: 842

how to download a file inside Java project's resource directory

I have a scenario where I click on download link in my app and it downloads an excel sheet in my local drive. I am using selenium webdriver to automate and validate if file has in fact downloaded. I am able to download file and validate its presence in local file system. But how do I make webdriver download file in the resource directory of my project.

This is what I am doing now:

System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver_Path_Win);
            HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
            chromePrefs.put("profile.default_content_settings.popups", 0);
            chromePrefs.put("download.default_directory", "C:\\pixeldownload");
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("prefs", chromePrefs);
            DesiredCapabilities cap = DesiredCapabilities.chrome();
            cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            cap.setCapability(ChromeOptions.CAPABILITY, options);
            driver = new ChromeDriver(cap);

Right now I am hardcoding "C:\pixeldownload" but I want to put the file into "resource" folder inside my java project.

chromePrefs.put("download.default_directory", "resource"). Putting destination as "resource" is not helping me. How do i download file inside "resource" directory.

public boolean isFileDownloaded(String downloadPath,String fileName) {
        boolean flag = false;
        File dir = new File(System.getProperty("user.dir") + "\\resources\\downloads");
        File[] dir_contents = dir.listFiles();
        for (int i = 0; i < dir_contents.length; i++) {
            if (dir_contents[0].getName().contains(fileName))   
                return flag=true;   
                }
        return flag;
    }

Upvotes: 2

Views: 3505

Answers (1)

baadnews
baadnews

Reputation: 128

If you want to download files to your test resource directory try this:

String downloadDir = System.getProperty("user.dir") + "\\src\\test\\resources";

chromePrefs.put("download.default_directory", downloadDir);

I'm using this in my project.

Upvotes: 2

Related Questions