Search code examples
javajunitautomationselenide

java cleanDirectory() how to make directory path OS idependent


Please, help me with solution. I'm testing the files downloading process and to clear up the Downloads directory using in @AfterClass cleanDirectory() on MAC OS

@AfterClass
public static void clearDownloadDirectory() throws IOException{
    FileUtils.cleanDirectory(new File("/Users/irina/Downloads"));
}

How to make the Downloads directory path platform independent so it could work for Windows too?

Maybe I'm looking for the answer in a wrong way. Primary thing I need is to delete all xlsx files from the Downloads folder.


Solution

  • Depending on your app you can use System.getProperty("os.name").

    Maybe like this

    //...
    String url;
    switch( System.getProperty("os.name") ) {
        case "Windows 8.1":
        case "Windows Vista":
        //etc for all windows systems
        {
          url = "C:/Users/user_name/downloads..."
          break;
        }
        case "Mac OS X": //...
        case "SunOS": //...
        case "Linux": //...
        //etc all systems you want to support
    }
    

    EDIT

    Also System.getProperty("user.home") can help you

    "user.home" User home directory

    Java System properties list