Rama
Rama

Reputation: 43

How to print the selenium exception stack trace in Extent reports

I would like log comments after each step in a Selenium test in Extent reports. Therefore, when an exception thrown in a step, I would like to capture the stack trace and print it out on the Extent Reports. I could not find any help on line. Has anyone tried this before and found a way?

For example, the below create an instance of the report and log the comment

// new instance

ExtentReports extent = new ExtentReports(file-path, replaceExisting);

// starting test

ExtentTest test = extent.startTest("Test Name", "Sample description");

// step log

test.log(LogStatus.INFO, "Click on the object");

Reference:

http://extentreports.relevantcodes.com/java/version2/docs.html#initialize-report

Upvotes: 3

Views: 11560

Answers (4)

user21073071
user21073071

Reputation: 1

public class ExtentReport
{
   public static ExtentReports extentReports;
   public static ExtentTest exParentTest;
   public static ExtentTest exChildTest;
   public static string dirpath;
   public TestContext instance;
   public TestContext TestContext
   {
       set { instance = value; }
       get { return instance; }
   }
   public static void LogReport(string testcase)
   {
       extentReports = new ExtentReports();
       //dirpath = @"..\..\TestExecutionReports\" + '_' + testcase;
       dirpath = @"C:\ExtentReports\" + '_' + testcase;
       ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(dirpath);
       htmlReporter.Config.Theme = Theme.Standard;
       extentReports.AttachReporter(htmlReporter);
    }
}

Upvotes: 0

Rinash
Rinash

Reputation: 1

Since ExceptionUtils is deprecated

test.log(LogStatus.INFO/ERROR, ExceptionUtils.getStackTrace()); ->this won't help

So we can use

test.log(Status.INFO, "StackTrace Result: " + Thread.currentThread().getStackTrace());

Upvotes: 0

Karthik
Karthik

Reputation: 477

Or you can simply do

catch (Exception e) {
    test.log(LogStatus.FAIL, e);
}

Upvotes: 1

Vicky
Vicky

Reputation: 3021

If you want to log the stack trace of the exception you can convert the Exception stack trace to String. This class is available in Apache commons-lang-3.3.4 jar ExceptionUtils.getStackTrace(e)

Simple Example

    try{
             int num[]={1,2,3,4};
             System.out.println(num[5]);
        }catch(Exception e){        
        test.log(LogStatus.INFO/ERROR, ExceptionUtils.getStackTrace(e));
        }

Hope this Helps you...Kindly get back if you have any queries

Upvotes: 4

Related Questions