我试图在测试失败时拍摄屏幕截图,然后将其添加到我的报告中。当前,当它再次打开应用程序以执行我的extent.flush();
中的afterMethod()
时,需要截图。
我的屏幕截图课程在这里:
public class CaptureScreenShot {
private static final DateFormat dateFormat = new SimpleDateFormat("yyy_MM_dd SSS");
public static String captureScreen(WebDriver driver, String screenName) throws IOException {
TakesScreenshot screen = (TakesScreenshot) driver;
File src = screen.getScreenshotAs(OutputType.FILE);
String dest = System.getProperty("user.dir") + "Test-ScreenShots" + screenName + ".png";
File target = new File(dest);
FileUtils.copyFile(src, target);
return dest;
}
public static String generateFileName(ITestResult results) {
Date date = new Date();
return results.getName() + "_" + dateFormat.format(date);
}
}
报告构建类在这里:
public class ExtentTestNGReportBuilder {
public static ExtentReports extent;
public static ThreadLocal<ExtentTest> parentTest = new ThreadLocal<>();
public static ThreadLocal<ExtentTest> test = new ThreadLocal<>();
private String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date());
@BeforeSuite
public void beforeSuite() {
extent = ExtentManager.createInstance("MobileCustomerCare " + fileName + ".html");
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("C:\\Users\\tom.cockram\\Documents");
extent.attachReporter(htmlReporter);
}
@BeforeClass
public synchronized void beforeClass() {
ExtentTest parent = extent.createTest(getClass().getName());
parentTest.set(parent);
}
@BeforeMethod
public synchronized void beforeMethod(Method method) {
ExtentTest child = parentTest.get().createNode(method.getName());
test.set(child);
}
@AfterMethod
public synchronized void afterMethod(ITestResult result) throws IOException {
AppiumDriver<MobileElement> driver = MetricellTest.setupTests();
String screenShot = CaptureScreenShot.captureScreen(driver, CaptureScreenShot.generateFileName(result));
if (result.getStatus() == ITestResult.FAILURE) {
test.get().log(Status.FAIL, result.getName());
test.get().log(Status.FAIL, result.getThrowable());
test.get().fail("Screen Shot : " + test.get().addScreenCaptureFromPath(screenShot));
test.get().fail(result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
test.get().skip(result.getThrowable());
} else
test.get().pass("Test passed");
extent.flush();
}