I am using Reflection Method Class,or also ITestResult Class result to fetch the name of the currently running test,
@AfterMethod
public String getTestMethodName(ITestResult result)
{
return result.getName();
}
I am using Reflection Method Class,or also ITestResult Class result to fetch the name of the currently running test,
@AfterMethod
public String getTestMethodName(ITestResult result)
{
return result.getName();
}
Declare a parameter of type ITestResult
in your @AfterMethod
and TestNG
will inject it
@AfterMethod
public void afterMethod(ITestResult result) {
System.out.println("method name:" + result.getMethod().getMethodName());
}
OR
If you want to get the method name before the test is executed you can use the following:
import java.lang.reflect.Method;
@BeforeMethod
public void nameBefore(Method method)
{
System.out.println("Test name: " + method.getName());
}
Whenever you are creating a new object, pass the testcase name to the constructor and initilaize a testcaseName variable with the value from the Test method. LoginTest.java:
public class LoginTest{
@Test
public void sampleTest(Method method) {
String testcaseName = method.getName();
LoginPageActions loginPageActions = new LoginPageActions(testcaseName);
}
}
LoginPageActions.java:
public class LoginPageActions{
private String testCaseName = null;
public LoginPageActions(String testcaseName) {
this.testCaseName = testcaseName;
}
}