Skip to content

Screenshot is not attached to Allure when executing it in beforeTestStop() method. #813

Closed as not planned

Description

@muarts

Describe the bug
I want to attach the screenshot of failed test to the Allure report. I used TestNG's ITestListener interface's onTestFailure() method first and see the attachment file is visible in the allure-results folder yet not visible in the report itself. After that, I implement the solution suggested here: #444
The attachment file is not visible both in the allure-results folder and report itself. When I debugged I saw that it executes the onTestFailure() method but beforeTestStop() is not executed.

You can see my implementation below.

public class TestListener implements ITestListener, TestLifecycleListener {

    ThreadLocal<WebDriver> drivers = new ThreadLocal<>();

    @Override
    public void onTestStart(ITestResult result) {
        System.out.printf("Test started: %s\n", result.getName());
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        System.out.printf("Test passed: %s\n", result.getName());
    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.out.printf("Test failed: %s\n", result.getName());
        WebDriver driver = ((BaseTest) result.getInstance()).getDriver();
        drivers.set(driver);
    }

    @Override
    public void beforeTestStop(TestResult result) {
        if(result.getStatus().name().equalsIgnoreCase("FAILED")) {
            takeScreenshot(drivers.get());
        }
    }

    private void takeScreenshot(WebDriver driver) {
        Allure.addAttachment("Screenshot of failed step", new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
    }

    @Attachment(value = "Page screenshot", type = "image/png")
    private byte[] saveScreenshot(WebDriver driver) {
        byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        return screenshot;
    }

}

Expected behavior
I want to see the screenshot of failed test in the Allure report.

Activity

  1. michalw666 commented on Sep 8, 2022

    @michalw666

    I have the same issue with beforeTestStop() method. Please advice.

    Allure version 2.19.0
    Test framework testng@7.6.1
    Allure adaptor allure-testng@2.19.0
    Generate report using allure-maven@2.11.2
  2. calvario31 commented on Sep 11, 2022

    @calvario31

    I have the same issue... tryt to ge the screenshot on test start and on test failure but on both cases says the same log message:

    [main] ERROR io.qameta.allure.AllureLifecycle - Could not add attachment: no test is running
    [main] ERROR io.qameta.allure.AllureLifecycle - Could not add attachment: no test is running
    

    Maybe is related to the browser version? I run on a previous framework I have locally and it also didnt get the screenshot (same error) even if they have old dependencies and previously was working

    Allure version 2.19.0
    Test framework testng@7.6.1
    Allure adaptor allure-testng@2.19.0
    Generate report using allure-maven@2.11.2
  3. SoLsysTeMus commented on Sep 14, 2022

    @SoLsysTeMus

    I have the same issue

    Allure version 2.19.0
    Test framework testng@7.5
    Allure adaptor allure-testng@2.19.0
    Generate report using allure-maven@2.11.2
  4. calvario31 commented on Sep 14, 2022

    @calvario31

    Describe the bug I want to attach the screenshot of failed test to the Allure report. I used TestNG's ITestListener interface's onTestFailure() method first and see the attachment file is visible in the allure-results folder yet not visible in the report itself. After that, I implement the solution suggested here: #444 The attachment file is not visible both in the allure-results folder and report itself. When I debugged I saw that it executes the onTestFailure() method but beforeTestStop() is not executed.

    You can see my implementation below.

    public class TestListener implements ITestListener, TestLifecycleListener {
    
        ThreadLocal<WebDriver> drivers = new ThreadLocal<>();
    
        @Override
        public void onTestStart(ITestResult result) {
            System.out.printf("Test started: %s\n", result.getName());
        }
    
        @Override
        public void onTestSuccess(ITestResult result) {
            System.out.printf("Test passed: %s\n", result.getName());
        }
    
        @Override
        public void onTestFailure(ITestResult result) {
            System.out.printf("Test failed: %s\n", result.getName());
            WebDriver driver = ((BaseTest) result.getInstance()).getDriver();
            drivers.set(driver);
        }
    
        @Override
        public void beforeTestStop(TestResult result) {
            if(result.getStatus().name().equalsIgnoreCase("FAILED")) {
                takeScreenshot(drivers.get());
            }
        }
    
        private void takeScreenshot(WebDriver driver) {
            Allure.addAttachment("Screenshot of failed step", new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
        }
    
        @Attachment(value = "Page screenshot", type = "image/png")
        private byte[] saveScreenshot(WebDriver driver) {
            byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
            return screenshot;
        }
    
    }
    

    Expected behavior I want to see the screenshot of failed test in the Allure report.

    I made it to work on this repo https://github.com/calvario-31/SeleniumJavaCompleteFramework/blob/main/src/main/java/listeners/AllureListeners.java but had to use a static driver

  5. baev commented on Mar 19, 2024

    @baev
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions