# Capture screenshot when test failed

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

```java
@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);
        }
    }

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tvn.gitbook.io/selenium-java/capture-screenshot-when-test-failed.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
