-
tanish20-coder authoredc05518eb
//Author: Tanish Srivastava
package TestComponents;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class ScreenshotUtil {
private WebDriver driver;
public ScreenshotUtil(WebDriver driver) {
this.driver = driver;
}
public String captureScreenshot(String scenarioName) {
try {
if (driver instanceof TakesScreenshot) {
// Save the screenshot file
String screenshotName = scenarioName.replaceAll(" ", "_") + ".jpeg";
// Store screenshots in the 'reports' folder
String relativePath = "screenshots/" + screenshotName;
String absolutePath = System.getProperty("user.dir") + "/" + relativePath;
File screenshotFile = new File(absolutePath, screenshotName);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, screenshotFile);
return screenshotFile.getAbsolutePath(); // Return relative path for reports
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}