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){
}
else if(Test_Running_On_Windows){
}
Now what if while forming that file path, I miss my slashes -- my whole test fails for a file separator? Not fair!
So here is a better approach:
If ( Test_Running_On_Linux){
}
else if(Test_Running_On_Windows){
}
Thus we handle file separators better!
For more information about File Object and Properties: Java File Object, Java System Properties Tutorial
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.strFilePathPart1 + "\\"+ TestProperties.strFilePathPart2;
}
Now what if while forming that file path, I miss my slashes -- my whole test fails for a file separator? Not fair!
So here is a better approach:
If ( Test_Running_On_Linux){
return System.getProperty("user.dir") + File.separator + TestProperties.strFilePathPart1 + File.separator + TestProperties.strFilePathPart2;
}
else if(Test_Running_On_Windows){
return System.getProperty("user.dir") + File.separator + TestProperties.strFilePathPart1 + File.separator + TestProperties.strFilePathPart2;
}
Thus we handle file separators better!
For more information about File Object and Properties: Java File Object, Java System Properties Tutorial
Comments