Capture screenshot when test failed

TestNG provide a listener capture all test events so we can reuse to check test pass or fail

@AfterMethod
    public void captureScreen(ITestResult testResult) throws IOException {
        if(!testResult.isSuccess()){
            File file = ((TakesScreenshot) Browser.getDriver()).getScreenshotAs(OutputType.FILE);
            File DestFile=new File("./target/screenshot/"
                    +testResult.getMethod().getMethodName()
                    +  "-"
                    +System.currentTimeMillis()+".png");

            FileUtils.copyFile(file, DestFile);
        }
    }

Last updated

Was this helpful?