Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.8 kB
3
Indexable
Never
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

import WookieWebAdmin.Resources.ExtentReporterNG;
import io.qameta.allure.Attachment;

public class Listeners extends BaseTest implements ITestListener {
    ExtentTest test;
    ExtentReports extent = ExtentReporterNG.getReportObject();
    static ThreadLocal<ExtentTest> extentTest = new ThreadLocal(); // Thread safe
    ThreadLocal<Integer> retryAttempts = new ThreadLocal<>();

    @Override
    public void onTestStart(ITestResult result) {
        test = extent.createTest(result.getMethod().getMethodName());
        extentTest.set(test);
        System.out.println("Test Started: " + result.getMethod().getMethodName());
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        extentTest.get().log(Status.PASS, "Test Passed");
        extentTest.get().pass("Assertion Passed");
        captureScreenshot(result.getMethod().getMethodName());
        saveLogs(result.getMethod().getConstructorOrMethod().getName() + " - Test Passed");
        System.out.println("Test Passed: " + result.getMethod().getMethodName());
    }

    @Override
    public void onTestFailure(ITestResult result) {
        extentTest.get().log(Status.FAIL, "Test Failed");
        extentTest.get().fail(result.getThrowable());
        captureScreenshot(result.getMethod().getMethodName());
        saveLogs(result.getMethod().getConstructorOrMethod().getName() + " - Test Failed");
        System.out.println("Test Failed: " + result.getMethod().getMethodName());
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        extentTest.get().log(Status.SKIP, result.getThrowable()).skip("SKIPPED Testcase: " + result.getName());
        saveLogs(result.getMethod().getConstructorOrMethod().getName() + " - Test Skipped");
        System.out.println("Test Skipped: " + result.getMethod().getMethodName());
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        int attempts = retryAttempts.get();
        if (attempts > 0) {
            extentTest.get().log(Status.WARNING, "Retry attempt: " + attempts);
            extentTest.get().log(Status.INFO, "Test Case Retry");
            extentTest.get().log(Status.WARNING, "Check Code When Done");
            extentTest.get().log(Status.WARNING, "Retry Step: " + result.getName());
            extentTest.get().warning(result.getThrowable());
        }
        retryAttempts.set(attempts + 1);
        saveLogs(result.getMethod().getConstructorOrMethod().getName() + " - Test Failed (within success percentage)");
        System.out.println("Test Failed (within success percentage): " + result.getMethod().getMethodName());
    }

    @Override
    public void onStart(ITestContext context) {
        if (extent == null) {
            extent = ExtentReporterNG.getReportObject();
        }
        System.out.println("Test Suite Started: " + context.getSuite().getName());
    }

    @Override
    public void onFinish(ITestContext context) {
        extent.flush();
        System.out.println("Test Suite Finished: " + context.getSuite().getName());
    }

    @Attachment(value = "Stacktrace", type = "text/plain")
    public static String saveLogs(String message) {
        return message;
    }

    public void captureScreenshot(String methodName) {
        try {
            if (driver != null) {
                TakesScreenshot ts = (TakesScreenshot) driver;
                File screenshot = ts.getScreenshotAs(OutputType.FILE);

                String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                String screenshotPath = System.getProperty("user.dir") + "/reports/" + methodName + "_" + timestamp
                        + "_failed.png";

                FileUtils.copyFile(screenshot, new File(screenshotPath));
                System.out.println("Screenshot captured: " + screenshotPath);

                File screenshotFile = new File(screenshotPath);
                if (screenshotFile.exists()) {
                    extentTest.get().addScreenCaptureFromPath(screenshotPath);
                } else {
                    System.out.println("Screenshot file not found!");
                }
            } else {
                System.out.println("WebDriver is null. Cannot capture screenshot.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Leave a Comment