Reputation: 597
I tried to find the root of the current project using the below code but it is not exactly giving me the project directory path instead it is giving me the user directory path,Have googled this and didn't find an appropriate solution.
If you can help me on this it will be very helpful.
Below is the code which I have tried:
The aim of the below code is to save the images to the current project path & when I use the below code it is not necessarily been saved in the project path.
Any suggestions?
FileUtils.copyFile(scrFile, new File(
System.getProperty("user.dir") + "/test-output/ScreenshotsTC/" + "screenshots" + currentDate + ".png"));
System.out.println("File name is: " + testname + currentDate + ".png");
Upvotes: 0
Views: 932
Reputation: 16524
Try this:
public String getPathFromWhereApplicationIsRunning() throws Exception {
try {
return new File(".").getCanonicalPath();
} catch (IOException ex) {
throw new Exception("Failed to get the absolute path of the process.",ex);
}
}
I used this method several times for standalone purposes (jar, maven, ant, eclipse, netbeans, etc)
This will not work if you will run your selenium code on a server (war , tomcat, widfly, spring-boot, etc). In this case, it will return:
java -jar ...
Use an environment variable for the screenshots or any other file locations. That will works on any stage:
System.getenv("SCREENSHOT_FOLDER") + "/test-output/ScreenshotsTC/" + "screenshots" + currentDate + ".png"));
Finally just export the variable before the execution:
#linux/mac
export SCREENSHOT_FOLDER=/foo/bar/
#windows
SET SCREENSHOT_FOLDER=/foo/bar/
Upvotes: 1