79780644

Date: 2025-10-02 05:50:37
Score: 1.5
Natty:
Report link

Title:
Allure report not showing screenshots from TestNG Listener

Body:
I’m trying to attach screenshots in my Allure report using a TestNG ITestListener.
Here is my listener code:

@Attachment(value = "Screenshot", type = "image/png")
public static byte[] attachScreenshot(WebDriver driver) {
    return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}

@Override
public void onTestFailure(ITestResult result) {
    WebDriver driver = DriverManager_For_Jenkins.getDriver();
    if (driver != null) {
        attachScreenshot(driver);
    }
}

In my LoginPageTest, I also added:

@Listeners({
        RetryListener.class,
        ScreenshotListener.class,
        io.qameta.allure.testng.AllureTestNg.class
})
public class LoginPageTest extends CommonToAllTest {
    // test methods
}

and in testng.xml:

<listeners>
    <listener class-name="io.qameta.allure.testng.AllureTestNg"/>
    <listener class-name="org.example.listeners.RetryListener"/>
    <listener class-name="org.example.listeners.ScreenshotListener"/>
</listeners>

But when I generate the report with:

allure generate target/allure-results --clean -o target/allure-report
allure open target/allure-report

→ The report runs fine, but no screenshots are shown in the “Attachments” tab.

How can I correctly attach screenshots to Allure reports from a TestNG listener?


✅ Solution (to paste on StackOverflow)

The problem was caused by duplicate listener declarations.
I had added ScreenshotListener and RetryListener both in testng.xml and in the test class via @Listeners annotation.
Because of this, the listener was being triggered multiple times and Allure wasn’t attaching the screenshots properly.

Fix:

So I removed the @Listeners annotation from my test class and only kept the listeners in testng.xml:

<listeners>
    <listener class-name="io.qameta.allure.testng.AllureTestNg"/>
    <listener class-name="org.example.listeners.RetryListener"/>
    <listener class-name="org.example.listeners.ScreenshotListener"/>
</listeners>

After running tests and regenerating the report, screenshots started appearing under the “Attachments” section in Allure. ✅


Reasons:
  • Blacklisted phrase (0.5): How can I
  • Blacklisted phrase (1): StackOverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Sheikh Hamid