Search code examples
javaselenium-webdriverreport

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


Solution

  • 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