Skip to main content

Posts

Showing posts with the label java file separator

Get the Parent Directory!

We all work with files and file paths…I got this recently when I was trying to get the current directory of the user while he is navigating through the application. Issue: At any time, I should get the parent directory of the current user directory and perform the designated actions. Solution: We have system properties which will give us the current user directory… Access the system property using String userDir = System.getProperty(“user.dir”); Now you can user the File Java Class to access the parent directory as String parentDir = new File(userDir).getParent(); For Example: User is in /home/someuser/SeleniumTests/Test1 userDir will have the value /home/someuser/SeleniumTests/Test1 parentDir will have the value /home/someuser/SeleniumTests   Hope this helps! Happy Coding!

Working with File Path Separators

Something I learned very recently.  Have you ever written code which involved file path's? If yes then this post might be helpful...Even otherwise knowing something unknown or brushing up concepts known won't hurt - would they? :) So here I go, I have my Selenium Scripts which might run on both Linux and Windows machines. I don't store my test files on a central location which can be accessed over the network/internet, so when my script runs on a Linux machine it would look locally on the linux file system for the test files. Same is the case when I run my tests on Windows.  Here is what I used to do :)  My Initial Approach:  If ( Test_Running_On_Linux ){ return System . getProperty ( "user.dir" ) + "/" + TestProperties . strFilePathPart1 + "/"+ TestProperties.strFilePathPart2; } else if( Test_Running_On_Windows ){ return System . getProperty ( "user.dir" ) + "\\" + TestProperties . strFilePathPa...