TestNG Tutorials 42: Parameters In TestNG or Parameterization of Methods in a TestNG Class
Hello Folks,
Arguments or Parameters help us to achieve reusability of codes and cleanliness of codes. We can do overloading of methods using different number, types and order of parameters to serve differently for different set of values.
TestNG also allows you to parameterized your tests. We will see how can we achieve parameterisation of methods in a TestNG class.
Usage of parameterized methods of TestNG in Selenium:
Some real time examples are as below:
- You need to login to your application with different users with different roles. You can not create multiple methods for each user with role type. Instead of that you can create a parameterized methods which will accept user type and login as required.
- You may need to connect different databases and each database may have different credentials. This can be achieved easily using parameterized methods.
You can parameterized @Test annotated methods as well as @BeforeXXXX @AfterXXXX methods. It is not like that you can parameterized on test methods. It is an interview question.
Let’ create some configuration methods and test methods with parameters:
[java]
package Parameters;
import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;