automated.test_manage.config.TakeScreenShot Maven / Gradle / Ivy
package automated.test_manage.config;
import automated.core.enums.EnvEnum;
import automated.core.enums.PathEnum;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import automated.reporting.TestReporter;
import org.testng.ITestResult;
import java.io.File;
/**
* Created by Ismail on 12/25/2017.
* This class contains all related methods for screenShot
* capturing and processing
*/
public class TakeScreenShot {
/*************** Class Methods Section ***************/
// This method capture screenShot on Test Case Fail
public static void captureScreenshotOnFail(ITestResult testResult) {
// Check if test case status is fail
if (testResult.getStatus() == ITestResult.FAILURE) {
// Save the image path to imagePath String variable
String imagePath = TakeScreenShot.captureScreenshot
(testResult.getName() + EnvEnum.CURRENT_TIME);
// If image is taken then send it to report test case logger
if (imagePath != null) {
String image = TestReporter.logger.addScreenCapture(imagePath);
TestReporter.info("Test Case Fail, Image Captured.\n"
+ image);
} else {// Else add error unable to get image and nothing to do
TestReporter.error("Fail to capture image.\n", false);
}
}
}
// This method capture screenShot and return image path string
public static String captureScreenshot(String imageName) {
// return image captured path string
return TakeScreenShot.captureScreenshot(Platform.driver, imageName);
}
// This method capture an image with WebDriver for
// current active tab and return image Path as string
private static String captureScreenshot(WebDriver driver, String screenshotName) {
try {// Initialize TakesScreenshot Object as WebDriver Type
TakesScreenshot screenshot = (TakesScreenshot) driver;
// Initialize File and save captured screenShot
// from getScreenshotAs Method to the file
File source = screenshot.getScreenshotAs(OutputType.FILE);
// Save default image Folder Path
String imageFolder = PathEnum.REPORT.toString();
// Save default image File Path and name
String imageFile = PathEnum.SCREENSHOTS + screenshotName + ".png";
// Copy captured image to imageFile absolute Path
FileUtils.copyFile(source, new File(imageFolder + imageFile));
return imageFile;// Return captured image as String
} catch (Throwable throwable) {
// In case fail then return null
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy