有没有办法在 After 方法中使用 ITestResult 获得完整的错误/异常? [英] Is there a way to get the complete error/exception using ITestResult in the After Method?

查看:55
本文介绍了有没有办法在 After 方法中使用 ITestResult 获得完整的错误/异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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:

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();
           }
    }

这只是显示一个线性错误(例如,空指针异常).有没有办法在 After 方法中使用 ITestResult 获取完整的错误/异常?

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?

推荐答案

您可以通过 ITestResult 对象在 AfterMethod 中获取它,如果没有通过 try catch() 块处理.

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

示例:

异常类型: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);
    } 
}

这里的 testDemo 方法因算术异常而失败,因此 ITestResult 有失败方法.通过使用 result.getThrowable(); 方法你可以有异常描述.

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

但是,如果事情是由 try-catch() 块处理的,则 AtferMethod 中不能有异常详细信息.

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);
    } 
}

这里的事情是由 try-catch 块处理的,所以它不会失败,测试方法成为通过.因此,您不能将 ITestResult 中的异常详细信息视为失败.

这篇关于有没有办法在 After 方法中使用 ITestResult 获得完整的错误/异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆