Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试失败时截图_Java_Testng_Appium_Appium Android_Extentreports - Fatal编程技术网

Java 测试失败时截图

Java 测试失败时截图,java,testng,appium,appium-android,extentreports,Java,Testng,Appium,Appium Android,Extentreports,我试图在测试失败时截图,然后将它们添加到我的报告中。当前,当它再次打开应用程序以执行extent.flush()时,会出现一个屏幕截图 我的截图课在这里: public class CaptureScreenShot { private static final DateFormat dateFormat = new SimpleDateFormat("yyy_MM_dd SSS"); public static String captureScreen(WebDriv

我试图在测试失败时截图,然后将它们添加到我的报告中。当前,当它再次打开应用程序以执行
extent.flush()时,会出现一个屏幕截图afterMethod()中的code>

我的截图课在这里:

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();
}
报表生成类位于以下位置:

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();
}
public类extendettentngreportbuilder{
公共静态扩展范围;
public static ThreadLocal parentTest=new ThreadLocal();
公共静态ThreadLocal测试=新ThreadLocal();
私有字符串文件名=新的SimpleDataFormat(“yyyy-MM-dd-HH-MM”).format(新日期());
@套房前
在套房()之前公开作废{
extent=ExtentManager.createInstance(“mobilecustomecare”+fileName+”.html);
ExtentHtmlReporter htmlReporter=新的ExtentHtmlReporter(“C:\\Users\\tom.cockram\\Documents”);
extent.attachReporter(htmlReporter);
}
@课前
公共同步的void beforeClass(){
extendettest parent=extent.createTest(getClass().getName());
parentTest.set(父级);
}
@预处理法
公共同步void before方法(方法){
extendettest child=parentTest.get().createNode(method.getName());
测试集(儿童);
}
@后置法
公共同步的void afterMethod(ITestResult结果)引发IOException{
AppiumDriver driver=MetricellTest.setupTests();
String screenShot=captureshreenshot.captureScreen(驱动程序,captureshreenshot.generateFileName(结果));
if(result.getStatus()==ITestResult.FAILURE){
test.get().log(Status.FAIL,result.getName());
test.get().log(Status.FAIL,result.getThrowable());
test.get().fail(“屏幕截图:+test.get().addScreenCaptureFromPath(屏幕截图));
test.get().fail(result.getThrowable());
}else if(result.getStatus()==ITestResult.SKIP){
test.get().skip(result.getThrowable());
}否则
test.get().pass(“测试通过”);
expndure.flush();
}

强烈建议使用适配器,最小设置和所有报告代码都与测试分开。请查看以下内容:。以及相关的。一般的经验法则是确保不要在AfterMethod块中删除驱动程序对象,而是在AtferSuite中删除block@foursyth我刚刚实现了adapter和它非常简单。但它似乎没有那么强大。我如何使用它添加屏幕截图?我如何更改文件名以包含今天的日期和时间?@TomCockram请参阅。您可以使用适配器,并且仍然可以利用API的所有灵活性来自定义它,无论您想要什么。
ExtentTestManager.getTest(结果)
将在当前上下文中使用测试添加屏幕截图。