Search code examples
javaseleniumdownloadautomationautoit

How to access generic user's My Downloads folder in Java?


I am writing some Selenium WebDriver and AutoIt Java Tests using Chrome. When I download a file in Chrome, it automatically downloads it to the Downloads folder on my computer. I am sharing this code project with multiple other users on a repository, and so I do not want to have my Downloads folder hard coded in with my specific username in it.

What I mean by this is let's say the user name on my computer is "Eamon." The downloads folder on my machine would be "C:\Users\Eamon\Downloads," but lets say my friend Mark downloads the project from the shared repository, his downloads folder will be located at "C:\Users\Mark\Downloads," yet as he pulls from the repository to see my updated code, the download location will still be hard coded as ""C:\Users\Eamon\Downloads" which will result in a "Folder does not exist" error.

Is there a way in java to access the generic Downloads folder on a machine that would change based on who the user of the machine is? This would help my test a lot.


Solution

  • For a Windows machine :

    new File("C:/Users/" + System.getProperty("user.name") + "/Downloads/");
    

    Could use similar snippets for any other operating system.