0
package com.actitime1.generics;

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.support.events.EventFiringWebDriver;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class Listeners extends TestListenerAdapter 
{
    ExtentHtmlReporter reporter;
    ExtentReports reports;
    ExtentTest test;
    public void onStart(ITestContext testcontext)
    {
        String Date=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
        //String s="test-report"+Date+".html";
        reporter =new ExtentHtmlReporter(System.getProperty("user.dir")+"/test-output1/"+Date+".html");
        reports =new ExtentReports();
        reports.attachReporter(reporter);
        reporter.config().setDocumentTitle("First report");
        reporter.config().setReportName("colour report2");
        reporter.config().setTheme(Theme.DARK);
        reports.setSystemInfo("QA", "john");
        reports.setSystemInfo("Windows", "8.1");
        reports.setSystemInfo("Browser", "chrome");
        test=reports.createTest(testcontext.getName());
        BaseClass.log.info("report created");
    }
    public void onTestFinish(ITestContext rv)
    {
        reports.flush();
        BaseClass.log.info("report flushed");
    }
    public void onTestFailure(ITestResult rv)
    {
        test=reports.createTest(rv.getName());  
        test.log(Status.FAIL, MarkupHelper.createLabel(rv.getName(), ExtentColor.RED));
        String Date=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
        String path=System.getProperty("user.dir")+"//Screenshots/"+rv.getName()+Date+".jpg";
        EventFiringWebDriver e=new EventFiringWebDriver(BaseClass.driver);
        File f1=e.getScreenshotAs(OutputType.FILE);
        File f2=new File(path);
        try {
            FileUtils.copyFile(f1, f2);
            test.addScreenCaptureFromPath(path);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        BaseClass.log.fatal(rv.getName()+"method failed");
    }
        public void onTestSuccess(ITestResult rv)
        {
            //test=Reports.createTest(rv.getName());
            test.log(Status.PASS,MarkupHelper.createLabel(rv.getName(), ExtentColor.GREEN));
            BaseClass.log.info(rv.getName()+"method passed");
        }
        public void onTestSkip(ITestResult rv)
        {
            test=reports.createTest(rv.getName());
            test.log(Status.SKIP, MarkupHelper.createLabel(rv.getHost(), ExtentColor.ORANGE));
            BaseClass.log.info(rv.getName()+"method sikipped");
        }
        public void onTestStart(ITestResult rv)
        {
            System.out.println("test started");
        }

    }   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.