简体   繁体   中英

How to get inner method name of a method in TestNG, Selenium Test Script?

In @AfterMethod to get class name (TC_UI_PRM_N_001)
I just use this.getClassName().toString();
to get method name ( permitsPageSaveWithEmptyMandatoryFields )
I just use result.getMethod().getMethodName().toString();

   @AfterMethod(alwaysRun = true)
    public void catchExceptions(ITestResult result) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
        String className = this.getClassName().toString();
        String methodName = result.getMethod().getMethodName().toString();
...
}

if the step (internal method) searchTextInPage(getTestCaseData(1)) fails how can I get its name in @AfterMethod ?

public class TC_UI_PRM_N_001 extends BaseTestCase {
    Login login;
    HomePage homePage;
    Permits permits;

    @Test
    public void permitsPageSaveWithEmptyMandatoryFields(){
        login = new Login(getDriver());
        login.verifyCorrectLogin();

        homePage = new HomePage(getDriver());
        homePage.clickMenuItem("Permits");

        permits = new Permits(getDriver());
        permits.addNewCertificate()
                .validate()
                .searchTextInPage(getTestCaseData(1));
    }
}

Your program correctly returns the method name with the following line: (String methodName = result.getMethod().getMethodName().toString();). You need to retrieve the specific line inside that method that raised the exception. One simple way is to find this through the Stacktrace.

In order to easily retrieve the stacktrace as a string from the Throwable you could use something like this String stackTrace = ExceptionUtils.getStackTrace(result.getThrowable()); from the Apache Commons. Then you will be able to find the specific point in your program that raised the Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM