I would like to get argument names and their values from @Test in TestNG. These arguments are provided by @DataProvider. Normally, I can store it in variables of class by its not good idea when i would like to run test parallel. Here is my code:
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestHomePage{
@BeforeMethod(alwaysRun = true)
public void setup(Method method) throws Exception {
System.out.println("INFO ====setup===:" + method.getName());
// how can I get parameters and value from @Test?
//ex: sLogin="notLogin";sLike=""
}
@AfterMethod()
public void tearnDown(Method method) {
System.out.println("INFO ====tearnDown===:" + method.getName());
// how can I get parameters and value from @Test?
}
@DataProvider(name = "likedata", parallel = true)
public Object[][] likeDataprovider() throws Exception {
return new Object[][] { { "notLogin", "" }, { "loggedIn", "notLike" }, { "loggedIn", "liked" } };
}
@Test(dataProvider = "likedata")
public void verifyLikeFunction(String sLogin, String sLike, Method method) throws Exception {
// Test
}
}
Any one can help me?
Thanks.
To get the value, Add params
as parameter of @BeforeMethod
@BeforeMethod(alwaysRun = true)
public void setup(Method method, Object[] params) throws Exception {
System.out.println("INFO ====setup===:" + method.getName());
System.out.println("Parameter value:");
for (Object parameter : params) {
System.out.println(parameter);
}
}
Take look at Dependency Injection in TestNG for more info.
And to get parameter name,
@BeforeMethod(alwaysRun = true)
public void setup(Method method, Object[] params) throws Exception {
System.out.println("Before , INFO ====setup===:" + method.getName());
Parameter[] parameters= method.getParameters();
System.out.println("Parameter names:");
for (Parameter parameter : parameters) {
System.out.println(parameter.getName());
}
}
Getting parameter names are possible if debug information is included during compilation. See this answer for more details
Simply, If you're using Eclipse go to project -> properties -> Java Compiler -> check "Store information about method parameters (usable via reflection)
Same we can do in @AfterMethod
.