Search code examples
testng

Is there a way to get the complete error/exception using ITestResult in the After Method?


@Test(priority = 16, enabled = true)
    public void C2410859_FilterAlertsByCard()
            throws IOException, InterruptedException, ATUTestRecorderException, APIException {

        record.start();
        launchUrl();
        startResult();
        startTestCase(Thread.currentThread().getStackTrace()[1].getMethodName().toString());
        LoginApplication(username, password);
        HomePage homePage = new HomePage();
        homePage.MyAlerts.click();
        MyAlerts myalerts = new MyAlerts();

    }




    @AfterMethod
    public void afterMethod(ITestResult result) throws ATUTestRecorderException {
        resultOfTest(result);   
        endTestcase();
        endResult();
        record.stop();
        closeBrowsers();
    }

resultOfTest:

public static void resultOfTest(ITestResult result) {
        try
         {
            if(result.getStatus() == ITestResult.SUCCESS)
            {

                //Do something here
                System.out.println("passed **********");
            }

            else if(result.getStatus() == ITestResult.FAILURE)
            {
                 //Do something here
                System.out.println("Failed ***********");
                test.fail("This test is failed due to "+ result.getThrowable());

            }

             else if(result.getStatus() == ITestResult.SKIP ){

                System.out.println("Skiped***********");

            }
        }
           catch(Exception e)
           {
             e.printStackTrace();
           }
    }

This just displays One liner error(eg., Null Pointer exception). Is there a way to get the complete error/exception using ITestResult in the After Method?


Solution

  • You can get it in AfterMethod by ITestResult object, if things are not handle by try catch() block.

    Example:

    Exception Type : java.lang.ArithmeticException: / by zero

    @Test
    public void testDemo() {
        int i;
        i = 9 / 0;
    }
    
    @AfterMethod
    public void afterMethod(ITestResult result) {
        if (result.getStatus() == ITestResult.FAILURE) {
                String testResult = result.getThrowable();
                System.out.println(testResult);
        } 
    }
    

    Here testDemo method get fail by arithmetic exception, and so ITestResult has fail method. By using result.getThrowable(); method you can have exception description.

    But, if things are handle by try-catch() block you can not have exception details in AtferMethod.

    @Test
    public void testDemo() {
        try {
            int i;
            i = 9 / 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @AfterMethod
    public void afterMethod(ITestResult result) {
        if (result.getStatus() == ITestResult.FAILURE) {
                String testResult = result.getThrowable();
                System.out.println(testResult);
        } 
    }
    

    Here things are handle by try-catch block and so it can not get fail, Test method become pass. So you can not have exception details in ITestResult as failure.