P. Bill
P. Bill

Reputation: 26

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

In @AfterMethodto 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));
    }
}

Upvotes: 0

Views: 162

Answers (1)

Investigator
Investigator

Reputation: 1549

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.

Upvotes: 1

Related Questions