Hi,
I am using below piece of code for taking screenshot -
public void takeScreenShot(){
Date d=new Date();
String screenshotFile=d.toString().replace(":", "_").replace(" ", "_")+".png";
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String filePath ="Screenshots//"+screenshotFile;
try {
FileUtils.copyFile(srcFile, new File(filePath)); //Put Capture in folder "Screenshots" > *WORKING FINE*
} catch (IOException e) {
e.printStackTrace();
}
test.log(LogStatus.INFO,"Screenshot -> "+ test.addScreenCapture(filePath)); //fetch capture from folder "Screenshots" to Extent Report > *NOT WORKING*
}
Folder Structure >
Project
Src
Screenshots
Report
> I am using relative path, so that I can share reports in mail. I understand that I need to share (report+screenshot) folders while sending in mail.
> Right now, In my local when I run this code, I can see screenshot file being saved in .png format in folder : Screenshots, But Report is not fetching these screenshots.
Please help to assist. Many thanks
Instructor
Ashish Thakur Replied on 01/06/2021
addScreenCapture requires absolute path
You can refer this post
https://stackoverflow.com/questions/3204955/converting-relative-paths-to-absolute-paths
public String capture() throws IOException {
String screenshotPath = null;
try{
//take screenshot and save it in a file
File sourceFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//copy the file to the required path
File destinationFile = new File(System.getProperty("user.dir")+"/Report/image_" + System.currentTimeMillis()+ ".png");
FileHandler.copy(sourceFile, destinationFile);
String[] relatvePath = destinationFile.toString().split("Report");
screenshotPath = ".\\" + relatvePath[1];
}
catch(Exception e){
System.out.println("Failure to take screenshot "+e);
}
return screenshotPath;
}
public void takeScreenShot() throws IOException {
test.log(LogStatus.INFO,"Screenshot -> "+ test.addScreenCapture(capture()));
}
Folder Structure:
Folder Project
> Folder Report
> .html report
> screenshots
above worked for me
Instructor
Ashish Thakur Replied on 30/06/2021
Thats good